lyimo commited on
Commit
c827f71
·
1 Parent(s): 9ba8b66

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -12
app.py CHANGED
@@ -1,19 +1,50 @@
1
- import gradio as gr
2
  import openai
 
3
 
4
- # Set API Key
5
  openai.api_key = "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"
6
 
7
- # Define a function that will use OpenAI's API to transcribe an audio file
8
- def transcribe_audio(inp):
9
- with open(inp.name, 'rb') as file:
10
- transcript = openai.Audio.transcribe("whisper-1", file)
11
  return transcript["text"]
12
 
13
- # Define the Gradio interface
14
- iface = gr.Interface(fn=transcribe_audio,
15
- inputs=gr.inputs.Audio(source="microphone", type="filepath", label="Record audio"),
16
- outputs="text")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
- # Launch the interface
19
- iface.launch()
 
 
1
  import openai
2
+ import gradio as gr
3
 
 
4
  openai.api_key = "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"
5
 
6
+ def transcribe(audio):
7
+ with open(audio, "rb") as audio_file:
8
+ transcript = openai.Audio.transcribe("whisper-1", audio_file)
 
9
  return transcript["text"]
10
 
11
+ def generate_response(transcribed_text):
12
+ response = openai.Completion.create(
13
+ engine="text-davinci-003",
14
+ prompt=transcribed_text,
15
+ max_tokens=1024,
16
+ n=1,
17
+ stop=None,
18
+ temperature=0.5,
19
+ )
20
+ return response.choices[0].text
21
+
22
+ def run_cmd(command):
23
+ try:
24
+ print(command)
25
+ call(command)
26
+ except KeyboardInterrupt:
27
+ print("Process interrupted")
28
+ sys.exit(1)
29
+
30
+ def inference(text):
31
+ cmd = ['tts', '--text', text]
32
+ run_cmd(cmd)
33
+ return 'tts_output.wav'
34
+
35
+ def process_audio_and_respond(audio):
36
+ text = transcribe(audio)
37
+ response_text = generate_response(text)
38
+ output_file = inference(response_text)
39
+ return output_file
40
+
41
+ demo = gr.Blocks()
42
+
43
+ with demo:
44
+ audio_file = gr.inputs.Audio(source="microphone", type="filepath")
45
+ button = gr.Button("Uliza Swali")
46
+ outputs = gr.outputs.Audio(type="filepath", label="Output Audio")
47
+
48
+ button.click(fn=process_audio_and_respond, inputs=audio_file, outputs=outputs)
49
 
50
+ demo.launch()