Update app.py
Browse files
app.py
CHANGED
@@ -2,9 +2,10 @@ import gradio as gr
|
|
2 |
import openai
|
3 |
from decouple import config
|
4 |
from gtts import gTTS
|
5 |
-
import
|
6 |
import pydub
|
7 |
-
import
|
|
|
8 |
|
9 |
openai.api_key = config.API_KEYS['openai']
|
10 |
|
@@ -18,18 +19,17 @@ def decipher(audio):
|
|
18 |
global messages
|
19 |
|
20 |
# Using openAI's speech to text model
|
21 |
-
|
|
|
22 |
|
23 |
messages.append({"role": "user", "content": transcript["text"]})
|
24 |
|
25 |
-
response =
|
26 |
-
model="
|
27 |
-
|
28 |
-
temperature=0.7,
|
29 |
-
max_tokens=1024,
|
30 |
)
|
31 |
|
32 |
-
system_message = response
|
33 |
messages.append({"role": "assistant", "content": system_message})
|
34 |
|
35 |
# Convert the text to audio using gTTS
|
@@ -39,11 +39,10 @@ def decipher(audio):
|
|
39 |
|
40 |
# Convert the audio to a playable format using pydub
|
41 |
audio_data.seek(0)
|
42 |
-
audio = pydub.AudioSegment.from_file(
|
43 |
|
44 |
-
# Play the audio using
|
45 |
-
|
46 |
-
player(audio)
|
47 |
|
48 |
chat_transcript = ""
|
49 |
for message in messages:
|
@@ -53,12 +52,6 @@ def decipher(audio):
|
|
53 |
return chat_transcript
|
54 |
|
55 |
# Using Gradio's audio Interface
|
56 |
-
interface = gr.Interface(fn=decipher, inputs=gr.
|
57 |
-
type="filepath",
|
58 |
-
|
59 |
-
title="AI Assistant",
|
60 |
-
description="An AI assistant that can transcribe and respond to voice messages",
|
61 |
-
theme="compact")
|
62 |
-
|
63 |
-
if __name__ == "__main__":
|
64 |
-
interface.launch()
|
|
|
2 |
import openai
|
3 |
from decouple import config
|
4 |
from gtts import gTTS
|
5 |
+
import os
|
6 |
import pydub
|
7 |
+
import io
|
8 |
+
import config
|
9 |
|
10 |
openai.api_key = config.API_KEYS['openai']
|
11 |
|
|
|
19 |
global messages
|
20 |
|
21 |
# Using openAI's speech to text model
|
22 |
+
audio_file = open(audio, "rb")
|
23 |
+
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
24 |
|
25 |
messages.append({"role": "user", "content": transcript["text"]})
|
26 |
|
27 |
+
response = openai.ChatCompletion.create(
|
28 |
+
model="gpt-3.5-turbo",
|
29 |
+
messages=messages
|
|
|
|
|
30 |
)
|
31 |
|
32 |
+
system_message = response["choices"][0]["message"]["content"]
|
33 |
messages.append({"role": "assistant", "content": system_message})
|
34 |
|
35 |
# Convert the text to audio using gTTS
|
|
|
39 |
|
40 |
# Convert the audio to a playable format using pydub
|
41 |
audio_data.seek(0)
|
42 |
+
audio = pydub.AudioSegment.from_file(audio_data, format="mp3")
|
43 |
|
44 |
+
# Play the audio using pydub.playback
|
45 |
+
pydub.playback.play(audio)
|
|
|
46 |
|
47 |
chat_transcript = ""
|
48 |
for message in messages:
|
|
|
52 |
return chat_transcript
|
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)
|
|
|
|
|
|
|
|
|
|
|
|