Adityadn commited on
Commit
03dc344
·
verified ·
1 Parent(s): c48f6b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -3
app.py CHANGED
@@ -81,7 +81,7 @@ def recognize_audio(choice, url, file):
81
  api_url = os.getenv("API_URL", "https://api.audd.io/").strip('"')
82
  params = {
83
  "return": "apple_music,spotify",
84
- "api_token": 'test' # os.getenv("API_TOKEN", "your_api_token_here")
85
  }
86
 
87
  if choice == "URL":
@@ -99,7 +99,44 @@ def recognize_audio(choice, url, file):
99
  else:
100
  return "Please select a method (URL or Upload File)."
101
 
102
- return response.text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
  # Gradio Interface
105
  interface = gr.Interface(
@@ -109,7 +146,7 @@ interface = gr.Interface(
109
  gr.Textbox(label="Enter Audio URL", placeholder="https://example.com/audio.mp3"),
110
  gr.File(label="Upload Audio File", type="filepath") # Menggunakan filepath agar sesuai dengan Gradio
111
  ],
112
- outputs=gr.Textbox(label="Recognition Result"),
113
  title="Audio Recognition",
114
  description="Choose a method: Upload an audio file or enter a URL to identify the song."
115
  )
 
81
  api_url = os.getenv("API_URL", "https://api.audd.io/").strip('"')
82
  params = {
83
  "return": "apple_music,spotify",
84
+ "api_token": os.getenv("API_TOKEN", "your_api_token_here").strip('"')
85
  }
86
 
87
  if choice == "URL":
 
99
  else:
100
  return "Please select a method (URL or Upload File)."
101
 
102
+ # Parse the response into a structured format
103
+ try:
104
+ data = response.json()
105
+
106
+ # Check if the response contains an error
107
+ if result.get("status") == "error":
108
+ error_message = result.get("error", {}).get("error_message", "Unknown error.")
109
+ return f"""
110
+ ### Song recognition failed
111
+ {error_message}
112
+ """
113
+
114
+ result = data.get('result', {})
115
+ artist = result.get('artist', 'Unknown Artist')
116
+ title = result.get('title', 'Unknown Title')
117
+ album = result.get('album', 'Unknown Album')
118
+ release_date = result.get('release_date', 'Unknown Date')
119
+ song_link = result.get('song_link', '')
120
+ apple_music_link = result.get('apple_music', {}).get('url', '')
121
+ spotify_link = result.get('spotify', {}).get('external_urls', {}).get('spotify', '')
122
+
123
+ markdown_output = f"""
124
+ ### Song Recognition Result
125
+ - **Title**: {title}
126
+ - **Artist**: {artist}
127
+ - **Album**: {album}
128
+ - **Release Date**: {release_date}
129
+
130
+ [Listen on Apple Music]({apple_music_link})
131
+ [Listen on Spotify]({spotify_link})
132
+
133
+ #### Song Link:
134
+ [Click here to listen]({song_link})
135
+ """
136
+ return markdown_output
137
+
138
+ except Exception as e:
139
+ return f"Error parsing response: {str(e)}"
140
 
141
  # Gradio Interface
142
  interface = gr.Interface(
 
146
  gr.Textbox(label="Enter Audio URL", placeholder="https://example.com/audio.mp3"),
147
  gr.File(label="Upload Audio File", type="filepath") # Menggunakan filepath agar sesuai dengan Gradio
148
  ],
149
+ outputs=gr.Markdown(label="Recognition Result"),
150
  title="Audio Recognition",
151
  description="Choose a method: Upload an audio file or enter a URL to identify the song."
152
  )