NinaMwangi commited on
Commit
1070f39
·
verified ·
1 Parent(s): 5c80f20

Update app.py

Browse files

removed gr.state

Files changed (1) hide show
  1. app.py +22 -22
app.py CHANGED
@@ -1,27 +1,30 @@
1
  import gradio as gr
2
- print("Gradio version:", gr.__version__)
3
  from datasets import load_dataset
4
  from transformers import AutoTokenizer, TFAutoModelForSeq2SeqLM
5
 
 
6
  model_name = "NinaMwangi/T5_finbot"
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
  model = TFAutoModelForSeq2SeqLM.from_pretrained(model_name)
9
 
 
10
  dataset = load_dataset("virattt/financial-qa-10K")["train"]
11
 
12
- # Function to retrieve matching context
 
 
 
13
  def get_context_for_question(question):
14
  for item in dataset:
15
  if item["question"].strip().lower() == question.strip().lower():
16
  return item["context"]
17
  return "No relevant context found."
18
 
19
- # Define the prediction function (inference)
20
- def generate_answer(question, chat_history):
21
  context = get_context_for_question(question)
22
  prompt = f"Q: {question} Context: {context} A:"
23
 
24
-
25
  inputs = tokenizer(
26
  prompt,
27
  return_tensors="tf",
@@ -41,37 +44,34 @@ def generate_answer(question, chat_history):
41
  chat_history.append((question, answer))
42
  return "", chat_history
43
 
 
 
 
 
 
 
 
44
  with gr.Blocks(theme=gr.themes.Base()) as interface:
45
  gr.Markdown(
46
  """
47
  # 💬 Finance QA Chatbot
48
  Ask a finance-related question and get an accurate, concise response.
49
  Built using a fine-tuned T5 Transformer on financial Q&A data.
50
- """,
51
  )
52
 
53
  chatbot = gr.Chatbot(label="Finance Chatbot", height=400, bubble_full_width=False)
 
54
  with gr.Row():
55
  with gr.Column(scale=8):
56
- question_box = gr.Textbox(
57
- placeholder="Ask a finance question...", show_label=False, lines=2
58
- )
59
  with gr.Column(scale=1):
60
  submit_btn = gr.Button("Send")
61
 
62
  clear_btn = gr.Button("Clear Chat")
63
 
64
- # Chat state
65
- state = gr.State([])
66
-
67
- # Bind function
68
- submit_btn.click(
69
- fn=generate_answer,
70
- inputs=[question_box, state],
71
- outputs=[question_box, chatbot, state]
72
- )
73
-
74
- clear_btn.click(lambda: [], inputs=[], outputs=[chatbot, state])
75
 
76
- # Run app
77
- interface.launch(share=True)
 
1
  import gradio as gr
 
2
  from datasets import load_dataset
3
  from transformers import AutoTokenizer, TFAutoModelForSeq2SeqLM
4
 
5
+ # Load model and tokenizer
6
  model_name = "NinaMwangi/T5_finbot"
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
  model = TFAutoModelForSeq2SeqLM.from_pretrained(model_name)
9
 
10
+ # Load dataset for context retrieval
11
  dataset = load_dataset("virattt/financial-qa-10K")["train"]
12
 
13
+ # Global chat history
14
+ chat_history = []
15
+
16
+ # Context lookup
17
  def get_context_for_question(question):
18
  for item in dataset:
19
  if item["question"].strip().lower() == question.strip().lower():
20
  return item["context"]
21
  return "No relevant context found."
22
 
23
+ # Inference function
24
+ def generate_answer(question):
25
  context = get_context_for_question(question)
26
  prompt = f"Q: {question} Context: {context} A:"
27
 
 
28
  inputs = tokenizer(
29
  prompt,
30
  return_tensors="tf",
 
44
  chat_history.append((question, answer))
45
  return "", chat_history
46
 
47
+ # Clear history function
48
+ def clear_chat():
49
+ global chat_history
50
+ chat_history = []
51
+ return chat_history
52
+
53
+ # Gradio UI
54
  with gr.Blocks(theme=gr.themes.Base()) as interface:
55
  gr.Markdown(
56
  """
57
  # 💬 Finance QA Chatbot
58
  Ask a finance-related question and get an accurate, concise response.
59
  Built using a fine-tuned T5 Transformer on financial Q&A data.
60
+ """
61
  )
62
 
63
  chatbot = gr.Chatbot(label="Finance Chatbot", height=400, bubble_full_width=False)
64
+
65
  with gr.Row():
66
  with gr.Column(scale=8):
67
+ question_box = gr.Textbox(placeholder="Ask a finance question...", show_label=False, lines=2)
 
 
68
  with gr.Column(scale=1):
69
  submit_btn = gr.Button("Send")
70
 
71
  clear_btn = gr.Button("Clear Chat")
72
 
73
+ submit_btn.click(fn=generate_answer, inputs=question_box, outputs=[question_box, chatbot])
74
+ clear_btn.click(fn=clear_chat, outputs=chatbot)
 
 
 
 
 
 
 
 
 
75
 
76
+ # Launch app
77
+ interface.launch()