Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,29 +3,28 @@ import requests
|
|
| 3 |
from gtts import gTTS
|
| 4 |
import os
|
| 5 |
import uuid
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
GROQ_API_KEY = "
|
| 9 |
GROQ_API_URL = "https://api.groq.com/v1/chat/completions"
|
| 10 |
|
| 11 |
# Function to process audio and return translated speech
|
| 12 |
-
def voice_to_voice(
|
| 13 |
-
if not
|
| 14 |
return "No audio received", None
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
with open(audio_path, "wb") as f:
|
| 19 |
-
f.write(audio)
|
| 20 |
|
| 21 |
# Transcribe using Groq API
|
| 22 |
headers = {"Authorization": f"Bearer {GROQ_API_KEY}"}
|
| 23 |
data = {
|
| 24 |
-
"model": "llama3-8b",
|
| 25 |
-
"messages": [{"role": "user", "content": "Transcribe this audio:
|
| 26 |
"max_tokens": 100
|
| 27 |
}
|
| 28 |
-
|
| 29 |
response = requests.post(GROQ_API_URL, json=data, headers=headers)
|
| 30 |
transcription = response.json().get("choices", [{}])[0].get("message", {}).get("content", "")
|
| 31 |
|
|
@@ -39,10 +38,10 @@ def voice_to_voice(audio):
|
|
| 39 |
# Translate text using Groq API
|
| 40 |
translate_data = {
|
| 41 |
"model": "llama3-8b",
|
| 42 |
-
"messages": [{"role": "user", "content": f"Translate
|
| 43 |
"max_tokens": 100
|
| 44 |
}
|
| 45 |
-
|
| 46 |
translate_response = requests.post(GROQ_API_URL, json=translate_data, headers=headers)
|
| 47 |
translated_text = translate_response.json().get("choices", [{}])[0].get("message", {}).get("content", "")
|
| 48 |
|
|
@@ -59,8 +58,8 @@ def voice_to_voice(audio):
|
|
| 59 |
# Gradio Interface
|
| 60 |
with gr.Blocks() as demo:
|
| 61 |
gr.Markdown("### ποΈ **Urdu β Pashto Real-Time Voice Translator**")
|
| 62 |
-
|
| 63 |
-
audio_input = gr.Audio(
|
| 64 |
translated_text = gr.Textbox(label="π Translated Text")
|
| 65 |
audio_output = gr.Audio(label="π Translated Voice")
|
| 66 |
|
|
@@ -69,3 +68,4 @@ with gr.Blocks() as demo:
|
|
| 69 |
|
| 70 |
demo.launch()
|
| 71 |
|
|
|
|
|
|
| 3 |
from gtts import gTTS
|
| 4 |
import os
|
| 5 |
import uuid
|
| 6 |
+
import soundfile as sf
|
| 7 |
|
| 8 |
+
# Groq API key
|
| 9 |
+
GROQ_API_KEY = "your-groq-api-key" # πΉ Replace this with your API key
|
| 10 |
GROQ_API_URL = "https://api.groq.com/v1/chat/completions"
|
| 11 |
|
| 12 |
# Function to process audio and return translated speech
|
| 13 |
+
def voice_to_voice(audio_path):
|
| 14 |
+
if not audio_path:
|
| 15 |
return "No audio received", None
|
| 16 |
|
| 17 |
+
# Read audio file
|
| 18 |
+
audio_data, samplerate = sf.read(audio_path)
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# Transcribe using Groq API
|
| 21 |
headers = {"Authorization": f"Bearer {GROQ_API_KEY}"}
|
| 22 |
data = {
|
| 23 |
+
"model": "llama3-8b",
|
| 24 |
+
"messages": [{"role": "user", "content": f"Transcribe this audio file: {audio_path}"}],
|
| 25 |
"max_tokens": 100
|
| 26 |
}
|
| 27 |
+
|
| 28 |
response = requests.post(GROQ_API_URL, json=data, headers=headers)
|
| 29 |
transcription = response.json().get("choices", [{}])[0].get("message", {}).get("content", "")
|
| 30 |
|
|
|
|
| 38 |
# Translate text using Groq API
|
| 39 |
translate_data = {
|
| 40 |
"model": "llama3-8b",
|
| 41 |
+
"messages": [{"role": "user", "content": f"Translate from {source_lang} to {target_lang}: {transcription}"}],
|
| 42 |
"max_tokens": 100
|
| 43 |
}
|
| 44 |
+
|
| 45 |
translate_response = requests.post(GROQ_API_URL, json=translate_data, headers=headers)
|
| 46 |
translated_text = translate_response.json().get("choices", [{}])[0].get("message", {}).get("content", "")
|
| 47 |
|
|
|
|
| 58 |
# Gradio Interface
|
| 59 |
with gr.Blocks() as demo:
|
| 60 |
gr.Markdown("### ποΈ **Urdu β Pashto Real-Time Voice Translator**")
|
| 61 |
+
|
| 62 |
+
audio_input = gr.Audio(type="filepath", label="π€ Speak Now")
|
| 63 |
translated_text = gr.Textbox(label="π Translated Text")
|
| 64 |
audio_output = gr.Audio(label="π Translated Voice")
|
| 65 |
|
|
|
|
| 68 |
|
| 69 |
demo.launch()
|
| 70 |
|
| 71 |
+
|