Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,36 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
import io
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
# Function to
|
| 11 |
-
def
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
}
|
| 17 |
-
payload = {
|
| 18 |
-
"model": "llama3-8b-8192",
|
| 19 |
-
"messages": [{
|
| 20 |
-
"role": "user",
|
| 21 |
-
"content": text
|
| 22 |
-
}]
|
| 23 |
-
}
|
| 24 |
-
|
| 25 |
-
response = requests.post(url, headers=headers, json=payload)
|
| 26 |
-
|
| 27 |
-
if response.status_code == 200:
|
| 28 |
-
data = response.json()
|
| 29 |
-
# Assuming the audio is returned as base64, decode it
|
| 30 |
-
audio_base64 = data.get("choices")[0].get("message").get("content")
|
| 31 |
-
return audio_base64
|
| 32 |
-
else:
|
| 33 |
-
st.error(f"Error with Groq API: {response.status_code}")
|
| 34 |
-
return None
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
if audio_base64:
|
| 39 |
-
audio_data = io.BytesIO(audio_base64.decode('base64'))
|
| 40 |
-
audio_path = "groq_audio.wav"
|
| 41 |
-
sf.write(audio_path, audio_data, 22050) # Save audio to WAV
|
| 42 |
-
return audio_path
|
| 43 |
-
return None
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
st.
|
| 47 |
-
st.write("Enter some text to convert it into speech using Groq API!")
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
if st.button("Generate Speech"):
|
| 53 |
-
audio_base64 = groq_tts(text_input)
|
| 54 |
-
if audio_base64:
|
| 55 |
-
audio_path = save_audio_from_base64(audio_base64)
|
| 56 |
-
if audio_path:
|
| 57 |
-
st.subheader("Generated Speech:")
|
| 58 |
-
st.audio(audio_path, format="audio/wav")
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from gtts import gTTS
|
| 3 |
+
import pyttsx3
|
| 4 |
+
import os
|
|
|
|
| 5 |
|
| 6 |
+
# Function to use Google Text-to-Speech (gTTS)
|
| 7 |
+
def gtts_text_to_speech(text):
|
| 8 |
+
tts = gTTS(text=text, lang='en', slow=False)
|
| 9 |
+
tts.save("output.mp3")
|
| 10 |
+
return "output.mp3"
|
| 11 |
|
| 12 |
+
# Function to use pyttsx3 (Offline TTS)
|
| 13 |
+
def pyttsx3_text_to_speech(text):
|
| 14 |
+
engine = pyttsx3.init()
|
| 15 |
+
engine.save_to_file(text, "output.mp3")
|
| 16 |
+
engine.runAndWait()
|
| 17 |
+
return "output.mp3"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Streamlit UI
|
| 20 |
+
st.title("Text-to-Speech Converter")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Input text
|
| 23 |
+
user_input = st.text_area("Enter text:", "Hello, how can I assist you today?")
|
|
|
|
| 24 |
|
| 25 |
+
# Select TTS option
|
| 26 |
+
tts_option = st.radio("Choose Text-to-Speech Method", ("Google TTS", "Offline TTS"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
# Button to convert to speech
|
| 29 |
+
if st.button("Convert to Speech"):
|
| 30 |
+
if tts_option == "Google TTS":
|
| 31 |
+
output_file = gtts_text_to_speech(user_input)
|
| 32 |
+
else:
|
| 33 |
+
output_file = pyttsx3_text_to_speech(user_input)
|
| 34 |
+
|
| 35 |
+
st.audio(output_file, format="audio/mp3")
|
| 36 |
+
st.success("Speech generated successfully!")
|