akazmi commited on
Commit
6dc4920
·
verified ·
1 Parent(s): f84248c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -33
app.py CHANGED
@@ -1,42 +1,59 @@
1
  import streamlit as st
2
- from transformers import pipeline
 
3
  import soundfile as sf
 
4
 
5
- # Initialize translation model (English <-> Urdu)
6
- translation_model = pipeline("translation_en_to_ur", model="Helsinki-NLP/opus-mt-en-ur")
7
- reverse_translation_model = pipeline("translation_ur_to_en", model="Helsinki-NLP/opus-mt-ur-en")
8
 
9
- # Initialize text-to-speech model (English to Speech)
10
- tts_model = pipeline("text-to-speech", model="espnet/kan-bayashi_ljspeech")
11
-
12
- # Function to translate text and provide feedback
13
- def translate_and_speak(text, direction):
14
- if direction == "English to Urdu":
15
- translated_text = translation_model(text)[0]['translation_text']
16
- else:
17
- translated_text = reverse_translation_model(text)[0]['translation_text']
 
 
 
 
 
18
 
19
- # Use TTS to synthesize speech from translated text
20
- audio = tts_model(translated_text)
21
- audio_path = "output.wav"
22
- sf.write(audio_path, audio["array"], 22050) # Save audio to file
23
- return translated_text, audio_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  # Streamlit app UI
26
- st.title("AI-Powered Language Tutor")
27
- st.write("An interactive tutor to help you practice English-Urdu translations with speech feedback!")
28
 
29
- # User input for translation
30
- text_input = st.text_area("Enter Text", "Hello, how are you?")
31
- direction = st.radio("Choose Translation Direction", ["English to Urdu", "Urdu to English"])
 
 
 
 
 
 
 
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