janasumit2911 commited on
Commit
860f2af
·
verified ·
1 Parent(s): 954274c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -23
app.py CHANGED
@@ -12,6 +12,7 @@ from pydub import AudioSegment
12
  import io
13
  from io import BytesIO
14
 
 
15
  # Load the model
16
  model = hub.load('Audio_Multiple_v1')
17
 
@@ -61,7 +62,7 @@ def process_audio_file(file_data, url):
61
  if inferred_class == "Silence" and len(top_two_indices) > 1:
62
  inferred_class = class_names[top_two_indices[1]]
63
 
64
- answer_dict = {'audio_url': url, 'answer': [inferred_class]}
65
  return answer_dict
66
  except Exception as e:
67
  logging.error(f"Error processing {url}: {e}")
@@ -72,21 +73,21 @@ def get_audio_data(url):
72
  response.raise_for_status()
73
  return response.content
74
 
75
- def send_results_to_api(data, result_url):
76
- headers = {"Content-Type": "application/json"}
77
- try:
78
- response = requests.post(result_url, json=data, headers=headers)
79
- response.raise_for_status() # Raise error for non-200 responses
80
- return response.json() # Return any JSON response from the API
81
- except requests.exceptions.HTTPError as http_err:
82
- logging.error(f"HTTP error occurred: {http_err}")
83
- return {"error": f"HTTP error occurred: {http_err}"}
84
- except requests.exceptions.RequestException as req_err:
85
- logging.error(f"Request error occurred: {req_err}")
86
- return {"error": f"Request error occurred: {req_err}"}
87
- except ValueError as val_err:
88
- logging.error(f"Error decoding JSON response: {val_err}")
89
- return {"error": f"Error decoding JSON response: {val_err}"}
90
 
91
  def process_audio(params):
92
  try:
@@ -95,8 +96,8 @@ def process_audio(params):
95
  return {"error": f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}"}
96
 
97
  audio_files = params.get("audio_files", [])
98
- api = params.get("api", "")
99
- job_id = params.get("job_id", "")
100
 
101
  solutions = []
102
  for audio_url in audio_files:
@@ -112,15 +113,15 @@ def process_audio(params):
112
  if result:
113
  solutions.append(result)
114
 
115
- result_url = f"{api}/{job_id}"
116
- send_results_to_api(solutions, result_url)
117
 
118
- return json.dumps({"solutions": solutions}, indent=4)
119
 
120
  import gradio as gr
121
 
122
- inputt = gr.Textbox(label="Parameters (JSON format) Eg. {'audio_files':['file1.mp3','file2.wav'], 'api':'https://api.example.com', 'job_id':'12345'}")
123
  outputs = gr.JSON()
124
 
125
  application = gr.Interface(fn=process_audio, inputs=inputt, outputs=outputs, title="Audio Classification with API Integration")
126
- application.launch(share=True)
 
12
  import io
13
  from io import BytesIO
14
 
15
+
16
  # Load the model
17
  model = hub.load('Audio_Multiple_v1')
18
 
 
62
  if inferred_class == "Silence" and len(top_two_indices) > 1:
63
  inferred_class = class_names[top_two_indices[1]]
64
 
65
+ answer_dict = {'url': url, 'answer': [inferred_class]}
66
  return answer_dict
67
  except Exception as e:
68
  logging.error(f"Error processing {url}: {e}")
 
73
  response.raise_for_status()
74
  return response.content
75
 
76
+ # def send_results_to_api(data, result_url):
77
+ # headers = {"Content-Type": "application/json"}
78
+ # try:
79
+ # response = requests.post(result_url, json=data, headers=headers)
80
+ # response.raise_for_status() # Raise error for non-200 responses
81
+ # return response.json() # Return any JSON response from the API
82
+ # except requests.exceptions.HTTPError as http_err:
83
+ # logging.error(f"HTTP error occurred: {http_err}")
84
+ # return {"error": f"HTTP error occurred: {http_err}"}
85
+ # except requests.exceptions.RequestException as req_err:
86
+ # logging.error(f"Request error occurred: {req_err}")
87
+ # return {"error": f"Request error occurred: {req_err}"}
88
+ # except ValueError as val_err:
89
+ # logging.error(f"Error decoding JSON response: {val_err}")
90
+ # return {"error": f"Error decoding JSON response: {val_err}"}
91
 
92
  def process_audio(params):
93
  try:
 
96
  return {"error": f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}"}
97
 
98
  audio_files = params.get("audio_files", [])
99
+ # api = params.get("api", "")
100
+ # job_id = params.get("job_id", "")
101
 
102
  solutions = []
103
  for audio_url in audio_files:
 
113
  if result:
114
  solutions.append(result)
115
 
116
+ # result_url = f"{api}/{job_id}"
117
+ # send_results_to_api(solutions, result_url)
118
 
119
+ return json.dumps({"solutions": solutions})
120
 
121
  import gradio as gr
122
 
123
+ inputt = gr.Textbox(label="Parameters (JSON format) Eg. {'audio_files':['file1.mp3','file2.wav']}")
124
  outputs = gr.JSON()
125
 
126
  application = gr.Interface(fn=process_audio, inputs=inputt, outputs=outputs, title="Audio Classification with API Integration")
127
+ application.launch()