Sapna36 commited on
Commit
3597254
Β·
verified Β·
1 Parent(s): ba61a38

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -15
app.py CHANGED
@@ -3,29 +3,28 @@ import requests
3
  from gtts import gTTS
4
  import os
5
  import uuid
 
6
 
7
- # Set your Groq API key
8
- GROQ_API_KEY = "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" # πŸ”Ή Replace with your actual 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(audio):
13
- if not audio:
14
  return "No audio received", None
15
 
16
- # Save audio file
17
- audio_path = f"input_{uuid.uuid4()}.wav"
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", # Adjust based on your Groq model availability
25
- "messages": [{"role": "user", "content": "Transcribe this audio: " + audio_path}],
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 this from {source_lang} to {target_lang}: {transcription}"}],
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(source="microphone", type="filepath", label="🎀 Speak Now")
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
+