Adityadn commited on
Commit
f9a0d06
·
verified ·
1 Parent(s): ec42275

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -8
app.py CHANGED
@@ -78,20 +78,25 @@ import json
78
 
79
  # Function to recognize audio from URL or uploaded file
80
  def recognize_audio(choice, url, file):
81
- api_url = os.getenv("API_URL")
82
- api_token = os.getenv("API_TOKEN")
83
- params = json.dumps(os.getenv("PARAMS"), indent=4)
 
 
 
84
 
85
  if choice == "URL":
86
  if not url:
87
  return "Please enter a valid URL."
88
  params['url'] = url
89
  response = requests.post(api_url, data=params)
 
90
  elif choice == "Upload File":
91
- if not file:
92
- return "Please upload an audio file."
93
- with open(file.name, "rb") as f:
94
  response = requests.post(api_url, data=params, files={'file': f})
 
95
  else:
96
  return "Please select a method (URL or Upload File)."
97
 
@@ -103,11 +108,11 @@ interface = gr.Interface(
103
  inputs=[
104
  gr.Radio(["URL", "Upload File"], label="Select Input Method"),
105
  gr.Textbox(label="Enter Audio URL", placeholder="https://example.com/audio.mp3"),
106
- gr.File(label="Upload Audio File", type="binary")
107
  ],
108
  outputs=gr.Textbox(label="Recognition Result"),
109
  title="Audio Recognition",
110
- description="Choose a method: Upload an audio file or enter a URL to identify the song."
111
  )
112
 
113
  # Run Gradio App
 
78
 
79
  # Function to recognize audio from URL or uploaded file
80
  def recognize_audio(choice, url, file):
81
+ api_url = os.getenv("API_URL", "https://api.audd.io/")
82
+ params = (
83
+ json.dumps(
84
+ os.getenv("PARAMS"), indent=4
85
+ )
86
+ )['api_token': os.getenv("API_TOKEN", "your_api_token_here"))
87
 
88
  if choice == "URL":
89
  if not url:
90
  return "Please enter a valid URL."
91
  params['url'] = url
92
  response = requests.post(api_url, data=params)
93
+
94
  elif choice == "Upload File":
95
+ if file:
96
+ return "Please upload a valid File."
97
+ with open(file, "rb") as f:
98
  response = requests.post(api_url, data=params, files={'file': f})
99
+
100
  else:
101
  return "Please select a method (URL or Upload File)."
102
 
 
108
  inputs=[
109
  gr.Radio(["URL", "Upload File"], label="Select Input Method"),
110
  gr.Textbox(label="Enter Audio URL", placeholder="https://example.com/audio.mp3"),
111
+ gr.File(label="Upload Audio File", type="filepath") # Menggunakan filepath agar sesuai dengan Gradio
112
  ],
113
  outputs=gr.Textbox(label="Recognition Result"),
114
  title="Audio Recognition",
115
+ description="Choose a method: Upload an audio file or enter a URL to identify the song. If no file is uploaded, a default audio file will be used."
116
  )
117
 
118
  # Run Gradio App