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