Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,6 @@ import gradio as gr
|
|
3 |
import json, re
|
4 |
import openai
|
5 |
from datetime import datetime
|
6 |
-
from pydub import AudioSegment
|
7 |
import pdfplumber, docx, pandas as pd
|
8 |
from PIL import Image
|
9 |
|
@@ -32,7 +31,8 @@ def chat_with_groq(message, state):
|
|
32 |
reply = f"β Error: {e}"
|
33 |
|
34 |
oai_history.append({"role": "assistant", "content": reply})
|
35 |
-
chatbot_ui.append(
|
|
|
36 |
|
37 |
return "", chatbot_ui, {"oai_history": oai_history, "chatbot_ui": chatbot_ui}
|
38 |
|
@@ -60,12 +60,9 @@ def load_chat(name):
|
|
60 |
with open(filename, "r", encoding="utf-8") as f:
|
61 |
oai_history = json.load(f)
|
62 |
chatbot_ui = []
|
63 |
-
for
|
64 |
-
if
|
65 |
-
|
66 |
-
if i + 1 < len(oai_history) and oai_history[i+1]["role"] == "assistant":
|
67 |
-
bot_msg = oai_history[i+1]["content"]
|
68 |
-
chatbot_ui.append([user_msg, bot_msg])
|
69 |
return chatbot_ui, {"oai_history": oai_history, "chatbot_ui": chatbot_ui}, f"β
Loaded {name}"
|
70 |
except Exception as e:
|
71 |
return [], {"oai_history": [], "chatbot_ui": []}, f"β Could not load {name}: {e}"
|
@@ -87,14 +84,14 @@ def process_file(file):
|
|
87 |
if ext == "pdf":
|
88 |
with pdfplumber.open(file.name) as pdf:
|
89 |
text = "\n".join([p.extract_text() for p in pdf.pages if p.extract_text()])
|
90 |
-
elif ext
|
91 |
doc = docx.Document(file.name)
|
92 |
text = "\n".join([p.text for p in doc.paragraphs])
|
93 |
elif ext in ["csv", "xlsx"]:
|
94 |
df = pd.read_csv(file.name) if ext == "csv" else pd.read_excel(file.name)
|
95 |
text = df.to_string()
|
96 |
elif ext in ["png", "jpg", "jpeg"]:
|
97 |
-
|
98 |
text = "Image uploaded. Please describe what you want to know."
|
99 |
else:
|
100 |
text = file.read().decode("utf-8")
|
@@ -126,14 +123,14 @@ textarea, input[type='text'] { background: #f0f0f0; border-radius: 8px; }
|
|
126 |
|
127 |
gr.Markdown("# π€ Neobot - Always Listening", elem_id="title")
|
128 |
|
129 |
-
chatbot = gr.Chatbot()
|
130 |
|
131 |
with gr.Row():
|
132 |
chat_input = gr.Textbox(placeholder="Type here or use mic...", scale=6, show_label=False)
|
133 |
send_btn = gr.Button("π", scale=1)
|
134 |
|
135 |
with gr.Row():
|
136 |
-
mic_audio = gr.Audio(
|
137 |
mic_audio.change(transcribe_audio, [mic_audio], [chat_input])
|
138 |
|
139 |
with gr.Row():
|
|
|
3 |
import json, re
|
4 |
import openai
|
5 |
from datetime import datetime
|
|
|
6 |
import pdfplumber, docx, pandas as pd
|
7 |
from PIL import Image
|
8 |
|
|
|
31 |
reply = f"β Error: {e}"
|
32 |
|
33 |
oai_history.append({"role": "assistant", "content": reply})
|
34 |
+
chatbot_ui.append({"role": "user", "content": message})
|
35 |
+
chatbot_ui.append({"role": "assistant", "content": reply})
|
36 |
|
37 |
return "", chatbot_ui, {"oai_history": oai_history, "chatbot_ui": chatbot_ui}
|
38 |
|
|
|
60 |
with open(filename, "r", encoding="utf-8") as f:
|
61 |
oai_history = json.load(f)
|
62 |
chatbot_ui = []
|
63 |
+
for m in oai_history:
|
64 |
+
if m["role"] in ["user", "assistant"]:
|
65 |
+
chatbot_ui.append({"role": m["role"], "content": m["content"]})
|
|
|
|
|
|
|
66 |
return chatbot_ui, {"oai_history": oai_history, "chatbot_ui": chatbot_ui}, f"β
Loaded {name}"
|
67 |
except Exception as e:
|
68 |
return [], {"oai_history": [], "chatbot_ui": []}, f"β Could not load {name}: {e}"
|
|
|
84 |
if ext == "pdf":
|
85 |
with pdfplumber.open(file.name) as pdf:
|
86 |
text = "\n".join([p.extract_text() for p in pdf.pages if p.extract_text()])
|
87 |
+
elif ext == "docx":
|
88 |
doc = docx.Document(file.name)
|
89 |
text = "\n".join([p.text for p in doc.paragraphs])
|
90 |
elif ext in ["csv", "xlsx"]:
|
91 |
df = pd.read_csv(file.name) if ext == "csv" else pd.read_excel(file.name)
|
92 |
text = df.to_string()
|
93 |
elif ext in ["png", "jpg", "jpeg"]:
|
94 |
+
Image.open(file.name) # just validate
|
95 |
text = "Image uploaded. Please describe what you want to know."
|
96 |
else:
|
97 |
text = file.read().decode("utf-8")
|
|
|
123 |
|
124 |
gr.Markdown("# π€ Neobot - Always Listening", elem_id="title")
|
125 |
|
126 |
+
chatbot = gr.Chatbot(type="messages")
|
127 |
|
128 |
with gr.Row():
|
129 |
chat_input = gr.Textbox(placeholder="Type here or use mic...", scale=6, show_label=False)
|
130 |
send_btn = gr.Button("π", scale=1)
|
131 |
|
132 |
with gr.Row():
|
133 |
+
mic_audio = gr.Audio(type="filepath", label="ποΈ Record Voice")
|
134 |
mic_audio.change(transcribe_audio, [mic_audio], [chat_input])
|
135 |
|
136 |
with gr.Row():
|