Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,11 +4,10 @@ from flask import Flask, request, jsonify, send_file, send_from_directory
|
|
4 |
import google.generativeai as genai
|
5 |
from gtts import gTTS, lang
|
6 |
import tempfile
|
7 |
-
import soundfile as sf
|
8 |
from werkzeug.utils import secure_filename
|
9 |
from flask_cors import CORS
|
10 |
|
11 |
-
app = Flask(__name__, static_folder='static')
|
12 |
CORS(app)
|
13 |
|
14 |
# Configure Gemini API
|
@@ -18,20 +17,8 @@ if not GEMINI_API_KEY:
|
|
18 |
genai.configure(api_key=GEMINI_API_KEY)
|
19 |
|
20 |
# Language configurations
|
21 |
-
KOKORO_LANGUAGES = {
|
22 |
-
"American English": "a",
|
23 |
-
"British English": "b",
|
24 |
-
"Japanese": "j",
|
25 |
-
"Mandarin Chinese": "z",
|
26 |
-
"Spanish": "e",
|
27 |
-
"French": "f",
|
28 |
-
"Hindi": "h",
|
29 |
-
"Italian": "i",
|
30 |
-
"Brazilian Portuguese": "p"
|
31 |
-
}
|
32 |
-
|
33 |
GTTS_LANGUAGES = lang.tts_langs()
|
34 |
-
SUPPORTED_LANGUAGES = sorted(
|
35 |
|
36 |
@app.route('/')
|
37 |
def serve_index():
|
@@ -64,8 +51,7 @@ def translate_audio():
|
|
64 |
|
65 |
# Transcribe with Gemini
|
66 |
model = genai.GenerativeModel("gemini-1.5-pro-latest")
|
67 |
-
prompt = """Accurately transcribe this audio file. Return only the raw text without
|
68 |
-
punctuation, or additional commentary. Preserve the original language and meaning."""
|
69 |
|
70 |
response = model.generate_content(
|
71 |
[
|
@@ -79,23 +65,15 @@ def translate_audio():
|
|
79 |
transcription = response.text.strip()
|
80 |
|
81 |
# Translate with Gemini
|
82 |
-
translate_prompt = f"
|
83 |
-
Return only the translated text without any explanations or formatting: {transcription}"""
|
84 |
-
|
85 |
translated_response = model.generate_content(translate_prompt)
|
86 |
translated_text = translated_response.text.strip()
|
87 |
|
88 |
# Generate TTS
|
89 |
-
if target_language
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
# Add actual Kokoro synthesis here
|
94 |
-
else:
|
95 |
-
lang_code = next((k for k, v in GTTS_LANGUAGES.items() if v == target_language), 'en')
|
96 |
-
tts = gTTS(translated_text, lang=lang_code)
|
97 |
-
_, temp_output_path = tempfile.mkstemp(suffix=".mp3")
|
98 |
-
tts.save(temp_output_path)
|
99 |
|
100 |
return jsonify({
|
101 |
'transcription': transcription,
|
|
|
4 |
import google.generativeai as genai
|
5 |
from gtts import gTTS, lang
|
6 |
import tempfile
|
|
|
7 |
from werkzeug.utils import secure_filename
|
8 |
from flask_cors import CORS
|
9 |
|
10 |
+
app = Flask(__name__, static_folder='static', static_url_path='')
|
11 |
CORS(app)
|
12 |
|
13 |
# Configure Gemini API
|
|
|
17 |
genai.configure(api_key=GEMINI_API_KEY)
|
18 |
|
19 |
# Language configurations
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
GTTS_LANGUAGES = lang.tts_langs()
|
21 |
+
SUPPORTED_LANGUAGES = sorted(GTTS_LANGUAGES.values())
|
22 |
|
23 |
@app.route('/')
|
24 |
def serve_index():
|
|
|
51 |
|
52 |
# Transcribe with Gemini
|
53 |
model = genai.GenerativeModel("gemini-1.5-pro-latest")
|
54 |
+
prompt = """Accurately transcribe this audio file. Return only the raw text without formatting."""
|
|
|
55 |
|
56 |
response = model.generate_content(
|
57 |
[
|
|
|
65 |
transcription = response.text.strip()
|
66 |
|
67 |
# Translate with Gemini
|
68 |
+
translate_prompt = f"Translate to {target_language} preserving meaning: {transcription}"
|
|
|
|
|
69 |
translated_response = model.generate_content(translate_prompt)
|
70 |
translated_text = translated_response.text.strip()
|
71 |
|
72 |
# Generate TTS
|
73 |
+
lang_code = next((k for k, v in GTTS_LANGUAGES.items() if v == target_language), 'en')
|
74 |
+
tts = gTTS(translated_text, lang=lang_code)
|
75 |
+
_, temp_output_path = tempfile.mkstemp(suffix=".mp3")
|
76 |
+
tts.save(temp_output_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
return jsonify({
|
79 |
'transcription': transcription,
|