Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
from decouple import config
|
4 |
+
from gtts import gTTS
|
5 |
+
import os
|
6 |
+
import win32com.client
|
7 |
+
import pythoncom
|
8 |
+
import config
|
9 |
+
|
10 |
+
openai.api_key = config.API_KEYS['openai']
|
11 |
+
|
12 |
+
# The Models Job or role
|
13 |
+
messages = [
|
14 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
15 |
+
]
|
16 |
+
|
17 |
+
|
18 |
+
# language = 'en'
|
19 |
+
|
20 |
+
|
21 |
+
# Main method goes here
|
22 |
+
def decipher(audio):
|
23 |
+
global messages
|
24 |
+
|
25 |
+
# Using openAI's speech to text model
|
26 |
+
audio_file = open(audio, "rb")
|
27 |
+
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
28 |
+
|
29 |
+
messages.append({"role": "user", "content": transcript["text"]})
|
30 |
+
|
31 |
+
response = openai.ChatCompletion.create(
|
32 |
+
model="gpt-3.5-turbo",
|
33 |
+
messages=messages
|
34 |
+
)
|
35 |
+
|
36 |
+
system_message = response["choices"][0]["message"]["content"]
|
37 |
+
pythoncom.CoInitialize()
|
38 |
+
speaker = win32com.client.Dispatch("SAPI.SpVoice")
|
39 |
+
speaker.Speak(system_message)
|
40 |
+
# myobj = gTTS(text=system_message, lang=language, slow=False)
|
41 |
+
# myobj.save("welcome.mp3")
|
42 |
+
# # Playing the converted file
|
43 |
+
# os.system("start welcome.mp3")
|
44 |
+
messages.append({"role": "assistant", "content": system_message},)
|
45 |
+
|
46 |
+
chat_transcript = ""
|
47 |
+
for message in messages:
|
48 |
+
if message['role'] != 'system':
|
49 |
+
chat_transcript += message['role'] + ": " + message['content'] + "\n\n"
|
50 |
+
|
51 |
+
return chat_transcript
|
52 |
+
|
53 |
+
|
54 |
+
# Using Gradio's audio Interface
|
55 |
+
interface = gr.Interface(fn=decipher, inputs=gr.Audio(
|
56 |
+
source="microphone", type="filepath"), outputs="text")
|
57 |
+
interface.launch(share=True)
|