AnthonyRiv09 commited on
Commit
400d3f9
·
verified ·
1 Parent(s): 9400209

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -16
app.py CHANGED
@@ -2,19 +2,17 @@ import gradio as gr
2
  import pandas as pd
3
  import os
4
  import requests
5
- from huggingface_hub import InferenceClient
6
 
7
- # Get Hugging Face token securely
8
  HF_TOKEN = os.getenv("HF_TOKEN")
9
  MODEL_ID = "deepset/tinyroberta-squad2"
10
-
11
  file_data = {"text": None}
12
  headers = {"Authorization": f"Bearer {HF_TOKEN}"}
13
 
14
  # Handle file upload
15
  def upload_file(file):
16
  if file is None or not hasattr(file, "name"):
17
- return "No file uploaded."
 
18
  filename = file.name
19
  try:
20
  if filename.endswith(".csv"):
@@ -23,18 +21,17 @@ def upload_file(file):
23
  elif filename.endswith(".txt"):
24
  file_data["text"] = file.read().decode("utf-8")
25
  else:
26
- return "Please upload a .txt or .csv file."
27
- return "File uploaded! Now ask your question."
28
  except Exception as e:
29
- return f"Error reading file: {e}"
30
 
31
- # Handle user questions
32
  def ask_question(message, history):
33
  if not message or not file_data["text"]:
34
- return "📄 Please upload a file and type a question before asking."
35
 
36
- # Safe now because we passed the checks
37
- context = file_data["text"][:1500]
38
 
39
  payload = {
40
  "inputs": {
@@ -54,18 +51,17 @@ def ask_question(message, history):
54
  except Exception as e:
55
  answer = f"API error: {e}"
56
 
57
- history.append((message, answer))
58
- return "", history
59
-
60
 
61
  # Gradio UI
62
  with gr.Blocks() as demo:
63
- chatbot = gr.Chatbot(label="Chat with Your File")
64
  file_input = gr.File(label="Upload .txt or .csv")
65
  msg = gr.Textbox(label="Your Question")
66
  send = gr.Button("Send")
 
67
 
68
- file_input.change(upload_file, inputs=file_input, outputs=chatbot)
69
  send.click(ask_question, inputs=[msg, chatbot], outputs=[msg, chatbot])
70
  gr.ClearButton([msg, file_input, chatbot])
71
 
 
2
  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}
9
  headers = {"Authorization": f"Bearer {HF_TOKEN}"}
10
 
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 filename.endswith(".csv"):
 
21
  elif filename.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 ask a question."]]
33
 
34
+ context = file_data["text"][:1500]
 
35
 
36
  payload = {
37
  "inputs": {
 
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 Message", 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])
66
  gr.ClearButton([msg, file_input, chatbot])
67