Athspi commited on
Commit
00d9ff4
·
verified ·
1 Parent(s): 4620e6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -2
app.py CHANGED
@@ -13,7 +13,7 @@ logging.getLogger("moviepy").setLevel(logging.ERROR)
13
 
14
  # Configure Gemini API
15
  genai.configure(api_key=os.environ["GEMINI_API_KEY"])
16
- model = genai.GenerativeModel("gemini-2.0-pro-exp-02-05")
17
 
18
  # Supported languages
19
  SUPPORTED_LANGUAGES = [
@@ -46,6 +46,13 @@ ORIGINAL:
46
 
47
  TRANSLATED:"""
48
 
 
 
 
 
 
 
 
49
  def split_audio(audio_path, chunk_duration=60):
50
  """Split audio into smaller chunks (default: 60 seconds)"""
51
  audio = AudioSegment.from_wav(audio_path)
@@ -140,8 +147,18 @@ def create_srt(subtitles_text):
140
 
141
  return "\n".join(srt_output)
142
 
 
 
 
 
 
 
 
 
 
143
  def process_video(video_path, source_lang, target_lang):
144
  """Complete processing pipeline"""
 
145
  try:
146
  # Extract audio
147
  audio_path = extract_audio(video_path)
@@ -178,7 +195,7 @@ def process_video(video_path, source_lang, target_lang):
178
  print(f"Processing error: {str(e)}")
179
  return None, None
180
  finally:
181
- if os.path.exists(audio_path):
182
  os.remove(audio_path)
183
 
184
  # Gradio Interface
 
13
 
14
  # Configure Gemini API
15
  genai.configure(api_key=os.environ["GEMINI_API_KEY"])
16
+ model = genai.GenerativeModel("gemini-2.0-flash-exp")
17
 
18
  # Supported languages
19
  SUPPORTED_LANGUAGES = [
 
46
 
47
  TRANSLATED:"""
48
 
49
+ def extract_audio(video_path):
50
+ """Extract high-quality audio from video"""
51
+ video = VideoFileClip(video_path)
52
+ audio_path = os.path.join(tempfile.gettempdir(), "extracted_audio.wav")
53
+ video.audio.write_audiofile(audio_path, fps=44100, nbytes=2, codec='pcm_s16le')
54
+ return audio_path
55
+
56
  def split_audio(audio_path, chunk_duration=60):
57
  """Split audio into smaller chunks (default: 60 seconds)"""
58
  audio = AudioSegment.from_wav(audio_path)
 
147
 
148
  return "\n".join(srt_output)
149
 
150
+ def translate_subtitles(subtitles, target_lang):
151
+ """Translate subtitles while preserving timestamps"""
152
+ prompt = TRANSLATION_PROMPT.format(
153
+ target_language=target_lang,
154
+ subtitles=subtitles
155
+ )
156
+ response = model.generate_content(prompt)
157
+ return response.text
158
+
159
  def process_video(video_path, source_lang, target_lang):
160
  """Complete processing pipeline"""
161
+ audio_path = None
162
  try:
163
  # Extract audio
164
  audio_path = extract_audio(video_path)
 
195
  print(f"Processing error: {str(e)}")
196
  return None, None
197
  finally:
198
+ if audio_path and os.path.exists(audio_path):
199
  os.remove(audio_path)
200
 
201
  # Gradio Interface