Adityadn commited on
Commit
3e5c391
·
verified ·
1 Parent(s): 1b4330c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -19
app.py CHANGED
@@ -74,35 +74,41 @@
74
  import requests
75
  import gradio as gr
76
  import os
77
- import json
78
-
79
- def recognize_audio(url, file):
80
- api_url = os.environ.get('API_URL')
81
- api_token = os.environ.get('API_TOKEN')
82
- data = json.dumps(os.environ.get('DATA'), indent=4)
83
-
84
- if url:
85
- data['url'] = url
86
- result = requests.post(api_url, data=data)
87
- return result.text
88
- elif file:
89
- with open(file.name, 'rb') as f:
90
- files = {'file': (file.name, f)}
91
- result = requests.post(api_url, data=data, files=files)
92
- return result.text
 
93
  else:
94
- return "Please enter a URL or upload an audio file."
 
 
95
 
 
96
  interface = gr.Interface(
97
  fn=recognize_audio,
98
  inputs=[
 
99
  gr.Textbox(label="Enter Audio URL", placeholder="https://example.com/audio.mp3"),
100
- gr.File(label="Upload Audio File")
101
  ],
102
  outputs=gr.Textbox(label="Recognition Result"),
103
  title="Audio Recognition",
104
- description="Upload an audio/video file or enter a URL to identify it."
105
  )
106
 
 
107
  if __name__ == "__main__":
108
  interface.launch()
 
74
  import requests
75
  import gradio as gr
76
  import os
77
+
78
+ # Function to recognize audio from URL or uploaded file
79
+ def recognize_audio(choice, url, file):
80
+ api_url = os.getenv("API_URL")
81
+ api_token = os.getenv("API_TOKEN")
82
+ params = os.getenv("PARAMS")
83
+
84
+ if choice == "URL":
85
+ if not url:
86
+ return "Please enter a valid URL."
87
+ params['url'] = url
88
+ response = requests.post(api_url, data=params)
89
+ elif choice == "Upload File":
90
+ if not file:
91
+ return "Please upload an audio file."
92
+ with open(file.name, "rb") as f:
93
+ response = requests.post(api_url, data=params, files={'file': f})
94
  else:
95
+ return "Please select a method (URL or Upload File)."
96
+
97
+ return response.text
98
 
99
+ # Gradio Interface
100
  interface = gr.Interface(
101
  fn=recognize_audio,
102
  inputs=[
103
+ gr.Radio(["URL", "Upload File"], label="Select Input Method"),
104
  gr.Textbox(label="Enter Audio URL", placeholder="https://example.com/audio.mp3"),
105
+ gr.File(label="Upload Audio File", type="file")
106
  ],
107
  outputs=gr.Textbox(label="Recognition Result"),
108
  title="Audio Recognition",
109
+ description="Choose a method: Upload an audio file or enter a URL to identify the song."
110
  )
111
 
112
+ # Run Gradio App
113
  if __name__ == "__main__":
114
  interface.launch()