Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -839,39 +839,38 @@ def listen_in_hindi(response_text):
|
|
839 |
try:
|
840 |
if not response_text:
|
841 |
raise ValueError("No response text available to translate.")
|
842 |
-
|
843 |
-
#
|
844 |
project_id = os.getenv("PROJECT_ID")
|
|
|
|
|
|
|
845 |
client = translate.TranslationServiceClient()
|
846 |
-
|
847 |
-
|
848 |
response = client.translate_text(
|
849 |
-
|
850 |
-
|
851 |
-
|
852 |
-
|
853 |
-
|
854 |
-
"target_language_code": "hi",
|
855 |
-
}
|
856 |
)
|
857 |
translated_text = response.translations[0].translated_text
|
858 |
|
859 |
-
|
860 |
-
|
861 |
-
# Generate audio using ElevenLabs
|
862 |
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
|
863 |
if not ELEVENLABS_API_KEY:
|
864 |
-
raise ValueError("ELEVENLABS_API_KEY
|
865 |
-
|
866 |
elevenlabs_client = ElevenLabs(api_key=ELEVENLABS_API_KEY)
|
867 |
-
|
868 |
text=translated_text,
|
869 |
-
voice_id="
|
870 |
model_id="eleven_multilingual_v2",
|
871 |
output_format="mp3_44100_128",
|
872 |
)
|
873 |
|
874 |
-
# Save to file
|
875 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f:
|
876 |
f.write(audio_bytes)
|
877 |
audio_path = f.name
|
@@ -880,7 +879,7 @@ def listen_in_hindi(response_text):
|
|
880 |
|
881 |
except Exception as e:
|
882 |
logging.error(f"Error in 'listen_in_hindi': {e}")
|
883 |
-
return None
|
884 |
|
885 |
def get_last_bot_response(chat_history):
|
886 |
if not chat_history or not isinstance(chat_history, list):
|
|
|
839 |
try:
|
840 |
if not response_text:
|
841 |
raise ValueError("No response text available to translate.")
|
842 |
+
|
843 |
+
# Step 1: Translate to Hindi
|
844 |
project_id = os.getenv("PROJECT_ID")
|
845 |
+
if not project_id:
|
846 |
+
raise ValueError("PROJECT_ID not set in environment.")
|
847 |
+
|
848 |
client = translate.TranslationServiceClient()
|
849 |
+
parent = f"projects/{project_id}/locations/global"
|
850 |
+
|
851 |
response = client.translate_text(
|
852 |
+
parent=parent,
|
853 |
+
contents=[response_text],
|
854 |
+
mime_type="text/plain",
|
855 |
+
source_language_code="en-US",
|
856 |
+
target_language_code="hi",
|
|
|
|
|
857 |
)
|
858 |
translated_text = response.translations[0].translated_text
|
859 |
|
860 |
+
# Step 2: Generate audio with ElevenLabs
|
|
|
|
|
861 |
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
|
862 |
if not ELEVENLABS_API_KEY:
|
863 |
+
raise ValueError("ELEVENLABS_API_KEY not set")
|
864 |
+
|
865 |
elevenlabs_client = ElevenLabs(api_key=ELEVENLABS_API_KEY)
|
866 |
+
audio_bytes = elevenlabs_client.text_to_speech.convert(
|
867 |
text=translated_text,
|
868 |
+
voice_id="MF4J4IDTRo0AxOO4dpFR",
|
869 |
model_id="eleven_multilingual_v2",
|
870 |
output_format="mp3_44100_128",
|
871 |
)
|
872 |
|
873 |
+
# Step 3: Save to a temp file and return path
|
874 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f:
|
875 |
f.write(audio_bytes)
|
876 |
audio_path = f.name
|
|
|
879 |
|
880 |
except Exception as e:
|
881 |
logging.error(f"Error in 'listen_in_hindi': {e}")
|
882 |
+
return None, f"Error: {e}"
|
883 |
|
884 |
def get_last_bot_response(chat_history):
|
885 |
if not chat_history or not isinstance(chat_history, list):
|