samirpeobz commited on
Commit
fa6e093
·
verified ·
1 Parent(s): 5e1459e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -20
app.py CHANGED
@@ -2,31 +2,26 @@ import gradio as gr
2
  import pandas as pd
3
  from transformers import pipeline
4
 
5
- # Load a small hosted model
6
  llm = pipeline("text2text-generation", model="google/flan-t5-base")
7
 
8
- def ask_from_excel(file, question):
9
  if file is None or not question:
10
- return "Please upload an Excel file and enter your question."
11
 
12
  df = pd.read_excel(file.name)
13
  context = df.head(10).to_string(index=False)
14
- prompt = f"Given the following table:\n{context}\n\nAnswer the question: {question}"
15
- answer = llm(prompt)[0]['generated_text']
16
- return answer.strip()
17
 
18
- def build_ui():
19
- with gr.Blocks() as demo:
20
- gr.Markdown("# Excel Q&A Chatbot")
21
- gr.Markdown("Ask questions based on your uploaded Excel sheet using a free LLM.")
22
-
23
- file_input = gr.File(label="Upload Excel File")
24
- question_input = gr.Textbox(label="Ask a Question")
25
- output_text = gr.Textbox(label="Answer")
26
 
27
- submit_button = gr.Button("Submit")
28
- submit_button.click(ask_from_excel, inputs=[file_input, question_input], outputs=output_text)
29
-
30
- return demo
31
-
32
- demo = build_ui()
 
2
  import pandas as pd
3
  from transformers import pipeline
4
 
5
+ # Load lightweight hosted model
6
  llm = pipeline("text2text-generation", model="google/flan-t5-base")
7
 
8
+ def chat(file, question):
9
  if file is None or not question:
10
+ return "Please upload an Excel file and ask a question."
11
 
12
  df = pd.read_excel(file.name)
13
  context = df.head(10).to_string(index=False)
14
+ prompt = f"Given this table:\n{context}\n\nQuestion: {question}"
15
+ result = llm(prompt, max_new_tokens=200)[0]["generated_text"]
16
+ return result.strip()
17
 
18
+ demo = gr.Interface(
19
+ fn=chat,
20
+ inputs=[gr.File(label="Upload Excel File"), gr.Textbox(label="Ask a Question")],
21
+ outputs=gr.Textbox(label="Answer"),
22
+ title="Excel Q&A Chatbot",
23
+ description="Ask questions from your Excel sheet using an open-source model."
24
+ )
 
25
 
26
+ if __name__ == "__main__":
27
+ demo.launch()