Return textboxes on file upload errors (#214)

This commit is contained in:
stackviolator 2025-01-16 09:33:01 -06:00 committed by GitHub
parent fdf4fe49ba
commit a4ec1e5be3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 5 deletions

View File

@ -5,7 +5,7 @@ from smolagents import (
) )
agent = CodeAgent( agent = CodeAgent(
tools=[], model=HfApiModel(), max_steps=4, verbosity_level=0 tools=[], model=HfApiModel(), max_steps=4, verbosity_level=1
) )
GradioUI(agent, file_upload_folder='./data').launch() GradioUI(agent, file_upload_folder='./data').launch()

View File

@ -120,15 +120,21 @@ class GradioUI:
""" """
if file is None: if file is None:
return "No file uploaded" return gr.Textbox(
"No file uploaded", visible=True
), file_uploads_log
try: try:
mime_type, _ = mimetypes.guess_type(file.name) mime_type, _ = mimetypes.guess_type(file.name)
except Exception as e: except Exception as e:
return f"Error: {e}" return gr.Textbox(
f"Error: {e}", visible=True
), file_uploads_log
if mime_type not in allowed_file_types: if mime_type not in allowed_file_types:
return "File type disallowed" return gr.Textbox(
"File type disallowed", visible=True
), file_uploads_log
# Sanitize file name # Sanitize file name
original_name = os.path.basename(file.name) original_name = os.path.basename(file.name)
@ -181,7 +187,7 @@ class GradioUI:
) )
# If an upload folder is provided, enable the upload feature # If an upload folder is provided, enable the upload feature
if self.file_upload_folder is not None: if self.file_upload_folder is not None:
upload_file = gr.File(label="Upload a file", height=1) upload_file = gr.File(label="Upload a file")
upload_status = gr.Textbox( upload_status = gr.Textbox(
label="Upload Status", interactive=False, visible=False label="Upload Status", interactive=False, visible=False
) )