Update app.py
Browse files
app.py
CHANGED
@@ -201,6 +201,42 @@ def read_srt_file(file_path):
|
|
201 |
except Exception as e:
|
202 |
print(f"An error occurred: {e}")
|
203 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
def translate_text(api_key, source_lang, target_lang, text):
|
205 |
url = "https://api.one-api.ir/translate/v1/google/"
|
206 |
request_body = {"source": source_lang, "target": target_lang, "text": text}
|
@@ -208,7 +244,8 @@ def translate_text(api_key, source_lang, target_lang, text):
|
|
208 |
response = requests.post(url, headers=headers, json=request_body)
|
209 |
if response.status_code == 200:
|
210 |
result = response.json()
|
211 |
-
|
|
|
212 |
else:
|
213 |
print(f"Error: {response.status_code}, {response.text}")
|
214 |
return None
|
|
|
201 |
except Exception as e:
|
202 |
print(f"An error occurred: {e}")
|
203 |
|
204 |
+
def enhance_text(api_key, text, google):
|
205 |
+
url = "https://api.one-api.ir/chatbot/v1/gpt4o/"
|
206 |
+
|
207 |
+
# Prepare the request body
|
208 |
+
request_body = [{
|
209 |
+
"role": "user",
|
210 |
+
"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}"
|
211 |
+
},
|
212 |
+
{
|
213 |
+
"role": "assistant",
|
214 |
+
"content": "ok"
|
215 |
+
},
|
216 |
+
{
|
217 |
+
"role": "user",
|
218 |
+
"content": f"tarnslation: {google} in response only return the better version of the translation with maintaining the srt format of the text"
|
219 |
+
},]
|
220 |
+
|
221 |
+
# Add the API key to the request
|
222 |
+
headers = {
|
223 |
+
"one-api-token": api_key,
|
224 |
+
"Content-Type": "application/json"
|
225 |
+
}
|
226 |
+
|
227 |
+
# Make the POST request
|
228 |
+
response = requests.post(url, headers=headers, json=request_body)
|
229 |
+
|
230 |
+
# Check the response status
|
231 |
+
if response.status_code == 200:
|
232 |
+
result = response.json()
|
233 |
+
print("result: ")
|
234 |
+
print(result)
|
235 |
+
return result["result"]
|
236 |
+
else:
|
237 |
+
print(f"Error: {response.status_code}, {response.text}")
|
238 |
+
return None
|
239 |
+
|
240 |
def translate_text(api_key, source_lang, target_lang, text):
|
241 |
url = "https://api.one-api.ir/translate/v1/google/"
|
242 |
request_body = {"source": source_lang, "target": target_lang, "text": text}
|
|
|
244 |
response = requests.post(url, headers=headers, json=request_body)
|
245 |
if response.status_code == 200:
|
246 |
result = response.json()
|
247 |
+
enhanced_text = enhance_text(api_key, text, result['result'])
|
248 |
+
return enhanced_text
|
249 |
else:
|
250 |
print(f"Error: {response.status_code}, {response.text}")
|
251 |
return None
|