Spaces:
Runtime error
Runtime error
Kabilash10
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,100 +1,101 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import requests
|
3 |
-
import openai
|
4 |
-
import asyncio
|
5 |
-
|
6 |
-
from
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
from vocode.streaming.models.
|
12 |
-
from vocode.streaming.models.
|
13 |
-
from vocode.streaming.
|
14 |
-
from vocode.streaming.
|
15 |
-
from vocode.streaming.
|
16 |
-
from vocode.streaming.
|
17 |
-
from vocode.
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
{
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
{"role": "
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
"
|
61 |
-
"
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
"
|
66 |
-
|
67 |
-
"
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
interface
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
gr.Textbox(label="
|
93 |
-
gr.
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
interface
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import openai
|
4 |
+
import asyncio
|
5 |
+
import os
|
6 |
+
from deepgram import Deepgram
|
7 |
+
from vocode.streaming.models.transcriber import (
|
8 |
+
DeepgramTranscriberConfig,
|
9 |
+
PunctuationEndpointingConfig,
|
10 |
+
)
|
11 |
+
from vocode.streaming.models.agent import ChatGPTAgentConfig
|
12 |
+
from vocode.streaming.models.message import BaseMessage
|
13 |
+
from vocode.streaming.models.synthesizer import ElevenLabsSynthesizerConfig
|
14 |
+
from vocode.streaming.transcriber.deepgram_transcriber import DeepgramTranscriber
|
15 |
+
from vocode.streaming.agent.chat_gpt_agent import ChatGPTAgent
|
16 |
+
from vocode.streaming.synthesizer.eleven_labs_synthesizer import ElevenLabsSynthesizer
|
17 |
+
from vocode.streaming.streaming_conversation import StreamingConversation
|
18 |
+
from vocode.helpers import create_streaming_microphone_input_and_speaker_output
|
19 |
+
|
20 |
+
# Fetch API keys and voice IDs from environment variables
|
21 |
+
DEEPGRAM_API_KEY = os.getenv("DEEPGRAM_API_KEY")
|
22 |
+
ELEVEN_LABS_API_KEY = os.getenv("ELEVEN_LABS_API_KEY")
|
23 |
+
VOICE_ID = os.getenv("VOICE_ID")
|
24 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
25 |
+
|
26 |
+
# Initialize OpenAI client
|
27 |
+
client = openai.OpenAI(api_key=OPENAI_API_KEY)
|
28 |
+
|
29 |
+
# Initialize Deepgram
|
30 |
+
deepgram = Deepgram(DEEPGRAM_API_KEY)
|
31 |
+
|
32 |
+
# Function to transcribe audio using Deepgram
|
33 |
+
async def transcribe_audio(audio_file_path):
|
34 |
+
with open(audio_file_path, 'rb') as audio_file:
|
35 |
+
audio_data = audio_file.read()
|
36 |
+
|
37 |
+
response = await deepgram.transcription.prerecorded(
|
38 |
+
{"buffer": audio_data, "mimetype": "audio/wav"},
|
39 |
+
{'punctuate': True, 'language': 'en'}
|
40 |
+
)
|
41 |
+
transcription = response['results']['channels'][0]['alternatives'][0]['transcript']
|
42 |
+
return transcription
|
43 |
+
|
44 |
+
# Function to generate content using OpenAI GPT-4
|
45 |
+
def generate_content(input_text):
|
46 |
+
response = client.chat.completions.create(
|
47 |
+
model="gpt-4",
|
48 |
+
messages=[
|
49 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
50 |
+
{"role": "user", "content": input_text}
|
51 |
+
]
|
52 |
+
)
|
53 |
+
generated_text = response.choices[0].message.content.strip()
|
54 |
+
return generated_text
|
55 |
+
|
56 |
+
# Function to convert text to speech using Eleven Labs
|
57 |
+
def text_to_speech(text):
|
58 |
+
url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}"
|
59 |
+
headers = {
|
60 |
+
"Accept": "audio/mpeg",
|
61 |
+
"Content-Type": "application/json",
|
62 |
+
"xi-api-key": ELEVEN_LABS_API_KEY
|
63 |
+
}
|
64 |
+
data = {
|
65 |
+
"text": text,
|
66 |
+
"voice_settings": {
|
67 |
+
"stability": 0.75,
|
68 |
+
"similarity_boost": 0.75
|
69 |
+
}
|
70 |
+
}
|
71 |
+
response = requests.post(url, json=data, headers=headers)
|
72 |
+
|
73 |
+
if response.status_code == 200:
|
74 |
+
with open("output.mp3", "wb") as f:
|
75 |
+
f.write(response.content)
|
76 |
+
return "output.mp3"
|
77 |
+
else:
|
78 |
+
return f"Error: {response.status_code} - {response.text}"
|
79 |
+
|
80 |
+
# Main function to handle the entire process
|
81 |
+
async def process_audio(audio):
|
82 |
+
transcription = await transcribe_audio(audio)
|
83 |
+
generated_text = generate_content(transcription)
|
84 |
+
audio_file = text_to_speech(generated_text)
|
85 |
+
return transcription, generated_text, audio_file
|
86 |
+
|
87 |
+
# Gradio interface setup
|
88 |
+
interface = gr.Interface(
|
89 |
+
fn=lambda audio: asyncio.run(process_audio(audio)),
|
90 |
+
inputs=gr.Audio(type="filepath", label="Speak into your microphone"),
|
91 |
+
outputs=[
|
92 |
+
gr.Textbox(label="Transcription Output"),
|
93 |
+
gr.Textbox(label="Generated Content"),
|
94 |
+
gr.Audio(label="Synthesized Speech")
|
95 |
+
],
|
96 |
+
title="Speech-to-Text, Content Generation, and Text-to-Speech",
|
97 |
+
description="Speak into the microphone, and the system will transcribe your speech, generate content, and convert the generated text into speech."
|
98 |
+
)
|
99 |
+
|
100 |
+
# Launch the Gradio interface
|
101 |
+
interface.launch()
|