Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import gradio as gr
|
|
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 |
|
@@ -60,22 +61,30 @@ 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 |
-
chatbot_ui.append({"role":
|
|
|
|
|
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}"
|
69 |
|
70 |
# Transcription
|
|
|
71 |
def transcribe_audio(file):
|
72 |
-
if not file:
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
-
|
78 |
-
def process_file(file):
|
79 |
if file is None:
|
80 |
return ""
|
81 |
filename = file.name
|
@@ -91,24 +100,19 @@ def process_file(file):
|
|
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
|
95 |
-
text = "Image uploaded. Please describe what you want to know."
|
96 |
else:
|
97 |
text = file.read().decode("utf-8")
|
98 |
|
99 |
if len(text) > 8000:
|
100 |
text = text[:8000]
|
101 |
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
{"role": "user", "content": text}
|
107 |
-
]
|
108 |
-
)
|
109 |
-
return response["choices"][0]["message"]["content"]
|
110 |
except Exception as e:
|
111 |
-
return f"β Error processing file: {e}"
|
112 |
|
113 |
# UI
|
114 |
with gr.Blocks(css="""
|
@@ -130,12 +134,12 @@ textarea, input[type='text'] { background: #f0f0f0; border-radius: 8px; }
|
|
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():
|
137 |
file_upload = gr.File(label="π Upload file", file_types=[".pdf", ".docx", ".txt", ".csv", ".xlsx", ".jpg", ".png"])
|
138 |
-
|
139 |
|
140 |
with gr.Row():
|
141 |
new_btn = gr.Button("π New Chat")
|
@@ -148,8 +152,6 @@ textarea, input[type='text'] { background: #f0f0f0; border-radius: 8px; }
|
|
148 |
send_btn.click(chat_with_groq, [chat_input, state], [chat_input, chatbot, state])
|
149 |
chat_input.submit(chat_with_groq, [chat_input, state], [chat_input, chatbot, state])
|
150 |
|
151 |
-
process_btn.click(process_file, [file_upload], [chat_input])
|
152 |
-
|
153 |
new_btn.click(lambda: ("", [], {"oai_history": [], "chatbot_ui": []}), [], [chat_input, chatbot, state])
|
154 |
save_btn.click(save_session, [state], [status])
|
155 |
save_btn.click(lambda: gr.update(choices=list_saved_files()), [], [dropdown])
|
|
|
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 |
|
|
|
61 |
with open(filename, "r", encoding="utf-8") as f:
|
62 |
oai_history = json.load(f)
|
63 |
chatbot_ui = []
|
64 |
+
for i in range(0, len(oai_history)):
|
65 |
+
if oai_history[i]["role"] == "user":
|
66 |
+
chatbot_ui.append({"role": "user", "content": oai_history[i]["content"]})
|
67 |
+
elif oai_history[i]["role"] == "assistant":
|
68 |
+
chatbot_ui.append({"role": "assistant", "content": oai_history[i]["content"]})
|
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}"
|
72 |
|
73 |
# Transcription
|
74 |
+
|
75 |
def transcribe_audio(file):
|
76 |
+
if not file:
|
77 |
+
return ""
|
78 |
+
try:
|
79 |
+
with open(file, "rb") as audio_file:
|
80 |
+
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
81 |
+
return transcript["text"]
|
82 |
+
except Exception as e:
|
83 |
+
return f"β Transcription error: {e}"
|
84 |
+
|
85 |
+
# File processor β now sends file content to model for follow-up conversation
|
86 |
|
87 |
+
def file_to_message(file, state):
|
|
|
88 |
if file is None:
|
89 |
return ""
|
90 |
filename = file.name
|
|
|
100 |
df = pd.read_csv(file.name) if ext == "csv" else pd.read_excel(file.name)
|
101 |
text = df.to_string()
|
102 |
elif ext in ["png", "jpg", "jpeg"]:
|
103 |
+
text = "[Image uploaded: please describe what you'd like me to do with it.]"
|
|
|
104 |
else:
|
105 |
text = file.read().decode("utf-8")
|
106 |
|
107 |
if len(text) > 8000:
|
108 |
text = text[:8000]
|
109 |
|
110 |
+
state["oai_history"].append({"role": "user", "content": f"File uploaded:\n{text}"})
|
111 |
+
state["chatbot_ui"].append({"role": "user", "content": f"File uploaded:\n{text}"})
|
112 |
+
|
113 |
+
return text, state
|
|
|
|
|
|
|
|
|
114 |
except Exception as e:
|
115 |
+
return f"β Error processing file: {e}", state
|
116 |
|
117 |
# UI
|
118 |
with gr.Blocks(css="""
|
|
|
134 |
send_btn = gr.Button("π", scale=1)
|
135 |
|
136 |
with gr.Row():
|
137 |
+
mic_audio = gr.Audio(source="microphone", type="filepath", label="ποΈ Record Voice")
|
138 |
mic_audio.change(transcribe_audio, [mic_audio], [chat_input])
|
139 |
|
140 |
with gr.Row():
|
141 |
file_upload = gr.File(label="π Upload file", file_types=[".pdf", ".docx", ".txt", ".csv", ".xlsx", ".jpg", ".png"])
|
142 |
+
file_upload.change(file_to_message, [file_upload, state], [chat_input, state])
|
143 |
|
144 |
with gr.Row():
|
145 |
new_btn = gr.Button("π New Chat")
|
|
|
152 |
send_btn.click(chat_with_groq, [chat_input, state], [chat_input, chatbot, state])
|
153 |
chat_input.submit(chat_with_groq, [chat_input, state], [chat_input, chatbot, state])
|
154 |
|
|
|
|
|
155 |
new_btn.click(lambda: ("", [], {"oai_history": [], "chatbot_ui": []}), [], [chat_input, chatbot, state])
|
156 |
save_btn.click(save_session, [state], [status])
|
157 |
save_btn.click(lambda: gr.update(choices=list_saved_files()), [], [dropdown])
|