Spaces:
Runtime error
Runtime error
Enrique Cardoza commited on
Commit ·
0bf4229
1
Parent(s): 4c49185
fix(upload): resolve mp4 file upload validation issues
Browse files- Add dedicated file validation function with size and extension checks
- Replace MIME type patterns with explicit file extensions list
- Implement proper validation in chat function with error handling
- Add size limit of 25MB with user-friendly error messages
- Show validation errors directly in the chat interface
- Support common audio/video formats (.mp3, .mp4, .wav, etc.)
app.py
CHANGED
|
@@ -28,6 +28,25 @@ def format_history(history):
|
|
| 28 |
messages.append({"role": item["role"], "content": item["content"]})
|
| 29 |
return messages
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
# Main chat function (streaming, with tool usage)
|
| 32 |
def chat_with_tools(user_message, history, audio_file):
|
| 33 |
# Add user message
|
|
@@ -37,6 +56,17 @@ def chat_with_tools(user_message, history, audio_file):
|
|
| 37 |
# Check if a file was uploaded
|
| 38 |
file_message = ""
|
| 39 |
if audio_file is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
file_name = os.path.basename(audio_file)
|
| 41 |
file_message = f"\n[Attached file: {file_name}]"
|
| 42 |
|
|
@@ -116,7 +146,7 @@ with gr.Blocks() as demo:
|
|
| 116 |
with gr.Column(scale=1):
|
| 117 |
audio_file = gr.File(
|
| 118 |
label="Upload Audio/Video File",
|
| 119 |
-
file_types=["
|
| 120 |
type="filepath"
|
| 121 |
)
|
| 122 |
|
|
|
|
| 28 |
messages.append({"role": item["role"], "content": item["content"]})
|
| 29 |
return messages
|
| 30 |
|
| 31 |
+
# File validation function
|
| 32 |
+
def validate_file(file):
|
| 33 |
+
"""Validate uploaded file type and size."""
|
| 34 |
+
if file is None:
|
| 35 |
+
return False, "No file uploaded"
|
| 36 |
+
|
| 37 |
+
# Check file size (25MB limit)
|
| 38 |
+
file_size_mb = os.path.getsize(file) / (1024 * 1024)
|
| 39 |
+
if file_size_mb > 25:
|
| 40 |
+
return False, f"File size ({file_size_mb:.1f}MB) exceeds 25MB limit"
|
| 41 |
+
|
| 42 |
+
# Check file extension
|
| 43 |
+
valid_extensions = ['.mp3', '.mp4', '.mpeg', '.mpga', '.m4a', '.wav', '.webm', '.flac', '.ogg', '.aac']
|
| 44 |
+
file_extension = os.path.splitext(file)[1].lower()
|
| 45 |
+
if file_extension not in valid_extensions:
|
| 46 |
+
return False, f"Invalid file type. Supported formats: {', '.join(valid_extensions)}"
|
| 47 |
+
|
| 48 |
+
return True, "File is valid"
|
| 49 |
+
|
| 50 |
# Main chat function (streaming, with tool usage)
|
| 51 |
def chat_with_tools(user_message, history, audio_file):
|
| 52 |
# Add user message
|
|
|
|
| 56 |
# Check if a file was uploaded
|
| 57 |
file_message = ""
|
| 58 |
if audio_file is not None:
|
| 59 |
+
# Validate the file
|
| 60 |
+
is_valid, message = validate_file(audio_file)
|
| 61 |
+
if not is_valid:
|
| 62 |
+
history.append(ChatMessage(
|
| 63 |
+
role="assistant",
|
| 64 |
+
content=message,
|
| 65 |
+
metadata={"title": "❌ Error", "status": "error"}
|
| 66 |
+
))
|
| 67 |
+
yield history
|
| 68 |
+
return
|
| 69 |
+
|
| 70 |
file_name = os.path.basename(audio_file)
|
| 71 |
file_message = f"\n[Attached file: {file_name}]"
|
| 72 |
|
|
|
|
| 146 |
with gr.Column(scale=1):
|
| 147 |
audio_file = gr.File(
|
| 148 |
label="Upload Audio/Video File",
|
| 149 |
+
file_types=[".mp3", ".mp4", ".mpeg", ".mpga", ".m4a", ".wav", ".webm", ".flac", ".ogg", ".aac"],
|
| 150 |
type="filepath"
|
| 151 |
)
|
| 152 |
|