Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,59 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
|
|
|
| 3 |
import soundfile as sf
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
reverse_translation_model = pipeline("translation_ur_to_en", model="Helsinki-NLP/opus-mt-ur-en")
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Streamlit app UI
|
| 26 |
-
st.title("
|
| 27 |
-
st.write("
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
text_input = st.text_area("Enter Text", "
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
# Button to process the text and play audio
|
| 34 |
-
if st.button("Translate and Speak"):
|
| 35 |
-
translated_text, audio_path = translate_and_speak(text_input, direction)
|
| 36 |
-
|
| 37 |
-
# Display translated text and audio
|
| 38 |
-
st.subheader("Translated Text:")
|
| 39 |
-
st.write(translated_text)
|
| 40 |
-
|
| 41 |
-
st.subheader("Generated Speech:")
|
| 42 |
-
st.audio(audio_path, format="audio/wav")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
import soundfile as sf
|
| 5 |
+
import io
|
| 6 |
|
| 7 |
+
# Set your Groq API key here
|
| 8 |
+
GROQ_API_KEY = "YOUR_GROQ_API_KEY"
|
|
|
|
| 9 |
|
| 10 |
+
# Function to interact with Groq API for Text-to-Speech
|
| 11 |
+
def groq_tts(text):
|
| 12 |
+
url = "https://api.groq.com/openai/v1/chat/completions"
|
| 13 |
+
headers = {
|
| 14 |
+
"Content-Type": "application/json",
|
| 15 |
+
"Authorization": f"Bearer {GROQ_API_KEY}"
|
| 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 |
+
# Function to save audio and display in Streamlit
|
| 37 |
+
def save_audio_from_base64(audio_base64):
|
| 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 |
# Streamlit app UI
|
| 46 |
+
st.title("Groq TTS Integration with Streamlit")
|
| 47 |
+
st.write("Enter some text to convert it into speech using Groq API!")
|
| 48 |
|
| 49 |
+
# Text input
|
| 50 |
+
text_input = st.text_area("Enter Text", "Explain the importance of fast language models")
|
| 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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|