Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import pandas as pd
|
|
3 |
import os
|
4 |
import requests
|
5 |
|
|
|
6 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
7 |
MODEL_ID = "deepset/tinyroberta-squad2"
|
8 |
file_data = {"text": None}
|
@@ -11,27 +12,26 @@ headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
|
11 |
# Handle file upload
|
12 |
def upload_file(file):
|
13 |
if file is None or not hasattr(file, "name"):
|
14 |
-
return [], "No file uploaded."
|
15 |
|
16 |
-
filename = file.name
|
17 |
try:
|
18 |
-
if
|
19 |
df = pd.read_csv(file.name)
|
20 |
file_data["text"] = df.to_string(index=False)
|
21 |
-
elif
|
22 |
file_data["text"] = file.read().decode("utf-8")
|
23 |
else:
|
24 |
-
return [], "Please upload a .txt or .csv file."
|
25 |
-
return [], "File uploaded. Now ask your question."
|
26 |
except Exception as e:
|
27 |
-
return [], f"Error reading file: {e}"
|
28 |
|
29 |
# Handle user question
|
30 |
def ask_question(message, history):
|
31 |
if not message or not file_data["text"]:
|
32 |
-
return "", history + [[message, "Please upload a file and
|
33 |
|
34 |
-
context = file_data["text"][:1500]
|
35 |
|
36 |
payload = {
|
37 |
"inputs": {
|
@@ -47,19 +47,19 @@ def ask_question(message, history):
|
|
47 |
json=payload
|
48 |
)
|
49 |
result = response.json()
|
50 |
-
answer = result.get("answer", "No answer found.")
|
51 |
except Exception as e:
|
52 |
-
answer = f"API error: {e}"
|
53 |
|
54 |
return "", history + [[message, answer]]
|
55 |
|
56 |
-
# Gradio UI
|
57 |
with gr.Blocks() as demo:
|
58 |
-
chatbot = gr.Chatbot()
|
59 |
file_input = gr.File(label="Upload .txt or .csv")
|
60 |
msg = gr.Textbox(label="Your Question")
|
61 |
send = gr.Button("Send")
|
62 |
-
status = gr.Textbox(label="Status
|
63 |
|
64 |
file_input.change(upload_file, inputs=file_input, outputs=[chatbot, status])
|
65 |
send.click(ask_question, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
|
|
3 |
import os
|
4 |
import requests
|
5 |
|
6 |
+
# Get Hugging Face API token from secret
|
7 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
8 |
MODEL_ID = "deepset/tinyroberta-squad2"
|
9 |
file_data = {"text": None}
|
|
|
12 |
# Handle file upload
|
13 |
def upload_file(file):
|
14 |
if file is None or not hasattr(file, "name"):
|
15 |
+
return [], "β No file uploaded."
|
16 |
|
|
|
17 |
try:
|
18 |
+
if file.name.endswith(".csv"):
|
19 |
df = pd.read_csv(file.name)
|
20 |
file_data["text"] = df.to_string(index=False)
|
21 |
+
elif file.name.endswith(".txt"):
|
22 |
file_data["text"] = file.read().decode("utf-8")
|
23 |
else:
|
24 |
+
return [], "β Please upload a .txt or .csv file."
|
25 |
+
return [], "β
File uploaded. Now ask your question."
|
26 |
except Exception as e:
|
27 |
+
return [], f"β Error reading file: {e}"
|
28 |
|
29 |
# Handle user question
|
30 |
def ask_question(message, history):
|
31 |
if not message or not file_data["text"]:
|
32 |
+
return "", history + [[message, "π Please upload a file and type a question."]]
|
33 |
|
34 |
+
context = file_data["text"][:1500] # truncate long content
|
35 |
|
36 |
payload = {
|
37 |
"inputs": {
|
|
|
47 |
json=payload
|
48 |
)
|
49 |
result = response.json()
|
50 |
+
answer = result.get("answer", "π€· No answer found.")
|
51 |
except Exception as e:
|
52 |
+
answer = f"β API error: {e}"
|
53 |
|
54 |
return "", history + [[message, answer]]
|
55 |
|
56 |
+
# Build the Gradio UI
|
57 |
with gr.Blocks() as demo:
|
58 |
+
chatbot = gr.Chatbot(label="Chat with Your File")
|
59 |
file_input = gr.File(label="Upload .txt or .csv")
|
60 |
msg = gr.Textbox(label="Your Question")
|
61 |
send = gr.Button("Send")
|
62 |
+
status = gr.Textbox(label="Status", interactive=False)
|
63 |
|
64 |
file_input.change(upload_file, inputs=file_input, outputs=[chatbot, status])
|
65 |
send.click(ask_question, inputs=[msg, chatbot], outputs=[msg, chatbot])
|