Use gr.Textbox for JSON API to receive raw file paths
Browse files
app.py
CHANGED
|
@@ -210,12 +210,15 @@ def predict_tone_json(audio_file):
|
|
| 210 |
|
| 211 |
try:
|
| 212 |
# Handle different input types from Gradio API
|
| 213 |
-
#
|
| 214 |
-
if isinstance(audio_file,
|
| 215 |
-
# FileData format from API
|
| 216 |
-
file_path = audio_file.get('path', audio_file.get('name', str(audio_file)))
|
| 217 |
-
elif isinstance(audio_file, str):
|
| 218 |
file_path = audio_file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
else:
|
| 220 |
# Try to get path attribute or convert to string
|
| 221 |
file_path = getattr(audio_file, 'path', str(audio_file))
|
|
@@ -432,14 +435,15 @@ with gr.Blocks(css=custom_css, title="Pro-TeVA Tone Recognition") as demo:
|
|
| 432 |
show_label=True
|
| 433 |
)
|
| 434 |
|
| 435 |
-
# JSON API interface - use gr.
|
|
|
|
| 436 |
json_api = gr.Interface(
|
| 437 |
fn=predict_tone_json,
|
| 438 |
-
inputs=gr.
|
| 439 |
outputs=gr.JSON(label="Prediction Result"),
|
| 440 |
api_name="predict_json",
|
| 441 |
title="Pro-TeVA JSON API",
|
| 442 |
-
description="
|
| 443 |
)
|
| 444 |
|
| 445 |
# API Documentation tab
|
|
|
|
| 210 |
|
| 211 |
try:
|
| 212 |
# Handle different input types from Gradio API
|
| 213 |
+
# gr.File returns the file path as a string
|
| 214 |
+
if isinstance(audio_file, str):
|
|
|
|
|
|
|
|
|
|
| 215 |
file_path = audio_file
|
| 216 |
+
elif hasattr(audio_file, 'name'):
|
| 217 |
+
# File-like object with name attribute
|
| 218 |
+
file_path = audio_file.name
|
| 219 |
+
elif isinstance(audio_file, dict):
|
| 220 |
+
# FileData format from API - prefer 'path' over 'name'/'orig_name'
|
| 221 |
+
file_path = audio_file.get('path') or audio_file.get('name', str(audio_file))
|
| 222 |
else:
|
| 223 |
# Try to get path attribute or convert to string
|
| 224 |
file_path = getattr(audio_file, 'path', str(audio_file))
|
|
|
|
| 435 |
show_label=True
|
| 436 |
)
|
| 437 |
|
| 438 |
+
# JSON API interface - use gr.Textbox to receive raw file path from API
|
| 439 |
+
# This avoids Gradio's file component preprocessing issues
|
| 440 |
json_api = gr.Interface(
|
| 441 |
fn=predict_tone_json,
|
| 442 |
+
inputs=gr.Textbox(label="Audio File Path", placeholder="Path to uploaded audio file"),
|
| 443 |
outputs=gr.JSON(label="Prediction Result"),
|
| 444 |
api_name="predict_json",
|
| 445 |
title="Pro-TeVA JSON API",
|
| 446 |
+
description="Upload file first via /gradio_api/upload, then pass the returned path here"
|
| 447 |
)
|
| 448 |
|
| 449 |
# API Documentation tab
|