ambrosfitz commited on
Commit
1d618ed
·
verified ·
1 Parent(s): 6a46606

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -48
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
- print(f"Error loading model or tokenizer: {e}")
13
- raise
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
- input_text = f"Generate question: {text}"
21
- input_ids = tokenizer(input_text, return_tensors="pt", max_length=max_length, truncation=True).input_ids.to(device)
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
- options_and_answer = qa_parts[1].split("Correct Answer:")
35
- options = options_and_answer[0].strip()
36
 
37
- answer_and_explanation = options_and_answer[1].split("Explanation:")
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
- return f"Question: {question}\n\nOptions: {options}\n\nCorrect Answer: {correct_answer}\n\nExplanation: {explanation}"
42
- else:
43
- return "Unable to generate a proper question and answer. Please try again with a different input."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  def slow_qa(message, history):
46
- full_response = generate_qa(message)
47
- for i in range(len(full_response)):
48
- time.sleep(0.01) # Adjust this value to control the speed of the response
49
- yield full_response[:i+1]
 
 
 
50
 
51
  # Create and launch the Gradio interface
52
- gr.ChatInterface(
53
- slow_qa,
54
- chatbot=gr.Chatbot(height=500),
55
- textbox=gr.Textbox(placeholder="Enter historical text here...", container=False, scale=7),
56
- title="History Q&A Generator",
57
- description="Enter a piece of historical text, and the model will generate a related question, answer options, correct answer, and explanation.",
58
- theme="soft",
59
- examples=[
60
- "The American Revolution was a colonial revolt that took place between 1765 and 1783.",
61
- "World War II was a global conflict that lasted from 1939 to 1945, involving many of the world's nations.",
62
- "The Renaissance was a period of cultural, artistic, political, and economic revival following the Middle Ages."
63
- ],
64
- cache_examples=False,
65
- retry_btn="Regenerate",
66
- undo_btn="Remove last",
67
- clear_btn="Clear",
68
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
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()}")