Spaces:
Sleeping
Sleeping
Manuel Zafra
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,8 @@ import os
|
|
6 |
import json
|
7 |
import gradio as gr
|
8 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
|
9 |
|
10 |
# File to save the history of recognized songs
|
11 |
HISTORY_FILE = "song_history.json"
|
@@ -373,7 +375,7 @@ with gr.Blocks(title="Music Recognition & Fun Facts", css=".large-text {font-siz
|
|
373 |
# Artist info section
|
374 |
with gr.Row():
|
375 |
artist_info_text = gr.Markdown("")
|
376 |
-
artist_info_image = gr.Image(type="url"
|
377 |
|
378 |
gr.Markdown(value=lambda: MESSAGES[selected_language.value]["chat_title"])
|
379 |
chat_history = gr.Chatbot(label="Music Chat")
|
@@ -399,6 +401,22 @@ with gr.Blocks(title="Music Recognition & Fun Facts", css=".large-text {font-siz
|
|
399 |
else:
|
400 |
return "no_audio", MESSAGES[lang]["no_audio"]
|
401 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
402 |
# Recognition function
|
403 |
def process_audio(audio_path, language_name, status):
|
404 |
lang = LANGUAGES.get(language_name, "en")
|
@@ -450,10 +468,13 @@ with gr.Blocks(title="Music Recognition & Fun Facts", css=".large-text {font-siz
|
|
450 |
# Artist info section title based on language
|
451 |
artist_facts_content = f"{MESSAGES[lang]['artist_info_title']}\n\n{artist_info['text']}"
|
452 |
|
|
|
|
|
|
|
453 |
# Update status message
|
454 |
status_msg = MESSAGES[lang]["recognized"]
|
455 |
|
456 |
-
return result, recognition_msg, artist_facts_content, status_msg,
|
457 |
|
458 |
except Exception as e:
|
459 |
return None, "", "", MESSAGES[lang]["error"].format(error=str(e)), ""
|
|
|
6 |
import json
|
7 |
import gradio as gr
|
8 |
from tools.final_answer import FinalAnswerTool
|
9 |
+
import tempfile
|
10 |
+
import shutil
|
11 |
|
12 |
# File to save the history of recognized songs
|
13 |
HISTORY_FILE = "song_history.json"
|
|
|
375 |
# Artist info section
|
376 |
with gr.Row():
|
377 |
artist_info_text = gr.Markdown("")
|
378 |
+
artist_info_image = gr.Image(type="filepath") # Changed from type="url" to type="filepath"
|
379 |
|
380 |
gr.Markdown(value=lambda: MESSAGES[selected_language.value]["chat_title"])
|
381 |
chat_history = gr.Chatbot(label="Music Chat")
|
|
|
401 |
else:
|
402 |
return "no_audio", MESSAGES[lang]["no_audio"]
|
403 |
|
404 |
+
# Function to download image from URL and return a local filepath
|
405 |
+
def download_image(url):
|
406 |
+
if not url:
|
407 |
+
return None
|
408 |
+
try:
|
409 |
+
response = requests.get(url, stream=True)
|
410 |
+
response.raise_for_status()
|
411 |
+
# Create a temporary file
|
412 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.jpg')
|
413 |
+
shutil.copyfileobj(response.raw, temp_file)
|
414 |
+
temp_file.close()
|
415 |
+
return temp_file.name
|
416 |
+
except Exception as e:
|
417 |
+
print(f"Error downloading image: {str(e)}")
|
418 |
+
return None
|
419 |
+
|
420 |
# Recognition function
|
421 |
def process_audio(audio_path, language_name, status):
|
422 |
lang = LANGUAGES.get(language_name, "en")
|
|
|
468 |
# Artist info section title based on language
|
469 |
artist_facts_content = f"{MESSAGES[lang]['artist_info_title']}\n\n{artist_info['text']}"
|
470 |
|
471 |
+
# Download the image if URL exists
|
472 |
+
image_filepath = download_image(artist_info['image']) if artist_info['image'] else ""
|
473 |
+
|
474 |
# Update status message
|
475 |
status_msg = MESSAGES[lang]["recognized"]
|
476 |
|
477 |
+
return result, recognition_msg, artist_facts_content, status_msg, image_filepath
|
478 |
|
479 |
except Exception as e:
|
480 |
return None, "", "", MESSAGES[lang]["error"].format(error=str(e)), ""
|