janasumit2911 commited on
Commit
d567901
·
verified ·
1 Parent(s): 6d852cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -13
app.py CHANGED
@@ -9,9 +9,9 @@ from scipy.io import wavfile
9
  from pydub import AudioSegment
10
  import gradio as gr
11
 
12
- model = hub.load('Audio_Multiple_v1') #loading model
13
 
14
- def class_names_from_csv(class_map_csv_text): # Function to load class names from CSV.
15
  """Returns list of class names corresponding to score vector."""
16
  class_names = []
17
  with tf.io.gfile.GFile(class_map_csv_text) as csvfile:
@@ -23,19 +23,19 @@ def class_names_from_csv(class_map_csv_text): # Function to load clas
23
  class_map_path = model.class_map_path().numpy()
24
  class_names = class_names_from_csv(class_map_path)
25
 
26
- def ensure_sample_rate(original_sample_rate, waveform, desired_sample_rate=16000): #to ensure/make a standard sample rate for audio
27
  if original_sample_rate != desired_sample_rate: # Resample waveform if required
28
  desired_length = int(round(float(len(waveform)) / original_sample_rate * desired_sample_rate))
29
  waveform = scipy.signal.resample(waveform, desired_length)
30
  return desired_sample_rate, waveform
31
 
32
- def convert_mp3_to_wav(mp3_file_path): #if audio file is mp3 then convert it to wav
33
  audio = AudioSegment.from_mp3(mp3_file_path)
34
  wav_file_path = mp3_file_path.replace('.mp3', '.wav')
35
  audio.export(wav_file_path, format='wav')
36
  return wav_file_path
37
 
38
- def process_audio_file(file_path): #reading audio file and making stereo to mono
39
  sample_rate, wav_data = wavfile.read(file_path, 'rb')
40
  if wav_data.ndim > 1: # Convert stereo to mono if needed
41
  wav_data = np.mean(wav_data, axis=1)
@@ -54,8 +54,7 @@ def process_audio_file(file_path): #reading audio file and making stere
54
  confidence_threshold = 0.60
55
  confident_classes = set()
56
 
57
- #excluding some unwanted classes
58
- exclusion_list = ['Mechanisms','Domestic animals, pets', 'Animal', 'Silence', 'Alarm', 'Wind chime', 'Water', 'Livestock, farm animals, working animals', 'Wild animals', 'Bleat', 'Siren', 'Computer keyboard', 'Toot', 'Shatter', 'Bird','Caw', 'Independent music', 'Tender music', 'Ocean', 'House music', 'Middle Eastern music', 'Swing music', 'Soul music', 'Shofar', 'Motor vehicle (road)', 'White noise','Pink noise', 'Cacophony', 'Sidetone', 'Static', 'Outside, rural or natural', 'Outside, urban or manmade', 'Inside, public space', 'Inside, large room or hall', 'Inside, small room', 'Sound effect' ]
59
  for frame_scores in scores_np:
60
  for i, score in enumerate(frame_scores):
61
  if score > confidence_threshold:
@@ -74,9 +73,9 @@ def process_audio_file(file_path): #reading audio file and making stere
74
  confident_classes.add(class_name)
75
 
76
  confident_classes = sorted(confident_classes)
 
77
  return confident_classes
78
 
79
- #main function to get the input
80
  def process_audio(params):
81
  try:
82
  params = json.loads(params)
@@ -99,7 +98,6 @@ def process_audio(params):
99
  solutions.append(answer_dict)
100
 
101
  result_url = f"{api}/{job_id}"
102
-
103
  # send_results_to_api(solutions, result_url)
104
 
105
  return json.dumps({"solutions": solutions}, indent=4)
@@ -112,8 +110,8 @@ def send_results_to_api(data, result_url):
112
  else:
113
  return {"error": f"Failed to send results to API: {response.status_code}"}
114
 
115
- inputt = gr.Textbox(label="Parameters (JSON format) Eg. audio_files:['',''], api:'', job_id:''")
116
- outputts = gr.JSON()
117
 
118
- application = gr.Interface(fn=process_audio, inputs=inputt, outputs=outputts, title="Audio Classification with API Integration")
119
- application.launch()
 
9
  from pydub import AudioSegment
10
  import gradio as gr
11
 
12
+ model = hub.load('D:\\Databae\\Saved Models\\FINAL MODELS\\Audio Multiple\\Audio_Multiple_v1')
13
 
14
+ def class_names_from_csv(class_map_csv_text):
15
  """Returns list of class names corresponding to score vector."""
16
  class_names = []
17
  with tf.io.gfile.GFile(class_map_csv_text) as csvfile:
 
23
  class_map_path = model.class_map_path().numpy()
24
  class_names = class_names_from_csv(class_map_path)
25
 
26
+ def ensure_sample_rate(original_sample_rate, waveform, desired_sample_rate=16000):
27
  if original_sample_rate != desired_sample_rate: # Resample waveform if required
28
  desired_length = int(round(float(len(waveform)) / original_sample_rate * desired_sample_rate))
29
  waveform = scipy.signal.resample(waveform, desired_length)
30
  return desired_sample_rate, waveform
31
 
32
+ def convert_mp3_to_wav(mp3_file_path):
33
  audio = AudioSegment.from_mp3(mp3_file_path)
34
  wav_file_path = mp3_file_path.replace('.mp3', '.wav')
35
  audio.export(wav_file_path, format='wav')
36
  return wav_file_path
37
 
38
+ def process_audio_file(file_path):
39
  sample_rate, wav_data = wavfile.read(file_path, 'rb')
40
  if wav_data.ndim > 1: # Convert stereo to mono if needed
41
  wav_data = np.mean(wav_data, axis=1)
 
54
  confidence_threshold = 0.60
55
  confident_classes = set()
56
 
57
+ exclusion_list = ['Mechanisms','Domestic animals, pets', 'Animal', 'Silence', 'Alarm', 'Wind chime', 'Water', 'Livestock, farm animals, working animals', 'Wild animals', 'Bleat', 'Siren', 'Computer keyboard', 'Toot', 'Shatter', 'Bird','Caw', 'Independent music', 'Tender music', 'Ocean', 'House music', 'Middle Eastern music', 'Swing music', 'Soul music', 'Shofar', 'Motor vehicle (road)', 'White noise','Pink noise', 'Cacophony', 'Sidetone', 'Static', 'Outside, rural or natural', 'Outside, urban or manmade', 'Inside, public space', 'Inside, large room or hall', 'Inside, small room', 'Sound effect']
 
58
  for frame_scores in scores_np:
59
  for i, score in enumerate(frame_scores):
60
  if score > confidence_threshold:
 
73
  confident_classes.add(class_name)
74
 
75
  confident_classes = sorted(confident_classes)
76
+
77
  return confident_classes
78
 
 
79
  def process_audio(params):
80
  try:
81
  params = json.loads(params)
 
98
  solutions.append(answer_dict)
99
 
100
  result_url = f"{api}/{job_id}"
 
101
  # send_results_to_api(solutions, result_url)
102
 
103
  return json.dumps({"solutions": solutions}, indent=4)
 
110
  else:
111
  return {"error": f"Failed to send results to API: {response.status_code}"}
112
 
113
+ inputt = gr.Textbox(label="Parameters (JSON format) Eg. {'audio_files':['file1.mp3','file2.wav'], 'api':'https://api.example.com', 'job_id':'12345'}")
114
+ outputs = gr.JSON()
115
 
116
+ application = gr.Interface(fn=process_audio, inputs=inputt, outputs=outputs, title="Audio Classification with API Integration")
117
+ application.launch()