Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
| 7 |
# Replace with your model name
|
| 8 |
#MODEL_NAME = "ssirikon/Gemma7b-bnb-Unsloth"
|
| 9 |
#MODEL_NAME = "unsloth/gemma-7b-bnb-4bit"
|
| 10 |
-
MODEL_NAME = "Lohith9459/
|
| 11 |
|
| 12 |
# Load the model and tokenizer
|
| 13 |
max_seq_length = 512
|
|
@@ -20,14 +20,17 @@ load_in_4bit = True
|
|
| 20 |
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.bfloat16, device_map="auto")
|
| 21 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 22 |
|
| 23 |
-
def
|
| 24 |
-
instruction = "Generate
|
| 25 |
formatted_text = f"""Below is an instruction that describes a task. \
|
| 26 |
Write a response that appropriately completes the request.
|
|
|
|
| 27 |
### Instruction:
|
| 28 |
{instruction}
|
|
|
|
| 29 |
### Input:
|
| 30 |
-
{
|
|
|
|
| 31 |
### Response:
|
| 32 |
"""
|
| 33 |
inputs = tokenizer([formatted_text], return_tensors="pt").to("cuda")
|
|
@@ -35,21 +38,29 @@ def generate_subject(email_body):
|
|
| 35 |
generated_ids = model.generate(**inputs, streamer=text_streamer, max_new_tokens=512)
|
| 36 |
generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
| 37 |
|
| 38 |
-
def
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
return extract_subject(generated_text)
|
| 47 |
|
| 48 |
# Create the Gradio interface
|
| 49 |
demo = gr.Interface(
|
| 50 |
-
fn=
|
| 51 |
-
inputs=gr.Textbox(lines=
|
| 52 |
-
outputs=gr.Textbox(label="Generated
|
| 53 |
)
|
| 54 |
|
| 55 |
demo.launch()
|
|
|
|
| 7 |
# Replace with your model name
|
| 8 |
#MODEL_NAME = "ssirikon/Gemma7b-bnb-Unsloth"
|
| 9 |
#MODEL_NAME = "unsloth/gemma-7b-bnb-4bit"
|
| 10 |
+
MODEL_NAME = "Lohith9459/QnAD2_gemma7b"
|
| 11 |
|
| 12 |
# Load the model and tokenizer
|
| 13 |
max_seq_length = 512
|
|
|
|
| 20 |
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.bfloat16, device_map="auto")
|
| 21 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 22 |
|
| 23 |
+
def generate_answer(question):
|
| 24 |
+
instruction = "Generate an answer for the following question in less than two sentences."
|
| 25 |
formatted_text = f"""Below is an instruction that describes a task. \
|
| 26 |
Write a response that appropriately completes the request.
|
| 27 |
+
|
| 28 |
### Instruction:
|
| 29 |
{instruction}
|
| 30 |
+
|
| 31 |
### Input:
|
| 32 |
+
{question}
|
| 33 |
+
|
| 34 |
### Response:
|
| 35 |
"""
|
| 36 |
inputs = tokenizer([formatted_text], return_tensors="pt").to("cuda")
|
|
|
|
| 38 |
generated_ids = model.generate(**inputs, streamer=text_streamer, max_new_tokens=512)
|
| 39 |
generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
| 40 |
|
| 41 |
+
def get_answer(text):
|
| 42 |
+
start_tag = "### Response:"
|
| 43 |
+
|
| 44 |
+
# Find the start and end indices
|
| 45 |
+
start_idx = text.find(start_tag)
|
| 46 |
+
|
| 47 |
+
# Check if both tags are found
|
| 48 |
+
if start_idx == -1:
|
| 49 |
+
return None # Tags not found
|
| 50 |
+
|
| 51 |
+
# Extract content between the tags
|
| 52 |
+
answer = text[start_idx + len(start_tag):].strip()
|
| 53 |
+
|
| 54 |
+
return answer
|
| 55 |
+
|
| 56 |
+
return get_answer(generated_text)
|
| 57 |
|
|
|
|
| 58 |
|
| 59 |
# Create the Gradio interface
|
| 60 |
demo = gr.Interface(
|
| 61 |
+
fn=generate_answer,
|
| 62 |
+
inputs=gr.Textbox(lines=5, label="Ask Question on AI/ML"),
|
| 63 |
+
outputs=gr.Textbox(label="G-15 Gemma7b Model Generated Answer")
|
| 64 |
)
|
| 65 |
|
| 66 |
demo.launch()
|