Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,71 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer, GenerationConfig, BitsAndBytesConfig
|
| 3 |
-
import gradio as gr
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
...
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer, GenerationConfig, BitsAndBytesConfig
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Authenticate using token from environment
|
| 7 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 8 |
+
login(token=hf_token)
|
| 9 |
+
|
| 10 |
+
# Use quantization for low-memory GPU inference
|
| 11 |
+
quantization_config = BitsAndBytesConfig(
|
| 12 |
+
load_in_4bit=True,
|
| 13 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
| 14 |
+
bnb_4bit_use_double_quant=True,
|
| 15 |
+
bnb_4bit_quant_type="nf4"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
model_name = "mistralai/Mistral-7B-Instruct-v0.3"
|
| 19 |
+
|
| 20 |
+
# Load model and tokenizer
|
| 21 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 22 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 23 |
+
model_name,
|
| 24 |
+
quantization_config=quantization_config,
|
| 25 |
+
torch_dtype=torch.bfloat16,
|
| 26 |
+
device_map="auto"
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# Define generation function
|
| 30 |
+
def generate_qa(text):
|
| 31 |
+
prompt = f"""### Instruction:
|
| 32 |
+
Based on the following SAP Note, generate exactly 20 unique and informative question-answer pairs.
|
| 33 |
+
Each question must refer to the SAP note number from text if additional context is needed.
|
| 34 |
+
Only output the pairs in the format:
|
| 35 |
+
Q1: ...
|
| 36 |
+
A1: ...
|
| 37 |
+
...
|
| 38 |
+
Q20: ...
|
| 39 |
+
A20: ...
|
| 40 |
+
|
| 41 |
+
### Input:
|
| 42 |
+
{text}
|
| 43 |
+
|
| 44 |
+
### Response:
|
| 45 |
+
"""
|
| 46 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 47 |
+
outputs = model.generate(
|
| 48 |
+
input_ids=inputs.input_ids,
|
| 49 |
+
attention_mask=inputs.attention_mask,
|
| 50 |
+
max_new_tokens=2500,
|
| 51 |
+
do_sample=True,
|
| 52 |
+
temperature=0.9,
|
| 53 |
+
top_p=0.95,
|
| 54 |
+
repetition_penalty=1.1,
|
| 55 |
+
pad_token_id=tokenizer.eos_token_id
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
output_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 59 |
+
qa_pairs = output_text.split("### Response:")[-1].strip()
|
| 60 |
+
return qa_pairs
|
| 61 |
+
|
| 62 |
+
# Define Gradio UI
|
| 63 |
+
demo = gr.Interface(
|
| 64 |
+
fn=generate_qa,
|
| 65 |
+
inputs=gr.Textbox(lines=20, label="SAP Note Text"),
|
| 66 |
+
outputs=gr.Textbox(lines=25, label="Generated Q&A Pairs"),
|
| 67 |
+
title="Mistral Q&A Generator for SAP Notes",
|
| 68 |
+
description="Upload or paste SAP Note content to generate 20 question-answer pairs."
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
demo.launch()
|