Mustafizur Rahman
commited on
Commit
·
8f73a6e
1
Parent(s):
26b36a9
Add application file
Browse files- demo/app.py +54 -0
- demo/requirements.txt +7 -0
demo/app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gradio_spreadsheetcomponent import SpreadsheetComponent
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
import os
|
| 5 |
+
import pandas as pd
|
| 6 |
+
|
| 7 |
+
# Load environment variables
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
# Get Hugging Face API token from environment
|
| 11 |
+
hf_token = os.getenv("HUGGINGFACE_API_TOKEN")
|
| 12 |
+
if not hf_token:
|
| 13 |
+
raise EnvironmentError("HUGGINGFACE_API_TOKEN environment variable is not set.")
|
| 14 |
+
|
| 15 |
+
# Create sample data
|
| 16 |
+
sample_data = pd.DataFrame({
|
| 17 |
+
"Product": ["Laptop", "Smartphone", "Tablet", "Smartwatch"],
|
| 18 |
+
"Price": [1200, 800, 500, 300],
|
| 19 |
+
"Stock": [50, 100, 75, 150],
|
| 20 |
+
"Rating": [4.5, 4.8, 4.2, 4.6]
|
| 21 |
+
})
|
| 22 |
+
|
| 23 |
+
def answer_question(file, question):
|
| 24 |
+
if not file or not question:
|
| 25 |
+
return "Please upload a file and enter a question."
|
| 26 |
+
|
| 27 |
+
# Load the spreadsheet data
|
| 28 |
+
df = pd.read_excel(file.name)
|
| 29 |
+
|
| 30 |
+
print(df)
|
| 31 |
+
|
| 32 |
+
# Create a SpreadsheetComponent instance
|
| 33 |
+
spreadsheet = SpreadsheetComponent(value=df, hf_token=hf_token)
|
| 34 |
+
|
| 35 |
+
# Use the component to answer the question
|
| 36 |
+
answer = spreadsheet.answer_question(question)
|
| 37 |
+
print(answer)
|
| 38 |
+
return answer
|
| 39 |
+
|
| 40 |
+
with gr.Blocks() as demo:
|
| 41 |
+
gr.Markdown("# Spreadsheet Question Answering")
|
| 42 |
+
|
| 43 |
+
with gr.Row():
|
| 44 |
+
file_input = gr.File(label="Upload Spreadsheet", file_types=[".xlsx"])
|
| 45 |
+
question_input = gr.Textbox(label="Ask a Question")
|
| 46 |
+
|
| 47 |
+
answer_output = gr.Textbox(label="Answer", interactive=False, lines=4)
|
| 48 |
+
|
| 49 |
+
submit_button = gr.Button("Submit")
|
| 50 |
+
submit_button.click(answer_question, inputs=[file_input, question_input], outputs=answer_output)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
demo.launch()
|
demo/requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==5.32.1
|
| 2 |
+
pandas
|
| 3 |
+
git+https://github.com/huggingface/huggingface_hub.git
|
| 4 |
+
openpyxl
|
| 5 |
+
python-dotenv
|
| 6 |
+
numpy
|
| 7 |
+
gradio_spreadsheetcomponent
|