MufinApps commited on
Commit
8f5c3df
·
1 Parent(s): 3f749e3

added noise cancellation

Browse files
Files changed (2) hide show
  1. app.py +31 -8
  2. requirements.txt +3 -0
app.py CHANGED
@@ -78,8 +78,29 @@ language_info={
78
  'Other': 'Other'
79
  }
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  def translate(audio_file,lan):
82
  message=""
 
83
  with open(audio_file, 'rb') as f:
84
  result = openai.Audio.translate("whisper-1", f)
85
  text=result.text
@@ -96,6 +117,7 @@ def translate(audio_file,lan):
96
 
97
  def transcription(audio_file,input_lang):
98
  global language_info
 
99
  with open(audio_file, 'rb') as f:
100
  if input_lang=="Other":
101
  result = openai.Audio.transcribe("whisper-1", f)
@@ -104,7 +126,9 @@ def transcription(audio_file,input_lang):
104
  result = openai.Audio.transcribe("whisper-1", f,language=language_info[input_lang])
105
  th.current_thread().return_value=result.text
106
 
107
-
 
 
108
 
109
  def func(audio_file,input_lang,lan,state="",state1=""):
110
 
@@ -127,8 +151,7 @@ def func(audio_file,input_lang,lan,state="",state1=""):
127
  return state,state1
128
 
129
 
130
- def gpt_api(text):
131
-
132
  if text=="":
133
  return ""
134
  if len(text)>2000:
@@ -136,7 +159,7 @@ def gpt_api(text):
136
  completion = openai.ChatCompletion.create(
137
  model="gpt-3.5-turbo",
138
  messages=[
139
- {"role": "system", "content": "your task is to make a concise summery of text in the same language used in given text"},
140
  {"role": "user", "content":text}
141
  ]
142
  )
@@ -145,9 +168,9 @@ def gpt_api(text):
145
  th.current_thread().return_value=message
146
 
147
 
148
- def make_summery(text,text1):
149
- t1=th.Thread(target=gpt_api, args=(text,))
150
- t2=th.Thread(target=gpt_api, args=(text,))
151
  t1.start()
152
  t2.start()
153
  t1.join()
@@ -203,7 +226,7 @@ with gr.Blocks(theme=gr.themes.Soft(),css=css) as app:
203
  sumer_tr=gr.Textbox(label="Summery of Translation",interactive=False,lines=4,max_lines=4)
204
 
205
  # pass
206
- summery.click(make_summery,[text,text1],[sumer_ts,sumer_tr],cancels=[st],queue=False)
207
  clear_output.click(clear_output_data,[],[text,text1,sumer_tr,sumer_ts],cancels=[st],queue=False)
208
  # gr.update(visible=True)
209
 
 
78
  'Other': 'Other'
79
  }
80
 
81
+ import librosa
82
+ import noisereduce as nr
83
+ from pydub import AudioSegment
84
+
85
+ def enhance_and_denoise_audio(file_path):
86
+ # Load the audio file
87
+ data, rate = librosa.load(file_path, sr=None)
88
+
89
+ # Perform noise reduction
90
+ reduced_noise_audio = nr.reduce_noise(y=data, sr=rate)
91
+
92
+ # Increase the volume by 10 dB (using PyDub)
93
+ audio_segment = AudioSegment(reduced_noise_audio.tobytes(), frame_rate=rate, sample_width=reduced_noise_audio.dtype.itemsize, channels=1)
94
+ enhanced_audio = audio_segment + 10
95
+
96
+ # Export the enhanced audio to the same file
97
+ enhanced_audio.export(file_path, format="wav")
98
+
99
+ return file_path
100
+
101
  def translate(audio_file,lan):
102
  message=""
103
+ audio_file=enhance_and_denoise_audio(audio_file)
104
  with open(audio_file, 'rb') as f:
105
  result = openai.Audio.translate("whisper-1", f)
106
  text=result.text
 
117
 
118
  def transcription(audio_file,input_lang):
119
  global language_info
120
+ audio_file=enhance_and_denoise_audio(audio_file)
121
  with open(audio_file, 'rb') as f:
122
  if input_lang=="Other":
123
  result = openai.Audio.transcribe("whisper-1", f)
 
126
  result = openai.Audio.transcribe("whisper-1", f,language=language_info[input_lang])
127
  th.current_thread().return_value=result.text
128
 
129
+
130
+
131
+
132
 
133
  def func(audio_file,input_lang,lan,state="",state1=""):
134
 
 
151
  return state,state1
152
 
153
 
154
+ def gpt_api(text,language):
 
155
  if text=="":
156
  return ""
157
  if len(text)>2000:
 
159
  completion = openai.ChatCompletion.create(
160
  model="gpt-3.5-turbo",
161
  messages=[
162
+ {"role": "system", "content": f"your task is to make a concise summery and useful summery from the given text in {language}."},
163
  {"role": "user", "content":text}
164
  ]
165
  )
 
168
  th.current_thread().return_value=message
169
 
170
 
171
+ def make_summery(text,text1,input_lan,output_lan):
172
+ t1=th.Thread(target=gpt_api, args=(text,input_lan,))
173
+ t2=th.Thread(target=gpt_api, args=(text1,output_lan,))
174
  t1.start()
175
  t2.start()
176
  t1.join()
 
226
  sumer_tr=gr.Textbox(label="Summery of Translation",interactive=False,lines=4,max_lines=4)
227
 
228
  # pass
229
+ summery.click(make_summery,[text,text1,input_lan,lan],[sumer_ts,sumer_tr],cancels=[st],queue=False)
230
  clear_output.click(clear_output_data,[],[text,text1,sumer_tr,sumer_ts],cancels=[st],queue=False)
231
  # gr.update(visible=True)
232
 
requirements.txt CHANGED
@@ -70,3 +70,6 @@ urllib3==2.0.7
70
  uvicorn==0.24.0.post1
71
  websockets==11.0.3
72
  yarl==1.9.2
 
 
 
 
70
  uvicorn==0.24.0.post1
71
  websockets==11.0.3
72
  yarl==1.9.2
73
+ noisereduce==3.0.0
74
+ librosa==0.10.1
75
+ pydub==0.25.1