GSridhar1982 commited on
Commit
4808775
·
verified ·
1 Parent(s): aca0e4b

Modified for unsloth

Browse files
Files changed (1) hide show
  1. app.py +31 -9
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 generate_subject(model_name,question_body):
7
- tokenizer = AutoTokenizer.from_pretrained(model_name)
8
- model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
9
- inputs = ["Ask a question: " + question_body]
10
- inputs = tokenizer(inputs, max_length=512, truncation=True, return_tensors="pt")
11
- output = model.generate(**inputs, num_beams=8, do_sample=True, min_length=1, max_length=10)
12
- decoded_output = tokenizer.batch_decode(output, skip_special_tokens=True)[0]
13
- predicted_title = nltk.sent_tokenize(decoded_output.strip())[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  return predicted_title
15
 
16
 
17
  iface = gr.Interface(
18
- fn=generate_subject,
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")