Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,8 +5,6 @@ from faster_whisper import WhisperModel
|
|
5 |
from moviepy.video.io.VideoFileClip import VideoFileClip
|
6 |
import logging
|
7 |
import google.generativeai as genai
|
8 |
-
import time
|
9 |
-
from google.api_core import retry
|
10 |
|
11 |
# Suppress moviepy logs
|
12 |
logging.getLogger("moviepy").setLevel(logging.ERROR)
|
@@ -16,8 +14,8 @@ genai.configure(api_key=os.environ["GEMINI_API_KEY"])
|
|
16 |
|
17 |
# Create the Gemini model
|
18 |
generation_config = {
|
19 |
-
"temperature":
|
20 |
-
"top_p": 0.
|
21 |
"top_k": 40,
|
22 |
"max_output_tokens": 8192,
|
23 |
"response_mime_type": "text/plain",
|
@@ -97,12 +95,11 @@ def format_timestamp(seconds):
|
|
97 |
milliseconds = int((seconds - int(seconds)) * 1000)
|
98 |
return f"{hours:02}:{minutes:02}:{int(seconds):02},{milliseconds:03}"
|
99 |
|
100 |
-
@retry.Retry()
|
101 |
def translate_text(text, target_language):
|
102 |
-
"""Translate text using Gemini
|
103 |
-
prompt
|
|
|
104 |
response = model.generate_content(prompt)
|
105 |
-
time.sleep(1) # Add a delay to avoid hitting the rate limit
|
106 |
return response.text
|
107 |
|
108 |
def translate_srt(srt_text, target_language):
|
@@ -135,14 +132,10 @@ def process_video(video_file, language="Auto Detect", translate_to=None):
|
|
135 |
# Translate subtitles if a target language is provided
|
136 |
translated_srt_file = None
|
137 |
if translate_to and translate_to != "None":
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
f.write(translated_subtitles)
|
143 |
-
except Exception as e:
|
144 |
-
print(f"Translation failed: {e}")
|
145 |
-
translated_srt_file = None
|
146 |
|
147 |
# Clean up extracted audio file
|
148 |
os.remove(audio_file)
|
|
|
5 |
from moviepy.video.io.VideoFileClip import VideoFileClip
|
6 |
import logging
|
7 |
import google.generativeai as genai
|
|
|
|
|
8 |
|
9 |
# Suppress moviepy logs
|
10 |
logging.getLogger("moviepy").setLevel(logging.ERROR)
|
|
|
14 |
|
15 |
# Create the Gemini model
|
16 |
generation_config = {
|
17 |
+
"temperature": 0.7, # Lower temperature for more focused translations
|
18 |
+
"top_p": 0.9,
|
19 |
"top_k": 40,
|
20 |
"max_output_tokens": 8192,
|
21 |
"response_mime_type": "text/plain",
|
|
|
95 |
milliseconds = int((seconds - int(seconds)) * 1000)
|
96 |
return f"{hours:02}:{minutes:02}:{int(seconds):02},{milliseconds:03}"
|
97 |
|
|
|
98 |
def translate_text(text, target_language):
|
99 |
+
"""Translate text using Gemini."""
|
100 |
+
# Magic prompt to ensure concise and accurate translations
|
101 |
+
prompt = f"Translate the following text into {target_language}. Only provide the translated text, no explanations or additional text:\n\n{text}"
|
102 |
response = model.generate_content(prompt)
|
|
|
103 |
return response.text
|
104 |
|
105 |
def translate_srt(srt_text, target_language):
|
|
|
132 |
# Translate subtitles if a target language is provided
|
133 |
translated_srt_file = None
|
134 |
if translate_to and translate_to != "None":
|
135 |
+
translated_subtitles = translate_srt(subtitles, translate_to)
|
136 |
+
translated_srt_file = "translated_subtitles.srt"
|
137 |
+
with open(translated_srt_file, "w", encoding="utf-8") as f:
|
138 |
+
f.write(translated_subtitles)
|
|
|
|
|
|
|
|
|
139 |
|
140 |
# Clean up extracted audio file
|
141 |
os.remove(audio_file)
|