Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
import speech_recognition as sr
|
4 |
+
from gtts import gTTS
|
5 |
+
from groq import Groq
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
import tempfile
|
8 |
+
import base64
|
9 |
+
|
10 |
+
# Load environment variables
|
11 |
+
load_dotenv()
|
12 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
13 |
+
|
14 |
+
# Initialize Groq Client
|
15 |
+
client = Groq(api_key=GROQ_API_KEY)
|
16 |
+
|
17 |
+
# Function to transcribe voice to text
|
18 |
+
def transcribe_audio(audio_file):
|
19 |
+
recognizer = sr.Recognizer()
|
20 |
+
with sr.AudioFile(audio_file) as source:
|
21 |
+
audio_data = recognizer.record(source)
|
22 |
+
try:
|
23 |
+
text = recognizer.recognize_google(audio_data)
|
24 |
+
return text
|
25 |
+
except sr.UnknownValueError:
|
26 |
+
return "Could not understand the audio"
|
27 |
+
except sr.RequestError:
|
28 |
+
return "Error with speech recognition service"
|
29 |
+
|
30 |
+
# Function to generate speech from text
|
31 |
+
def text_to_speech(text):
|
32 |
+
tts = gTTS(text=text, lang="en")
|
33 |
+
temp_audio_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3").name
|
34 |
+
tts.save(temp_audio_path)
|
35 |
+
return temp_audio_path
|
36 |
+
|
37 |
+
# Function to get AI insights from LLaMA 3 70B
|
38 |
+
def get_ai_response(text):
|
39 |
+
response = client.chat.completions.create(
|
40 |
+
model="llama-3.3-70b-versatile",
|
41 |
+
messages=[
|
42 |
+
{"role": "system", "content": "You are an advanced AI that helps with speech processing."},
|
43 |
+
{"role": "user", "content": f"Analyze this text: {text}"}
|
44 |
+
]
|
45 |
+
)
|
46 |
+
return response.choices[0].message.content
|
47 |
+
|
48 |
+
# Streamlit UI
|
49 |
+
st.title("🎙️ AI Voice Converter: Speech-to-Text & Text-to-Speech")
|
50 |
+
st.write("Convert voice into text and generate AI-powered speech.")
|
51 |
+
|
52 |
+
# Voice-to-Text Section
|
53 |
+
st.subheader("🎤 Voice-to-Text")
|
54 |
+
audio_file = st.file_uploader("Upload an audio file (WAV format)", type=["wav"])
|
55 |
+
|
56 |
+
if audio_file:
|
57 |
+
with open("temp.wav", "wb") as f:
|
58 |
+
f.write(audio_file.getbuffer())
|
59 |
+
|
60 |
+
st.audio(audio_file, format="audio/wav")
|
61 |
+
|
62 |
+
if st.button("Transcribe Audio"):
|
63 |
+
transcribed_text = transcribe_audio("temp.wav")
|
64 |
+
st.write("**Transcribed Text:**", transcribed_text)
|
65 |
+
|
66 |
+
# AI insights
|
67 |
+
ai_insights = get_ai_response(transcribed_text)
|
68 |
+
st.write("**AI Analysis:**", ai_insights)
|
69 |
+
|
70 |
+
# Text-to-Voice Section
|
71 |
+
st.subheader("📝 Text-to-Speech")
|
72 |
+
input_text = st.text_area("Enter text to convert into speech")
|
73 |
+
|
74 |
+
if st.button("Generate Speech"):
|
75 |
+
if input_text.strip():
|
76 |
+
audio_path = text_to_speech(input_text)
|
77 |
+
|
78 |
+
# Convert audio file to base64 for download
|
79 |
+
with open(audio_path, "rb") as f:
|
80 |
+
audio_bytes = f.read()
|
81 |
+
b64 = base64.b64encode(audio_bytes).decode()
|
82 |
+
|
83 |
+
# Audio player and download button
|
84 |
+
st.audio(audio_path, format="audio/mp3")
|
85 |
+
st.markdown(f'<a href="data:audio/mp3;base64,{b64}" download="output.mp3">Download Speech</a>', unsafe_allow_html=True)
|
86 |
+
else:
|
87 |
+
st.error("Please enter text to generate speech.")
|