Cryptic commited on
Commit
90bcc62
·
1 Parent(s): 0d062b8
Files changed (2) hide show
  1. app.py +16 -22
  2. requirements.txt +4 -4
app.py CHANGED
@@ -5,12 +5,9 @@ import librosa
5
  import numpy as np
6
  import soundfile as sf
7
  import torch
8
- from flask import Flask, request, jsonify
9
  from transformers import pipeline
10
 
11
- # Initialize Flask app
12
- app = Flask(__name__)
13
-
14
  # Load models globally to avoid reloading on every request
15
  device = 0 if torch.cuda.is_available() else -1
16
  models = {
@@ -27,12 +24,12 @@ def load_and_convert_audio(audio_path):
27
  sf.write(temp_wav.name, audio_data, sample_rate, format='WAV')
28
  return temp_wav.name
29
 
30
- def process_audio(audio_path):
31
  """Process audio file and return transcription and summary"""
32
  results = {}
33
 
34
  try:
35
- temp_wav_path = load_and_convert_audio(audio_path)
36
 
37
  # Transcription
38
  transcription = models['transcriber'](temp_wav_path, return_timestamps=True)
@@ -48,7 +45,7 @@ def process_audio(audio_path):
48
  results['summary'] = ' '.join(summaries)
49
 
50
  except Exception as e:
51
- return {'error': str(e)}, 500 # Return error message if something goes wrong
52
 
53
  finally:
54
  if os.path.exists(temp_wav_path):
@@ -56,21 +53,18 @@ def process_audio(audio_path):
56
 
57
  return results
58
 
 
 
 
59
 
60
- @app.route('/process-audio', methods=['POST'])
61
- def process_audio_endpoint():
62
- """API endpoint to process audio file"""
63
- if 'file' not in request.files:
64
- return jsonify({'error': 'No file part'}), 400
65
-
66
- audio_file = request.files['file']
67
- temp_audio_path = os.path.join(tempfile.gettempdir(), audio_file.filename)
68
- audio_file.save(temp_audio_path)
69
-
70
- results = process_audio(temp_audio_path)
71
- os.remove(temp_audio_path) # Clean up the temporary audio file
72
-
73
- return jsonify(results)
74
 
75
  if __name__ == "__main__":
76
- app.run(host='0.0.0.0', port=5000)
 
5
  import numpy as np
6
  import soundfile as sf
7
  import torch
8
+ import gradio as gr
9
  from transformers import pipeline
10
 
 
 
 
11
  # Load models globally to avoid reloading on every request
12
  device = 0 if torch.cuda.is_available() else -1
13
  models = {
 
24
  sf.write(temp_wav.name, audio_data, sample_rate, format='WAV')
25
  return temp_wav.name
26
 
27
+ def process_audio(audio_file):
28
  """Process audio file and return transcription and summary"""
29
  results = {}
30
 
31
  try:
32
+ temp_wav_path = load_and_convert_audio(audio_file.name)
33
 
34
  # Transcription
35
  transcription = models['transcriber'](temp_wav_path, return_timestamps=True)
 
45
  results['summary'] = ' '.join(summaries)
46
 
47
  except Exception as e:
48
+ return {'error': str(e)} # Return error message if something goes wrong
49
 
50
  finally:
51
  if os.path.exists(temp_wav_path):
 
53
 
54
  return results
55
 
56
+ def gradio_interface(audio):
57
+ """Gradio interface function"""
58
+ return process_audio(audio)
59
 
60
+ # Create Gradio interface
61
+ iface = gr.Interface(
62
+ fn=gradio_interface,
63
+ inputs=gr.inputs.Audio(source="upload", type="file", label="Upload Audio File"),
64
+ outputs=["json"],
65
+ title="Audio Transcription and Summarization",
66
+ description="Upload an audio file to get its transcription and summary."
67
+ )
 
 
 
 
 
 
68
 
69
  if __name__ == "__main__":
70
+ iface.launch()
requirements.txt CHANGED
@@ -1,6 +1,6 @@
1
- Flask
2
- transformers
3
- librosa
4
  soundfile
 
5
  numpy
6
- torch
 
1
+ gradio
2
+ torch
 
3
  soundfile
4
+ transformers
5
  numpy
6
+ flask