Spaces:
Running
Running
Upload 2 files
Browse files- app.py +71 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""VoiveToVoice.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colab.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1rtd7ax_ftu_b-swqrCoZPjXLRmELmysq
|
8 |
+
"""
|
9 |
+
|
10 |
+
!pip install gradio openai gtts pydub numpy requests groq openai-whisper
|
11 |
+
!apt-get install -y ffmpeg
|
12 |
+
|
13 |
+
import os
|
14 |
+
os.environ["GROQ_API_KEY"] = "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"
|
15 |
+
|
16 |
+
import os
|
17 |
+
import gradio as gr
|
18 |
+
import whisper
|
19 |
+
from gtts import gTTS
|
20 |
+
import io
|
21 |
+
from groq import Groq
|
22 |
+
|
23 |
+
# Initialize the Groq client
|
24 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
25 |
+
|
26 |
+
# Load the Whisper model
|
27 |
+
model = whisper.load_model("base") # You can choose other models like "small", "medium", "large"
|
28 |
+
|
29 |
+
def process_audio(file_path):
|
30 |
+
try:
|
31 |
+
# Load the audio file
|
32 |
+
audio = whisper.load_audio(file_path)
|
33 |
+
|
34 |
+
# Transcribe the audio using Whisper
|
35 |
+
result = model.transcribe(audio)
|
36 |
+
text = result["text"]
|
37 |
+
|
38 |
+
# Generate a response using Groq
|
39 |
+
chat_completion = client.chat.completions.create(
|
40 |
+
messages=[{"role": "user", "content": text}],
|
41 |
+
model="llama3-8b-8192", # Replace with the correct model if necessary
|
42 |
+
)
|
43 |
+
|
44 |
+
# Access the response using dot notation
|
45 |
+
response_message = chat_completion.choices[0].message.content.strip()
|
46 |
+
|
47 |
+
# Convert the response text to speech
|
48 |
+
tts = gTTS(response_message)
|
49 |
+
response_audio_io = io.BytesIO()
|
50 |
+
tts.write_to_fp(response_audio_io) # Save the audio to the BytesIO object
|
51 |
+
response_audio_io.seek(0)
|
52 |
+
|
53 |
+
# Save audio to a file to ensure it's generated correctly
|
54 |
+
with open("response.mp3", "wb") as audio_file:
|
55 |
+
audio_file.write(response_audio_io.getvalue())
|
56 |
+
|
57 |
+
# Return the response text and the path to the saved audio file
|
58 |
+
return response_message, "response.mp3"
|
59 |
+
|
60 |
+
except Exception as e:
|
61 |
+
return f"An error occurred: {e}", None
|
62 |
+
|
63 |
+
iface = gr.Interface(
|
64 |
+
fn=process_audio,
|
65 |
+
inputs=gr.Audio(type="filepath"), # Use type="filepath"
|
66 |
+
outputs=[gr.Textbox(label="Response Text"), gr.Audio(label="Response Audio")],
|
67 |
+
live=True
|
68 |
+
)
|
69 |
+
|
70 |
+
iface.launch()
|
71 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
whisper
|
2 |
+
groq
|
3 |
+
gtts
|
4 |
+
gradio
|
5 |
+
numpy
|
6 |
+
soundfile
|