ambrosfitz commited on
Commit
597f19c
1 Parent(s): ba2ecb5

Create app.py

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