Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,67 +2,86 @@ import gradio as gr
|
|
2 |
import torch
|
3 |
from transformers import AutoModelForSeq2SeqLM, T5Tokenizer
|
4 |
import time
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# Load the model and tokenizer from Hugging Face
|
7 |
model_name = "ambrosfitz/history-qa-t5-base"
|
8 |
try:
|
9 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
10 |
tokenizer = T5Tokenizer.from_pretrained(model_name, use_fast=False)
|
|
|
|
|
11 |
except Exception as e:
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
# Set device
|
16 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
17 |
-
model.to(device)
|
18 |
|
19 |
def generate_qa(text, max_length=512):
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
with torch.no_grad():
|
24 |
-
outputs = model.generate(input_ids, max_length=max_length, num_return_sequences=1, do_sample=True, temperature=0.7)
|
25 |
-
|
26 |
-
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
27 |
-
|
28 |
-
# Parse the generated text
|
29 |
-
parts = generated_text.split("Question: ")
|
30 |
-
if len(parts) > 1:
|
31 |
-
qa_parts = parts[1].split("Options:")
|
32 |
-
question = qa_parts[0].strip()
|
33 |
|
34 |
-
|
35 |
-
|
36 |
|
37 |
-
|
38 |
-
correct_answer = answer_and_explanation[0].strip()
|
39 |
-
explanation = answer_and_explanation[1].strip() if len(answer_and_explanation) > 1 else "No explanation provided."
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
def slow_qa(message, history):
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
50 |
|
51 |
# Create and launch the Gradio interface
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import torch
|
3 |
from transformers import AutoModelForSeq2SeqLM, T5Tokenizer
|
4 |
import time
|
5 |
+
import sys
|
6 |
+
import traceback
|
7 |
+
|
8 |
+
# Global variables to store error information
|
9 |
+
error_message = ""
|
10 |
|
11 |
# Load the model and tokenizer from Hugging Face
|
12 |
model_name = "ambrosfitz/history-qa-t5-base"
|
13 |
try:
|
14 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
15 |
tokenizer = T5Tokenizer.from_pretrained(model_name, use_fast=False)
|
16 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
17 |
+
model.to(device)
|
18 |
except Exception as e:
|
19 |
+
error_message = f"Error loading model or tokenizer: {str(e)}\n{traceback.format_exc()}"
|
20 |
+
print(error_message)
|
|
|
|
|
|
|
|
|
21 |
|
22 |
def generate_qa(text, max_length=512):
|
23 |
+
try:
|
24 |
+
input_text = f"Generate question: {text}"
|
25 |
+
input_ids = tokenizer(input_text, return_tensors="pt", max_length=max_length, truncation=True).input_ids.to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
with torch.no_grad():
|
28 |
+
outputs = model.generate(input_ids, max_length=max_length, num_return_sequences=1, do_sample=True, temperature=0.7)
|
29 |
|
30 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
31 |
|
32 |
+
# Parse the generated text
|
33 |
+
parts = generated_text.split("Question: ")
|
34 |
+
if len(parts) > 1:
|
35 |
+
qa_parts = parts[1].split("Options:")
|
36 |
+
question = qa_parts[0].strip()
|
37 |
+
|
38 |
+
options_and_answer = qa_parts[1].split("Correct Answer:")
|
39 |
+
options = options_and_answer[0].strip()
|
40 |
+
|
41 |
+
answer_and_explanation = options_and_answer[1].split("Explanation:")
|
42 |
+
correct_answer = answer_and_explanation[0].strip()
|
43 |
+
explanation = answer_and_explanation[1].strip() if len(answer_and_explanation) > 1 else "No explanation provided."
|
44 |
+
|
45 |
+
return f"Question: {question}\n\nOptions: {options}\n\nCorrect Answer: {correct_answer}\n\nExplanation: {explanation}"
|
46 |
+
else:
|
47 |
+
return "Unable to generate a proper question and answer. Please try again with a different input."
|
48 |
+
except Exception as e:
|
49 |
+
return f"An error occurred: {str(e)}\n{traceback.format_exc()}"
|
50 |
|
51 |
def slow_qa(message, history):
|
52 |
+
try:
|
53 |
+
full_response = generate_qa(message)
|
54 |
+
for i in range(len(full_response)):
|
55 |
+
time.sleep(0.01)
|
56 |
+
yield full_response[:i+1]
|
57 |
+
except Exception as e:
|
58 |
+
yield f"An error occurred: {str(e)}\n{traceback.format_exc()}"
|
59 |
|
60 |
# Create and launch the Gradio interface
|
61 |
+
try:
|
62 |
+
iface = gr.ChatInterface(
|
63 |
+
slow_qa,
|
64 |
+
chatbot=gr.Chatbot(height=500),
|
65 |
+
textbox=gr.Textbox(placeholder="Enter historical text here...", container=False, scale=7),
|
66 |
+
title="History Q&A Generator",
|
67 |
+
description="Enter a piece of historical text, and the model will generate a related question, answer options, correct answer, and explanation.",
|
68 |
+
theme="soft",
|
69 |
+
examples=[
|
70 |
+
"The American Revolution was a colonial revolt that took place between 1765 and 1783.",
|
71 |
+
"World War II was a global conflict that lasted from 1939 to 1945, involving many of the world's nations.",
|
72 |
+
"The Renaissance was a period of cultural, artistic, political, and economic revival following the Middle Ages."
|
73 |
+
],
|
74 |
+
cache_examples=False,
|
75 |
+
retry_btn="Regenerate",
|
76 |
+
undo_btn="Remove last",
|
77 |
+
clear_btn="Clear",
|
78 |
+
)
|
79 |
+
|
80 |
+
if error_message:
|
81 |
+
print("Launching interface with error message.")
|
82 |
+
iface.launch(debug=True)
|
83 |
+
else:
|
84 |
+
print("Launching interface normally.")
|
85 |
+
iface.launch(debug=True)
|
86 |
+
except Exception as e:
|
87 |
+
print(f"An error occurred while creating or launching the interface: {str(e)}\n{traceback.format_exc()}")
|