akazmi commited on
Commit
0ed986b
·
verified ·
1 Parent(s): 4cc6338

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -52
app.py CHANGED
@@ -1,59 +1,36 @@
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
 
 
 
 
 
 
 
 
 
 
 
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!")