Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,49 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
|
4 |
+
# Load your fine-tuned model and tokenizer
|
5 |
+
model_name = "legacy107/flan-t5-large-bottleneck-adapter-cpgQA-unique"
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, device_map="auto")
|
7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(
|
8 |
+
model_checkpoint, device_map="auto"
|
9 |
+
)
|
10 |
+
model.set_active_adapters("question_answering")
|
11 |
+
|
12 |
+
max_length = 512
|
13 |
+
max_target_length = 128
|
14 |
+
|
15 |
+
# Define your function to generate answers
|
16 |
+
def generate_answer(question, context):
|
17 |
+
# Combine question and context
|
18 |
+
input_text = f"question: {question} context: {context}"
|
19 |
+
|
20 |
+
# Tokenize the input text
|
21 |
+
input_ids = tokenizer(
|
22 |
+
input_text,
|
23 |
+
return_tensors="pt",
|
24 |
+
padding="max_length",
|
25 |
+
truncation=True,
|
26 |
+
max_length=512,
|
27 |
+
).input_ids
|
28 |
+
|
29 |
+
# Generate the answer
|
30 |
+
with torch.no_grad():
|
31 |
+
generated_ids = model.generate(input_ids, max_new_tokens=max_target_length)
|
32 |
+
|
33 |
+
# Decode and return the generated answer
|
34 |
+
generated_answer = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
35 |
+
|
36 |
+
return generated_answer
|
37 |
+
|
38 |
+
# Create a Gradio interface
|
39 |
+
iface = gr.Interface(
|
40 |
+
fn=generate_answer,
|
41 |
+
inputs=[
|
42 |
+
gr.inputs.Textbox(label="Question"),
|
43 |
+
gr.inputs.Textbox(label="Context")
|
44 |
+
],
|
45 |
+
outputs=gr.outputs.Textbox(label="Generated Answer")
|
46 |
+
)
|
47 |
+
|
48 |
+
# Launch the Gradio interface
|
49 |
+
iface.launch()
|