ambrosfitz commited on
Commit
bf2d247
·
verified ·
1 Parent(s): a5b5e3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -21
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
  import torch
3
- from transformers import AutoModelForSeq2SeqLM, T5Tokenizer
4
  import time
5
  import sys
6
  import traceback
@@ -9,40 +9,32 @@ import traceback
9
  error_message = ""
10
 
11
  # Load the model and tokenizer from Hugging Face
12
- model_name = "ambrosfitz/history-qa-t5-base-large"
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:
@@ -63,8 +55,8 @@ try:
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.",
@@ -79,9 +71,8 @@ try:
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()}")
 
1
  import gradio as gr
2
  import torch
3
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
4
  import time
5
  import sys
6
  import traceback
 
9
  error_message = ""
10
 
11
  # Load the model and tokenizer from Hugging Face
12
+ model_name = "ambrosfitz/history-qa-flan-t5-large"
13
  try:
14
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
15
+ tokenizer = AutoTokenizer.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 a history question and answer based on this text: {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
+ generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
30
 
31
  # Parse the generated text
32
  parts = generated_text.split("Question: ")
33
  if len(parts) > 1:
34
+ qa_parts = parts[1].split("Answer: ")
35
  question = qa_parts[0].strip()
36
+ answer = qa_parts[1].strip() if len(qa_parts) > 1 else "No answer provided."
37
+ return f"Question: {question}\n\nAnswer: {answer}"
 
 
 
 
 
 
 
38
  else:
39
  return "Unable to generate a proper question and answer. Please try again with a different input."
40
  except Exception as e:
 
55
  slow_qa,
56
  chatbot=gr.Chatbot(height=500),
57
  textbox=gr.Textbox(placeholder="Enter historical text here...", container=False, scale=7),
58
+ title="History Q&A Generator (FLAN-T5)",
59
+ description="Enter a piece of historical text, and the model will generate a related question and answer.",
60
  theme="soft",
61
  examples=[
62
  "The American Revolution was a colonial revolt that took place between 1765 and 1783.",
 
71
 
72
  if error_message:
73
  print("Launching interface with error message.")
 
74
  else:
75
  print("Launching interface normally.")
76
+ iface.launch(debug=True)
77
  except Exception as e:
78
  print(f"An error occurred while creating or launching the interface: {str(e)}\n{traceback.format_exc()}")