Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -281,13 +281,20 @@ def read_srt_file(file_path):
|
|
281 |
except Exception as e:
|
282 |
print(f"An error occurred: {e}")
|
283 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
284 |
def enhance_text(api_key, text, google):
|
285 |
url = "https://api.one-api.ir/chatbot/v1/gpt4o/"
|
286 |
|
287 |
# Prepare the request body
|
288 |
request_body = [{
|
289 |
"role": "user",
|
290 |
-
"content": f" i have a main English text that has been translated, use the main text and the translations to write a better translation of the main text in persian. main text: {text}"
|
291 |
},
|
292 |
{
|
293 |
"role": "assistant",
|
@@ -305,17 +312,26 @@ def enhance_text(api_key, text, google):
|
|
305 |
}
|
306 |
|
307 |
# Make the POST request
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
319 |
|
320 |
def translate_text(api_key, source_lang, target_lang, text):
|
321 |
url = "https://api.one-api.ir/translate/v1/google/"
|
@@ -337,11 +353,11 @@ def write_google(google_translate):
|
|
337 |
|
338 |
def time_to_seconds(time_obj):
|
339 |
return time_obj.hours * 3600 + time_obj.minutes * 60 + time_obj.seconds + time_obj.milliseconds / 1000
|
340 |
-
|
341 |
def create_subtitle_clips(subtitles, videosize, fontsize, font, color, debug):
|
342 |
subtitle_clips = []
|
343 |
for subtitle in subtitles:
|
344 |
-
start_time = time_to_seconds(subtitle.start)
|
345 |
end_time = time_to_seconds(subtitle.end)
|
346 |
duration = end_time - start_time
|
347 |
video_width, video_height = videosize
|
@@ -349,18 +365,11 @@ def create_subtitle_clips(subtitles, videosize, fontsize, font, color, debug):
|
|
349 |
max_height = video_height * 0.2
|
350 |
#reshaped_text = arabic_reshaper.reshape(subtitle.text)
|
351 |
#bidi_text = get_display(reshaped_text)
|
352 |
-
text_clip = TextClip(font, subtitle.text, font_size=fontsize, size=(int(video_width * 0.
|
353 |
-
color=color, bg_color='black', method='caption').with_start(start_time).with_duration(duration)
|
354 |
-
color_clip = ColorClip(size=(int(max_width), int(max_height)),
|
355 |
-
color=(0, 255, 255)).with_start(start_time).with_duration(duration)
|
356 |
-
color_clip = color_clip.with_opacity(.6)
|
357 |
subtitle_x_position = 'center'
|
358 |
-
subtitle_y_position = video_height * 0.
|
359 |
text_position = (subtitle_x_position, subtitle_y_position)
|
360 |
-
|
361 |
-
clip_to_overlay = clip_to_overlay.with_position('center')
|
362 |
-
subtitle_clips.append(clip_to_overlay)
|
363 |
-
|
364 |
return subtitle_clips
|
365 |
|
366 |
|
|
|
281 |
except Exception as e:
|
282 |
print(f"An error occurred: {e}")
|
283 |
|
284 |
+
def clean_text(text):
|
285 |
+
# Remove 'srt ' from the start of each line
|
286 |
+
# Remove ''' from the start and end
|
287 |
+
text = re.sub(r"^```|```$", '', text)
|
288 |
+
text = re.sub(r'^srt', '', text, flags=re.MULTILINE)
|
289 |
+
return text
|
290 |
+
|
291 |
def enhance_text(api_key, text, google):
|
292 |
url = "https://api.one-api.ir/chatbot/v1/gpt4o/"
|
293 |
|
294 |
# Prepare the request body
|
295 |
request_body = [{
|
296 |
"role": "user",
|
297 |
+
"content": f" i have a main English text that has been translated, use the main text and the translations to write a better translation of the main text in persian. main text: {text}. in response dont add anything and only return the translation will keeping the srt format"
|
298 |
},
|
299 |
{
|
300 |
"role": "assistant",
|
|
|
312 |
}
|
313 |
|
314 |
# Make the POST request
|
315 |
+
attempts = 0
|
316 |
+
max_attempts = 3
|
317 |
+
|
318 |
+
while attempts < max_attempts:
|
319 |
+
response = requests.post(url, headers=headers, json=request_body)
|
320 |
+
if response.status_code == 200:
|
321 |
+
result = response.json()
|
322 |
+
if result["status"] == 200:
|
323 |
+
print("status: ", result["status"])
|
324 |
+
te = clean_text(result["result"][0])
|
325 |
+
print("result: ", te)
|
326 |
+
return te
|
327 |
+
else:
|
328 |
+
print(f"Error: status {result['status']}, retrying in 30 seconds...")
|
329 |
+
else:
|
330 |
+
print(f"Error: {response.status_code}, {response.text}, retrying in 30 seconds...")
|
331 |
+
attempts += 1
|
332 |
+
time.sleep(30)
|
333 |
+
|
334 |
+
return {"Error": "Max attempts reached. Could not retrieve a successful response."}
|
335 |
|
336 |
def translate_text(api_key, source_lang, target_lang, text):
|
337 |
url = "https://api.one-api.ir/translate/v1/google/"
|
|
|
353 |
|
354 |
def time_to_seconds(time_obj):
|
355 |
return time_obj.hours * 3600 + time_obj.minutes * 60 + time_obj.seconds + time_obj.milliseconds / 1000
|
356 |
+
|
357 |
def create_subtitle_clips(subtitles, videosize, fontsize, font, color, debug):
|
358 |
subtitle_clips = []
|
359 |
for subtitle in subtitles:
|
360 |
+
start_time = time_to_seconds(subtitle.start) # Add 2 seconds offset
|
361 |
end_time = time_to_seconds(subtitle.end)
|
362 |
duration = end_time - start_time
|
363 |
video_width, video_height = videosize
|
|
|
365 |
max_height = video_height * 0.2
|
366 |
#reshaped_text = arabic_reshaper.reshape(subtitle.text)
|
367 |
#bidi_text = get_display(reshaped_text)
|
368 |
+
text_clip = TextClip(font, subtitle.text, font_size=fontsize, size=(int(video_width * 0.8), int(video_height * 0.2)) ,text_align="center" ,color=color, method='caption').with_start(start_time).with_duration(duration)
|
|
|
|
|
|
|
|
|
369 |
subtitle_x_position = 'center'
|
370 |
+
subtitle_y_position = video_height * 0.68
|
371 |
text_position = (subtitle_x_position, subtitle_y_position)
|
372 |
+
subtitle_clips.append(text_clip.with_position(text_position))
|
|
|
|
|
|
|
373 |
return subtitle_clips
|
374 |
|
375 |
|