Spaces:
Sleeping
Sleeping
Update Gradio_UI.py
Browse files- Gradio_UI.py +28 -50
Gradio_UI.py
CHANGED
|
@@ -215,15 +215,15 @@ class GradioUI:
|
|
| 215 |
import gradio as gr
|
| 216 |
|
| 217 |
if file is None:
|
| 218 |
-
return gr.Textbox("No file uploaded", visible=True), file_uploads_log
|
| 219 |
|
| 220 |
try:
|
| 221 |
mime_type, _ = mimetypes.guess_type(file.name)
|
| 222 |
except Exception as e:
|
| 223 |
-
return gr.Textbox(f"Error: {e}", visible=True), file_uploads_log
|
| 224 |
|
| 225 |
if mime_type not in allowed_file_types:
|
| 226 |
-
return gr.Textbox("File type disallowed", visible=True), file_uploads_log
|
| 227 |
|
| 228 |
# Sanitize file name
|
| 229 |
original_name = os.path.basename(file.name)
|
|
@@ -242,29 +242,28 @@ class GradioUI:
|
|
| 242 |
sanitized_name = "".join(sanitized_name)
|
| 243 |
|
| 244 |
# Save the uploaded file to the specified folder
|
| 245 |
-
|
| 246 |
-
shutil.
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
|
|
|
| 260 |
def launch(self, **kwargs):
|
| 261 |
import gradio as gr
|
| 262 |
-
|
| 263 |
with gr.Blocks(fill_height=True) as demo:
|
| 264 |
stored_messages = gr.State([])
|
| 265 |
file_uploads_log = gr.State([])
|
| 266 |
-
current_file_path = gr.State(None)
|
| 267 |
-
|
| 268 |
chatbot = gr.Chatbot(
|
| 269 |
label="Agent",
|
| 270 |
type="messages",
|
|
@@ -275,44 +274,23 @@ class GradioUI:
|
|
| 275 |
resizeable=True,
|
| 276 |
scale=1,
|
| 277 |
)
|
| 278 |
-
|
| 279 |
if self.file_upload_folder is not None:
|
| 280 |
upload_file = gr.File(label="Upload a file")
|
| 281 |
upload_status = gr.Textbox(label="Upload Status", interactive=False, visible=False)
|
| 282 |
-
|
| 283 |
upload_file.change(
|
| 284 |
self.upload_file,
|
| 285 |
[upload_file, file_uploads_log],
|
| 286 |
-
[upload_status, file_uploads_log
|
| 287 |
)
|
| 288 |
-
|
| 289 |
-
# New button to send the uploaded document to the agent
|
| 290 |
-
send_doc_btn = gr.Button("Send Document")
|
| 291 |
-
|
| 292 |
-
# Handler function for document sending
|
| 293 |
-
def send_document(file_path, messages):
|
| 294 |
-
if not file_path:
|
| 295 |
-
messages.append(gr.ChatMessage(role="assistant", content="No document uploaded."))
|
| 296 |
-
return messages
|
| 297 |
-
# Construct prompt with file path
|
| 298 |
-
prompt = f"Please process this document: {file_path}"
|
| 299 |
-
messages.append(gr.ChatMessage(role="user", content=prompt))
|
| 300 |
-
for msg in stream_to_gradio(self.agent, task=prompt, reset_agent_memory=False):
|
| 301 |
-
messages.append(msg)
|
| 302 |
-
yield messages
|
| 303 |
-
yield messages
|
| 304 |
-
|
| 305 |
-
send_doc_btn.click(
|
| 306 |
-
send_document,
|
| 307 |
-
inputs=[current_file_path, stored_messages],
|
| 308 |
-
outputs=chatbot,
|
| 309 |
-
)
|
| 310 |
-
|
| 311 |
text_input = gr.Textbox(lines=1, label="Chat Message")
|
| 312 |
text_input.submit(
|
| 313 |
self.log_user_message,
|
| 314 |
-
[text_input, file_uploads_log
|
| 315 |
[stored_messages, text_input],
|
| 316 |
).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
|
| 317 |
-
|
| 318 |
-
demo.launch(debug=True, share=True, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
import gradio as gr
|
| 216 |
|
| 217 |
if file is None:
|
| 218 |
+
return gr.Textbox("No file uploaded", visible=True), file_uploads_log
|
| 219 |
|
| 220 |
try:
|
| 221 |
mime_type, _ = mimetypes.guess_type(file.name)
|
| 222 |
except Exception as e:
|
| 223 |
+
return gr.Textbox(f"Error: {e}", visible=True), file_uploads_log
|
| 224 |
|
| 225 |
if mime_type not in allowed_file_types:
|
| 226 |
+
return gr.Textbox("File type disallowed", visible=True), file_uploads_log
|
| 227 |
|
| 228 |
# Sanitize file name
|
| 229 |
original_name = os.path.basename(file.name)
|
|
|
|
| 242 |
sanitized_name = "".join(sanitized_name)
|
| 243 |
|
| 244 |
# Save the uploaded file to the specified folder
|
| 245 |
+
file_path = os.path.join(self.file_upload_folder, os.path.basename(sanitized_name))
|
| 246 |
+
shutil.copy(file.name, file_path)
|
| 247 |
+
|
| 248 |
+
return gr.Textbox(f"File uploaded: {file_path}", visible=True), file_uploads_log + [file_path]
|
| 249 |
+
|
| 250 |
+
def log_user_message(self, text_input, file_uploads_log):
|
| 251 |
+
return (
|
| 252 |
+
text_input
|
| 253 |
+
+ (
|
| 254 |
+
f"\nYou have been provided with these files, which might be helpful or not: {file_uploads_log}"
|
| 255 |
+
if len(file_uploads_log) > 0
|
| 256 |
+
else ""
|
| 257 |
+
),
|
| 258 |
+
"",
|
| 259 |
+
)
|
| 260 |
+
|
| 261 |
def launch(self, **kwargs):
|
| 262 |
import gradio as gr
|
| 263 |
+
|
| 264 |
with gr.Blocks(fill_height=True) as demo:
|
| 265 |
stored_messages = gr.State([])
|
| 266 |
file_uploads_log = gr.State([])
|
|
|
|
|
|
|
| 267 |
chatbot = gr.Chatbot(
|
| 268 |
label="Agent",
|
| 269 |
type="messages",
|
|
|
|
| 274 |
resizeable=True,
|
| 275 |
scale=1,
|
| 276 |
)
|
| 277 |
+
# If an upload folder is provided, enable the upload feature
|
| 278 |
if self.file_upload_folder is not None:
|
| 279 |
upload_file = gr.File(label="Upload a file")
|
| 280 |
upload_status = gr.Textbox(label="Upload Status", interactive=False, visible=False)
|
|
|
|
| 281 |
upload_file.change(
|
| 282 |
self.upload_file,
|
| 283 |
[upload_file, file_uploads_log],
|
| 284 |
+
[upload_status, file_uploads_log],
|
| 285 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
text_input = gr.Textbox(lines=1, label="Chat Message")
|
| 287 |
text_input.submit(
|
| 288 |
self.log_user_message,
|
| 289 |
+
[text_input, file_uploads_log],
|
| 290 |
[stored_messages, text_input],
|
| 291 |
).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
|
| 292 |
+
|
| 293 |
+
demo.launch(debug=True, share=True, **kwargs)
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
__all__ = ["stream_to_gradio", "GradioUI"]
|