janasumit2911 commited on
Commit
6b5077f
·
verified ·
1 Parent(s): 980baa8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -437
app.py CHANGED
@@ -1,139 +1,3 @@
1
- #1
2
- # import os
3
- # import tensorflow as tf
4
- # import tensorflow_hub as hub
5
- # import numpy as np
6
- # import csv
7
- # import requests
8
- # import json
9
- # import scipy
10
- # from scipy.io import wavfile
11
- # from pydub import AudioSegment
12
- # import gradio as gr
13
- # import io
14
- # from io import BytesIO
15
- # import soundfile as sf
16
-
17
- # # Load the model
18
- # model = hub.load('Audio_Multiple_v1')
19
- # solutions=[]
20
- # def class_names_from_csv(class_map_csv_text):
21
- # """Returns list of class names corresponding to score vector."""
22
- # class_names = []
23
- # with tf.io.gfile.GFile(class_map_csv_text) as csvfile:
24
- # reader = csv.DictReader(csvfile)
25
- # for row in reader:
26
- # class_names.append(row['display_name'])
27
- # return class_names
28
-
29
- # class_map_path = model.class_map_path().numpy()
30
- # class_names = class_names_from_csv(class_map_path)
31
-
32
- # def ensure_sample_rate(original_sample_rate, waveform, desired_sample_rate=16000):
33
- # if original_sample_rate != desired_sample_rate: # Resample waveform if required
34
- # desired_length = int(round(float(len(waveform)) / original_sample_rate * desired_sample_rate))
35
- # waveform = scipy.signal.resample(waveform, desired_length)
36
- # return desired_sample_rate, waveform
37
-
38
- # def convert_mp3_to_wav(mp3_data):
39
- # audio = AudioSegment.from_file(io.BytesIO(mp3_data), format="mp3")
40
- # wav_buffer = io.BytesIO()
41
- # audio.export(wav_buffer, format='wav')
42
- # wav_buffer.seek(0)
43
- # return wav_buffer
44
-
45
- # def process_audio_file(file_data, url):
46
- # sample_rate, wav_data = wavfile.read(BytesIO(file_data))
47
-
48
- # if wav_data.ndim > 1:
49
- # wav_data = np.mean(wav_data, axis=1)
50
- # sample_rate, wav_data = ensure_sample_rate(sample_rate, wav_data)
51
-
52
- # waveform = wav_data / tf.int16.max
53
-
54
- # scores, embeddings, spectrogram = model(waveform)
55
-
56
- # scores_np = scores.numpy()
57
- # mean_scores = np.mean(scores, axis=0)
58
-
59
- # inferred_class = class_names[mean_scores.argmax()]
60
-
61
- # confidence_threshold = 0.60
62
- # confident_classes = set()
63
-
64
-
65
- # 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' ]
66
- # for frame_scores in scores_np:
67
- # for i, score in enumerate(frame_scores):
68
- # if score > confidence_threshold:
69
- # class_name = class_names[i]
70
-
71
- # if class_name =='Child speech, kid speaking':
72
- # class_name='Child speech'
73
- # elif class_name =='Vehicle horn, car horn, honking':
74
- # class_name='Vehicle horn'
75
- # elif class_name =='Railroad car, train wagon':
76
- # class_name='Train/wagon'
77
- # elif class_name=='Rail transport':
78
- # class_name='Train/wagon'
79
-
80
- # if class_name not in exclusion_list:
81
- # confident_classes.add(class_name)
82
-
83
- # confident_classes = sorted(confident_classes)
84
-
85
- # answer_dict= {}
86
- # answer_dict.update({'file_name': url, 'class_names': confident_classes}) #os.path.basename(file_path
87
- # solutions.append(answer_dict)
88
-
89
- # def get_audio_data(url):
90
- # response = requests.get(url)
91
- # response.raise_for_status()
92
- # return response.content
93
-
94
- # def process_audio(params):
95
- # try:
96
- # params = json.loads(params)
97
- # except json.JSONDecodeError as e:
98
- # return {"error": f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}"}
99
-
100
- # audio_files = params.get("audio_files", [])
101
- # api = params.get("api", "")
102
- # job_id = params.get("job_id", "")
103
-
104
- # solutions = []
105
- # for audio_url in audio_files:
106
- # audio_data = get_audio_data(audio_url)
107
-
108
- # if audio_url.endswith(".mp3"):
109
- # wav_buffer = convert_mp3_to_wav(audio_data)
110
- # process_audio_file(wav_buffer.getvalue(), audio_url)
111
-
112
- # elif audio_url.endswith(".wav"):
113
- # process_audio_file(audio_data, audio_url)
114
-
115
- # result_url = f"{api}/{job_id}"
116
- # response = requests.patch(result_url, json={"solutions": solutions})
117
-
118
- # return json.dumps({"solutions": solutions}, indent=4)
119
-
120
- # def send_results_to_api(data, result_url):
121
- # headers = {"Content-Type": "application/json"}
122
- # response = requests.patch(result_url, json=data, headers=headers)
123
- # if response.status_code == 200:
124
- # return response.json() # Return any response from the API if needed
125
- # else:
126
- # return {"error": f"Failed to send results to API: {response.status_code}"}
127
-
128
- # inputt = gr.Textbox(label="Parameters (JSON format) Eg. {'audio_files':['file1.mp3','file2.wav'], 'api':'https://api.example.com', 'job_id':'12345'}")
129
- # outputs = gr.JSON()
130
-
131
- # application = gr.Interface(fn=process_audio, inputs=inputt, outputs=outputs, title="Audio Classification with API Integration")
132
- # application.launch()
133
-
134
-
135
-
136
-
137
  import os
138
  import tensorflow as tf
139
  import tensorflow_hub as hub
@@ -264,304 +128,3 @@ outputs = gr.JSON()
264
 
265
  application = gr.Interface(fn=process_audio, inputs=inputt, outputs=outputs, title="Audio Classification with API Integration")
266
  application.launch()
267
-
268
-
269
-
270
-
271
-
272
-
273
-
274
-
275
- # import os
276
- # import tensorflow as tf
277
- # import tensorflow_hub as hub
278
- # import numpy as np
279
- # import csv
280
- # import requests
281
- # import json
282
- # import scipy
283
- # from scipy.io import wavfile
284
- # from pydub import AudioSegment
285
- # import gradio as gr
286
- # import io
287
- # from io import BytesIO
288
- # import soundfile as sf
289
- # import warnings
290
- # import logging
291
-
292
- # # Suppress specific warnings
293
- # warnings.filterwarnings("ignore", category=scipy.io.wavfile.WavFileWarning)
294
-
295
- # # Configure logging
296
- # logging.basicConfig(level=logging.INFO) # Set logging level as needed
297
-
298
- # # Load the model
299
- # model = hub.load('https://tfhub.dev/google/yamnet/1')
300
-
301
- # def class_names_from_csv(class_map_csv_text):
302
- # """Returns list of class names corresponding to score vector."""
303
- # class_names = []
304
- # with tf.io.gfile.GFile(class_map_csv_text) as csvfile:
305
- # reader = csv.DictReader(csvfile)
306
- # for row in reader:
307
- # class_names.append(row['display_name'])
308
- # return class_names
309
-
310
- # class_map_path = model.class_map_path().numpy()
311
- # class_names = class_names_from_csv(class_map_path)
312
-
313
- # def ensure_sample_rate(original_sample_rate, waveform, desired_sample_rate=16000):
314
- # if original_sample_rate != desired_sample_rate: # Resample waveform if required
315
- # desired_length = int(round(float(len(waveform)) / original_sample_rate * desired_sample_rate))
316
- # waveform = scipy.signal.resample(waveform, desired_length)
317
- # return desired_sample_rate, waveform
318
-
319
- # def convert_mp3_to_wav(mp3_data):
320
- # audio = AudioSegment.from_file(io.BytesIO(mp3_data), format="mp3")
321
- # wav_buffer = io.BytesIO()
322
- # audio.export(wav_buffer, format='wav')
323
- # wav_buffer.seek(0)
324
- # return wav_buffer
325
-
326
- # def process_audio_file(file_data, url):
327
- # try:
328
- # sample_rate, wav_data = wavfile.read(BytesIO(file_data))
329
- # except Exception as e:
330
- # logging.error(f"Error reading WAV file from {url}: {e}")
331
- # return
332
-
333
- # if wav_data.ndim > 1:
334
- # wav_data = np.mean(wav_data, axis=1)
335
- # sample_rate, wav_data = ensure_sample_rate(sample_rate, wav_data)
336
-
337
- # waveform = wav_data / tf.int16.max
338
-
339
- # scores, embeddings, spectrogram = model(waveform)
340
-
341
- # scores_np = scores.numpy()
342
- # mean_scores = np.mean(scores, axis=0)
343
-
344
- # inferred_class = class_names[mean_scores.argmax()]
345
-
346
- # confidence_threshold = 0.60
347
- # confident_classes = set()
348
-
349
- # 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']
350
- # for frame_scores in scores_np:
351
- # for i, score in enumerate(frame_scores):
352
- # if score > confidence_threshold:
353
- # class_name = class_names[i]
354
-
355
- # if class_name =='Child speech, kid speaking':
356
- # class_name='Child speech'
357
- # elif class_name =='Vehicle horn, car horn, honking':
358
- # class_name='Vehicle horn'
359
- # elif class_name =='Railroad car, train wagon':
360
- # class_name='Train/wagon'
361
- # elif class_name=='Rail transport':
362
- # class_name='Train/wagon'
363
-
364
- # if class_name not in exclusion_list:
365
- # confident_classes.add(class_name)
366
-
367
- # confident_classes = sorted(confident_classes)
368
-
369
- # answer_dict= {}
370
- # answer_dict.update({'file_name': url, 'class_names': confident_classes}) #os.path.basename(file_path
371
- # solutions.append(answer_dict)
372
- # return solutions
373
-
374
- # def get_audio_data(url):
375
- # response = requests.get(url)
376
- # response.raise_for_status()
377
- # return response.content
378
-
379
- # def process_audio(params):
380
- # try:
381
- # params = json.loads(params)
382
- # except json.JSONDecodeError as e:
383
- # return {"error": f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}"}
384
-
385
- # audio_files = params.get("audio_files", [])
386
- # api = params.get("api", "")
387
- # job_id = params.get("job_id", "")
388
-
389
- # solutions = []
390
- # for audio_url in audio_files:
391
- # try:
392
- # audio_data = get_audio_data(audio_url)
393
-
394
- # if audio_url.endswith(".mp3"):
395
- # wav_buffer = convert_mp3_to_wav(audio_data)
396
- # process_audio_file(wav_buffer.getvalue(), audio_url)
397
-
398
- # elif audio_url.endswith(".wav"):
399
- # process_audio_file(audio_data, audio_url)
400
- # except Exception as e:
401
- # logging.error(f"Error processing {audio_url}: {e}")
402
-
403
- # result_url = f"{api}/{job_id}"
404
- # response = requests.patch(result_url, json={"solutions": solutions})
405
-
406
- # return json.dumps({"solutions": solutions}, indent=4)
407
-
408
- # def send_results_to_api(data, result_url):
409
- # headers = {"Content-Type": "application/json"}
410
- # response = requests.patch(result_url, json=data, headers=headers)
411
- # if response.status_code == 200:
412
- # return response.json() # Return any response from the API if needed
413
- # else:
414
- # return {"error": f"Failed to send results to API: {response.status_code}"}
415
-
416
- # inputt = gr.Textbox(label="Parameters (JSON format) Eg. {'audio_files':['file1.mp3','file2.wav'], 'api':'https://api.example.com', 'job_id':'12345'}")
417
- # outputs = gr.JSON()
418
-
419
- # application = gr.Interface(fn=process_audio, inputs=inputt, outputs=outputs, title="Audio Classification with API Integration")
420
- # application.launch()
421
-
422
-
423
-
424
-
425
-
426
-
427
-
428
-
429
- # import os
430
- # import tensorflow as tf
431
- # import tensorflow_hub as hub
432
- # import numpy as np
433
- # import csv
434
- # import requests
435
- # import json
436
- # from scipy.io import wavfile
437
- # import gradio as gr
438
- # import io
439
- # from io import BytesIO
440
- # import soundfile as sf
441
- # import scipy
442
- # import ffmpeg
443
-
444
- # # Load the model
445
- # model = hub.load('https://tfhub.dev/google/yamnet/1')
446
-
447
- # def class_names_from_csv(class_map_csv_text):
448
- # """Returns list of class names corresponding to score vector."""
449
- # class_names = []
450
- # with tf.io.gfile.GFile(class_map_csv_text) as csvfile:
451
- # reader = csv.DictReader(csvfile)
452
- # for row in reader:
453
- # class_names.append(row['display_name'])
454
- # return class_names
455
-
456
- # class_map_path = model.class_map_path().numpy()
457
- # class_names = class_names_from_csv(class_map_path)
458
-
459
- # def ensure_sample_rate(original_sample_rate, waveform, desired_sample_rate=16000):
460
- # if original_sample_rate != desired_sample_rate: # Resample waveform if required
461
- # desired_length = int(round(float(len(waveform)) / original_sample_rate * desired_sample_rate))
462
- # waveform = scipy.signal.resample(waveform, desired_length)
463
- # return desired_sample_rate, waveform
464
-
465
- # def convert_mp3_to_wav(mp3_data):
466
- # mp3_buffer = BytesIO(mp3_data)
467
- # wav_buffer = BytesIO()
468
- # process = (
469
- # ffmpeg
470
- # .input('pipe:0', format='mp3')
471
- # .output('pipe:1', format='wav')
472
- # .run_async(pipe_stdin=True, pipe_stdout=True, pipe_stderr=True)
473
- # )
474
- # out, err = process.communicate(input=mp3_buffer.read())
475
- # if process.returncode != 0:
476
- # raise RuntimeError(f"ffmpeg process failed with error: {err}")
477
- # wav_buffer.write(out)
478
- # wav_buffer.seek(0)
479
- # return wav_buffer
480
-
481
- # def process_audio_file(file_data, url):
482
- # sample_rate, wav_data = wavfile.read(BytesIO(file_data))
483
-
484
- # if wav_data.ndim > 1:
485
- # wav_data = np.mean(wav_data, axis=1)
486
- # sample_rate, wav_data = ensure_sample_rate(sample_rate, wav_data)
487
-
488
- # waveform = wav_data / tf.int16.max
489
-
490
- # scores, embeddings, spectrogram = model(waveform)
491
-
492
- # scores_np = scores.numpy()
493
- # mean_scores = np.mean(scores, axis=0)
494
-
495
- # inferred_class = class_names[mean_scores.argmax()]
496
-
497
- # confidence_threshold = 0.60
498
- # confident_classes = set()
499
- # solutions=[]
500
- # 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' ]
501
- # for frame_scores in scores_np:
502
- # for i, score in enumerate(frame_scores):
503
- # if score > confidence_threshold:
504
- # class_name = class_names[i]
505
-
506
- # if class_name =='Child speech, kid speaking':
507
- # class_name='Child speech'
508
- # elif class_name =='Vehicle horn, car horn, honking':
509
- # class_name='Vehicle horn'
510
- # elif class_name =='Railroad car, train wagon':
511
- # class_name='Train/wagon'
512
- # elif class_name=='Rail transport':
513
- # class_name='Train/wagon'
514
-
515
- # if class_name not in exclusion_list:
516
- # confident_classes.add(class_name)
517
-
518
- # confident_classes = sorted(confident_classes)
519
-
520
- # answer_dict= {}
521
- # answer_dict.update({'file_name': url, 'class_names': confident_classes}) #os.path.basename(file_path
522
- # solutions.append(answer_dict)
523
-
524
- # def get_audio_data(url):
525
- # response = requests.get(url)
526
- # response.raise_for_status()
527
- # return response.content
528
-
529
- # def process_audio(params):
530
- # try:
531
- # params = json.loads(params)
532
- # except json.JSONDecodeError as e:
533
- # return {"error": f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}"}
534
-
535
- # audio_files = params.get("audio_files", [])
536
- # api = params.get("api", "")
537
- # job_id = params.get("job_id", "")
538
-
539
- # solutions = []
540
- # for audio_url in audio_files:
541
- # audio_data = get_audio_data(audio_url)
542
-
543
- # if audio_url.endswith(".mp3"):
544
- # wav_buffer = convert_mp3_to_wav(audio_data)
545
- # process_audio_file(wav_buffer.getvalue(), audio_url)
546
-
547
- # elif audio_url.endswith(".wav"):
548
- # process_audio_file(audio_data, audio_url)
549
-
550
- # result_url = f"{api}/{job_id}"
551
- # response = requests.patch(result_url, json={"solutions": solutions})
552
-
553
- # return json.dumps({"solutions": solutions}, indent=4)
554
-
555
- # def send_results_to_api(data, result_url):
556
- # headers = {"Content-Type": "application/json"}
557
- # response = requests.patch(result_url, json=data, headers=headers)
558
- # if response.status_code == 200:
559
- # return response.json() # Return any response from the API if needed
560
- # else:
561
- # return {"error": f"Failed to send results to API: {response.status_code}"}
562
-
563
- # inputt = gr.Textbox(label="Parameters (JSON format) Eg. {'audio_files':['file1.mp3','file2.wav'], 'api':'https://api.example.com', 'job_id':'12345'}")
564
- # outputs = gr.JSON()
565
-
566
- # application = gr.Interface(fn=process_audio, inputs=inputt, outputs=outputs, title="Audio Classification with API Integration")
567
- # application.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import tensorflow as tf
3
  import tensorflow_hub as hub
 
128
 
129
  application = gr.Interface(fn=process_audio, inputs=inputt, outputs=outputs, title="Audio Classification with API Integration")
130
  application.launch()