Spaces:
Runtime error
Runtime error
Commit
Β·
ae24dbd
1
Parent(s):
48d4c58
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import bitsandbytes as bnb
|
3 |
+
import pandas as pd
|
4 |
+
import torch
|
5 |
+
import torch.nn as nn
|
6 |
+
import transformers
|
7 |
+
|
8 |
+
|
9 |
+
from peft import (
|
10 |
+
LoraConfig,
|
11 |
+
PeftConfig,
|
12 |
+
get_peft_model,
|
13 |
+
prepare_model_for_kbit_training,
|
14 |
+
PeftModel
|
15 |
+
)
|
16 |
+
from transformers import (
|
17 |
+
AutoConfig,
|
18 |
+
AutoModelForCausalLM,
|
19 |
+
AutoTokenizer,
|
20 |
+
BitsAndBytesConfig,
|
21 |
+
)
|
22 |
+
|
23 |
+
import gradio as gr
|
24 |
+
|
25 |
+
import warnings
|
26 |
+
|
27 |
+
warnings.filterwarnings("ignore")
|
28 |
+
device = "cuda:0"
|
29 |
+
|
30 |
+
|
31 |
+
MODEL_NAME = 'diegi97/dolly-v2-6.9b-sharded-bf16'
|
32 |
+
|
33 |
+
bnb_config = BitsAndBytesConfig(
|
34 |
+
load_in_4bit=True,
|
35 |
+
load_4bit_use_double_quant=True,
|
36 |
+
bnb_4bit_quant_type="nf4",
|
37 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
38 |
+
)
|
39 |
+
|
40 |
+
model =AutoModelForCausalLM.from_pretrained(
|
41 |
+
MODEL_NAME,
|
42 |
+
device_map="auto",
|
43 |
+
trust_remote_code=True,
|
44 |
+
quantization_config=bnb_config,
|
45 |
+
)
|
46 |
+
|
47 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
48 |
+
tokenizer.pad_token = tokenizer.eos_token
|
49 |
+
|
50 |
+
peft_model_id = "AdiOO7/Azure-Classifier-dolly-7B"
|
51 |
+
# peft_model_id = "SparkExpedition/Ticket-Classifier-dolly-7B"
|
52 |
+
config = PeftConfig.from_pretrained(peft_model_id)
|
53 |
+
model = AutoModelForCausalLM.from_pretrained(
|
54 |
+
config.base_model_name_or_path,
|
55 |
+
return_dict=True,
|
56 |
+
quantization_config=bnb_config,
|
57 |
+
device_map="auto",
|
58 |
+
trust_remote_code=True,
|
59 |
+
)
|
60 |
+
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
|
61 |
+
tokenizer.pad_token = tokenizer.eos_token
|
62 |
+
|
63 |
+
model = PeftModel.from_pretrained(model, peft_model_id)
|
64 |
+
|
65 |
+
generation_config = model.generation_config
|
66 |
+
generation_config.max_new_tokens = 8
|
67 |
+
generation_config.num_return_sequences = 1
|
68 |
+
generation_config.temperature = 0.3
|
69 |
+
generation_config.top_p = 0.7
|
70 |
+
generation_config.pad_token_id = tokenizer.eos_token_id
|
71 |
+
generation_config.eos_token_id = tokenizer.eos_token_id
|
72 |
+
|
73 |
+
instruct = "From which azure service the issue is raised from {Power BI/Azure Data Factory/Azure Analysis Services}"
|
74 |
+
|
75 |
+
def generate_response(question: str) -> str:
|
76 |
+
|
77 |
+
prompt = f"""
|
78 |
+
### <instruction>: {instruct}
|
79 |
+
### <human>: {question}
|
80 |
+
### <assistant>:
|
81 |
+
""".strip()
|
82 |
+
|
83 |
+
encoding = tokenizer(prompt, return_tensors="pt").to(device)
|
84 |
+
with torch.inference_mode():
|
85 |
+
outputs = model.generate(
|
86 |
+
input_ids=encoding.input_ids,
|
87 |
+
attention_mask=encoding.attention_mask,
|
88 |
+
generation_config=generation_config,
|
89 |
+
)
|
90 |
+
response = tokenizer.decode(outputs[0],skip_special_tokens=True)
|
91 |
+
|
92 |
+
assistant_start = '<assistant>:'
|
93 |
+
response_start = response.find(assistant_start)
|
94 |
+
return response[response_start + len(assistant_start):].strip()
|
95 |
+
|
96 |
+
labels = ['PowerBI', 'Azure Data Factory', 'Azure Analysis Services']
|
97 |
+
|
98 |
+
def answer_prompt(prompt):
|
99 |
+
response = generate_response(prompt)
|
100 |
+
for lab in labels:
|
101 |
+
if response.find(lab) != -1:
|
102 |
+
return lab
|
103 |
+
|
104 |
+
iface = gr.Interface(fn=answer_prompt,
|
105 |
+
inputs=gr.Textbox(lines=5, label="Enter Your Issue", css={"font-size":"18px"}),
|
106 |
+
outputs=gr.Textbox(lines=5, label="Generated Answer", css={"font-size":"16px"}))
|
107 |
+
|
108 |
+
iface.launch()
|