Spaces:
Runtime error
Runtime error
enstazao
commited on
Commit
·
4ccb996
1
Parent(s):
e3bc3ca
done with functionality
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForTableQuestionAnswering, TableQuestionAnsweringPipeline
|
| 5 |
+
|
| 6 |
+
# Load the tokenizer and model directly
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained("google/tapas-large-finetuned-wikisql-supervised")
|
| 8 |
+
model = AutoModelForTableQuestionAnswering.from_pretrained("google/tapas-large-finetuned-wikisql-supervised")
|
| 9 |
+
|
| 10 |
+
# Initialize the TableQuestionAnsweringPipeline manually
|
| 11 |
+
pipe = TableQuestionAnsweringPipeline(model=model, tokenizer=tokenizer)
|
| 12 |
+
|
| 13 |
+
def answer_question(uploaded_file, question):
|
| 14 |
+
# Convert the binary stream to a file-like object
|
| 15 |
+
file_like = BytesIO(uploaded_file)
|
| 16 |
+
|
| 17 |
+
# Read the uploaded file directly into a DataFrame
|
| 18 |
+
df = pd.read_csv(file_like)
|
| 19 |
+
|
| 20 |
+
# Convert all DataFrame elements to string, as TAPAS expects string inputs
|
| 21 |
+
df = df.astype(str)
|
| 22 |
+
|
| 23 |
+
# Use the pipeline to answer the question based on the table
|
| 24 |
+
result = pipe({"table": df, "query": question})
|
| 25 |
+
|
| 26 |
+
# Format the answer before returning it
|
| 27 |
+
answer = result['answer']
|
| 28 |
+
return answer
|
| 29 |
+
|
| 30 |
+
# Define the Gradio app interface
|
| 31 |
+
iface = gr.Interface(
|
| 32 |
+
fn=answer_question,
|
| 33 |
+
inputs=[gr.File(label="Upload CSV File", type="binary"), gr.Textbox(lines=2, placeholder="Ask a question...")],
|
| 34 |
+
outputs=gr.Text(),
|
| 35 |
+
title="Table-based Question Answering",
|
| 36 |
+
description="Upload a CSV file and ask a question related to the data in the file."
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Run the app
|
| 40 |
+
iface.launch()
|