antfraia commited on
Commit
5fc56a1
·
verified ·
1 Parent(s): 2081c0c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -0
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from langchain_groq import ChatGroq
3
+ from langchain.schema import SystemMessage, HumanMessage
4
+ import requests
5
+ import tempfile
6
+ import time
7
+
8
+ # Configuration of the Groq model
9
+ groq_api_key = "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" # Replace with your actual Groq API key
10
+ llm = ChatGroq(api_key=groq_api_key, model_name="llama3-70b-8192") # Corrected model name
11
+
12
+ # ElevenLabs API key and voice ID
13
+ XI_API_KEY = "sk_b254c267851485b60d23fb2e15fa8fde9f5fbc0d835127e2" # Replace with your ElevenLabs API key
14
+ VOICE_ID = "iYwRDEf2D1WyqRRecXPA" # Replace with your voice ID
15
+
16
+ def translate_and_speak(user_input, target_language):
17
+ try:
18
+ start_time = time.time() # Start total processing time
19
+
20
+ # Generate translation using Groq model
21
+ translation_start = time.time()
22
+ system_prompt = f"You are expected to translate the user input exclusively into {target_language} without adding anything else."
23
+ messages = [
24
+ SystemMessage(content=system_prompt),
25
+ HumanMessage(content=user_input)
26
+ ]
27
+ response = llm.invoke(messages)
28
+ translation_end = time.time()
29
+
30
+ # Check if the response is valid
31
+ if not response or not hasattr(response, 'content'):
32
+ raise ValueError("Invalid response from the translation model.")
33
+
34
+ generated_text = response.content.strip()
35
+
36
+ # Use ElevenLabs API to generate speech
37
+ tts_start = time.time()
38
+ url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}"
39
+ headers = {
40
+ "Accept": "audio/mpeg",
41
+ "Content-Type": "application/json",
42
+ "xi-api-key": XI_API_KEY
43
+ }
44
+ data = {
45
+ "text": generated_text,
46
+ "model_id": "eleven_multilingual_v2",
47
+ "voice_settings": {
48
+ "stability": 0.75,
49
+ "similarity_boost": 0.75
50
+ }
51
+ }
52
+ tts_response = requests.post(url, json=data, headers=headers)
53
+ tts_end = time.time()
54
+
55
+ if tts_response.status_code == 200:
56
+ # Save audio to a temporary file
57
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
58
+ fp.write(tts_response.content)
59
+ audio_file = fp.name
60
+ end_time = time.time()
61
+
62
+ # Calculate processing times
63
+ translation_time = translation_end - translation_start
64
+ tts_time = tts_end - tts_start
65
+ total_time = end_time - start_time
66
+
67
+ # Prepare timings information
68
+ timings_info = f"Translation time: {translation_time:.2f} seconds\n"
69
+ timings_info += f"Text-to-Speech time: {tts_time:.2f} seconds\n"
70
+ timings_info += f"Total processing time: {total_time:.2f} seconds"
71
+
72
+ return generated_text, audio_file, timings_info
73
+ else:
74
+ error_message = f"Text-to-Speech API Error: {tts_response.status_code} - {tts_response.text}"
75
+ return error_message, None, None
76
+ except Exception as e:
77
+ # Return the exception message
78
+ error_details = f"An error occurred: {str(e)}"
79
+ return error_details, None, None
80
+
81
+ # Create Gradio interface
82
+ iface = gr.Interface(
83
+ fn=translate_and_speak,
84
+ inputs=[
85
+ gr.Textbox(lines=2, placeholder="Enter text to translate...", label="Input Text"),
86
+ gr.Dropdown(
87
+ choices=["Spanish", "French", "German", "Italian", "Chinese", "Japanese"],
88
+ value="Spanish",
89
+ label="Target Language"
90
+ )
91
+ ],
92
+ outputs=[
93
+ gr.Textbox(label="Translated Text"),
94
+ gr.Audio(label="Spoken Audio", autoplay=True),
95
+ gr.Textbox(label="Processing Times")
96
+ ],
97
+ title="Multilingual Text Translator and Speech Synthesizer",
98
+ description="Translate text into the selected language and listen to the spoken audio.",
99
+ allow_flagging="never"
100
+ )
101
+
102
+ # Launch the app
103
+ iface.launch()