Spaces:
Running
Running
File size: 3,996 Bytes
6e6c0b4 6b46b9a 6e6c0b4 b1b46fc 6e6c0b4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
import time
import requests
import re
api_key = "268976:66f4f58a2a905"
def read_srt_file(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as file:
srt_content = file.read()
return srt_content
except FileNotFoundError:
print(f"The file {file_path} was not found.")
except Exception as e:
print(f"An error occurred: {e}")
def clean_text(text):
# Remove 'srt ' from the start of each line
# Remove ''' from the start and end
text = re.sub(r"^```|```$", '', text)
text = re.sub(r'^srt', '', text, flags=re.MULTILINE)
return text
def translate_text(api_key, text, source_language = "en", target_language = "fa"):
url = "https://api.one-api.ir/translate/v1/google/"
request_body = {"source": source_language, "target": target_language, "text": text}
headers = {"one-api-token": api_key, "Content-Type": "application/json"}
response = requests.post(url, headers=headers, json=request_body)
if response.status_code == 200:
result = response.json()
return result['result']
else:
print(f"Error: {response.status_code}, {response.text}")
return None
def enhance_text(api_key, text):
url = "https://api.one-api.ir/chatbot/v1/gpt4o/"
# Prepare the request body
request_body = [{
"role": "user",
"content": f"Please take the following SRT subtitle text in English and translate only the subtitle text into Persian. Ensure that all numbering and time codes remain unchanged. convert English terms in to common persian terms. The output should be a new SRT file with the subtitles in Persian, preserving the original formatting and timings and exept for the subtitle dont return anything in response. the subtitle will be provided in the following message"
},
{
"role": "assistant",
"content": "okay"
},
{
"role": "user",
"content": text
}
]
# Add the API key to the request
headers = {
"one-api-token": api_key,
"Content-Type": "application/json"
}
# Make the POST request
attempts = 0
max_attempts = 3
while attempts < max_attempts:
response = requests.post(url, headers=headers, json=request_body)
if response.status_code == 200:
result = response.json()
if result["status"] == 200:
print("status: ", result["status"])
te = clean_text(result["result"][0])
print("result: ", te)
return te
else:
print(f"Error: status {result['status']}, retrying in 30 seconds...")
else:
print(f"Error: {response.status_code}, {response.text}, retrying in 30 seconds...")
attempts += 1
time.sleep(10)
print("Error Max attempts reached. Could not retrieve a successful response.")
te = translate_text(api_key, text)
return te
def generate_translated_subtitle(language, segments, input_video_name):
input_video_name=input_video_name.split('/')[-1]
subtitle_file = f"{input_video_name}.srt"
text = ""
lines = segments.split('\n')
new_list = [item for item in lines if item != '']
segment_number = 1
for index, segment in enumerate(new_list):
if (index+1) % 3 == 1 or (index+1)==1:
text += f"{segment}\n"
segment_number += 1
if (index+1) % 3 == 2 or (index+1)==2:
text += segment + "\n"
if (index+1) % 3 == 0:
text += f"\u200F{segment}\n\n"
with open(subtitle_file, "a", encoding='utf8') as f:
f.write(text)
return subtitle_file
def translate(srt_files):
print("translate")
for srt_file in srt_files:
srt = read_srt_file(srt_file)
srt_string = enhance_text(api_key, srt)
print(srt_string)
subtitle_file = generate_translated_subtitle('fa', srt_string, 'video_subtitled')
time.sleep(10)
return subtitle_file
|