Sreekumar1608 commited on
Commit
4618ee7
·
1 Parent(s): 9234e22

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+ import winsound
4
+ from elevenlabslib import *
5
+ from pydub import AudioSegment
6
+ from pydub.playback import play
7
+ import io
8
+ import config
9
+
10
+ openai.api_key = config.OPENAI_API_KEY
11
+ api_key = config.ELEVENLABS_API_KEY
12
+ from elevenlabslib import ElevenLabsUser
13
+ user = ElevenLabsUser(api_key)
14
+
15
+ messages = ["You are an advisor. Please respond to all input in 50 words or less."]
16
+
17
+ def transcribe(audio):
18
+ global messages
19
+
20
+ audio_file = open(audio, "rb")
21
+ transcript = openai.Audio.transcribe("whisper-1", audio_file)
22
+
23
+ messages.append(f"\nUser: {transcript['text']}")
24
+
25
+ response = openai.Completion.create(
26
+ engine="text-davinci-003",
27
+ prompt=messages[-1],
28
+ max_tokens=80,
29
+ n=1,
30
+ stop=None,
31
+ temperature=0.5,
32
+ )
33
+
34
+ system_message = response["choices"][0]["text"]
35
+ messages.append(f"{system_message}")
36
+
37
+ voice = user.get_voices_by_name("Antoni")[0]
38
+ audio = voice.generate_audio_bytes(system_message)
39
+
40
+ audio = AudioSegment.from_file(io.BytesIO(audio), format="mp3")
41
+ audio.export("output.wav", format="wav")
42
+
43
+ winsound.PlaySound("output.wav", winsound.SND_FILENAME)
44
+
45
+ chat_transcript = "\n".join(messages)
46
+ return chat_transcript
47
+
48
+ iface = gr.Interface(
49
+ fn=transcribe,
50
+ inputs=gr.Audio(source="microphone", type="filepath", placeholder="Please start speaking..."),
51
+ outputs="text",
52
+ title="🤖 My Desktop ChatGPT Assistant 🤖",
53
+ description="🌟 Please ask me your question and I will respond both verbally and in text to you...",
54
+ )
55
+
56
+ iface.launch()