Spaces:
Runtime error
Runtime error
Modified for unsloth
Browse files
app.py
CHANGED
@@ -2,20 +2,42 @@ import gradio as gr
|
|
2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
import nltk
|
4 |
nltk.download('punkt')
|
|
|
|
|
5 |
|
6 |
-
def
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
return predicted_title
|
15 |
|
16 |
|
17 |
iface = gr.Interface(
|
18 |
-
fn=
|
19 |
inputs=[
|
20 |
gr.Dropdown(choices=["GSridhar1982/AIML_QA_Mistral7B_FineTuned_Unsloth","GSridhar1982/AIML_QA_Mistral7B_FineTuned_Unsloth"], label="Select Model"),
|
21 |
gr.Textbox(lines=5, label="Question")
|
|
|
2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
import nltk
|
4 |
nltk.download('punkt')
|
5 |
+
from peft import AutoPeftModelForCausalLM
|
6 |
+
from transformers import AutoTokenizer
|
7 |
|
8 |
+
def preprocess_text(text):
|
9 |
+
# Convert to lowercase
|
10 |
+
text = text.lower()
|
11 |
+
# Remove punctuation
|
12 |
+
text = re.sub(r'[^\w\s]', '', text)
|
13 |
+
# Remove extra whitespace
|
14 |
+
text = ' '.join(text.split())
|
15 |
+
return text
|
16 |
+
|
17 |
+
def generate_answer(model_name,question):
|
18 |
+
model = AutoPeftModelForCausalLM.from_pretrained(
|
19 |
+
model_name, # YOUR MODEL YOU USED FOR TRAINING
|
20 |
+
load_in_4bit = load_in_4bit,
|
21 |
+
)
|
22 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
23 |
+
|
24 |
+
question_preprocessed = preprocess_text(question)
|
25 |
+
inputs = tokenizer(
|
26 |
+
[
|
27 |
+
qa_prompt.format(
|
28 |
+
"Please provide the answer for the question", # instruction
|
29 |
+
question_preprocessed, # input
|
30 |
+
"", # output - leave this blank for generation!
|
31 |
+
)
|
32 |
+
], return_tensors = "pt")
|
33 |
+
outputs = model.generate(**inputs, max_new_tokens = 64, use_cache = True)
|
34 |
+
decoded_output = tokenizer.batch_decode(outputs,skip_special_tokens=True)[0]
|
35 |
+
predicted_title = nltk.sent_tokenize(decoded_output.strip())[0]
|
36 |
return predicted_title
|
37 |
|
38 |
|
39 |
iface = gr.Interface(
|
40 |
+
fn=generate_answer,
|
41 |
inputs=[
|
42 |
gr.Dropdown(choices=["GSridhar1982/AIML_QA_Mistral7B_FineTuned_Unsloth","GSridhar1982/AIML_QA_Mistral7B_FineTuned_Unsloth"], label="Select Model"),
|
43 |
gr.Textbox(lines=5, label="Question")
|