teoo33 commited on
Commit
01962fb
·
verified ·
1 Parent(s): 4602624

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -4
app.py CHANGED
@@ -4,13 +4,13 @@ import re
4
  import os
5
 
6
  # تنظیم API Key
7
- API_KEY = os.getenv("GEMINI_API_KEY") # کلیدت رو اینجا بذار
8
  genai.configure(api_key=API_KEY)
9
  model = genai.GenerativeModel("gemini-1.5-flash")
10
 
11
  # تابع ترجمه
12
  def translate_with_gemini(text):
13
- if not text.strip(): # اگه متن خالی بود
14
  return ""
15
  prompt = f"Translate this to Persian: {text}"
16
  response = model.generate_content(prompt)
@@ -18,8 +18,20 @@ def translate_with_gemini(text):
18
 
19
  # تابع پردازش SRT
20
  def process_srt(file):
21
- with open(file.name, 'r', encoding='utf-8') as f:
22
- content = f.read()
 
 
 
 
 
 
 
 
 
 
 
 
23
  blocks = re.split(r'\n\n', content.strip())
24
  subtitles = ["خروجی‌ها توسط هوش مصنوعی انجام شده\n"]
25
  for block in blocks:
 
4
  import os
5
 
6
  # تنظیم API Key
7
+ API_KEY = "YOUR_API_KEY_HERE" # کلیدت رو اینجا بذار یا از Secrets استفاده کن
8
  genai.configure(api_key=API_KEY)
9
  model = genai.GenerativeModel("gemini-1.5-flash")
10
 
11
  # تابع ترجمه
12
  def translate_with_gemini(text):
13
+ if not text.strip():
14
  return ""
15
  prompt = f"Translate this to Persian: {text}"
16
  response = model.generate_content(prompt)
 
18
 
19
  # تابع پردازش SRT
20
  def process_srt(file):
21
+ # تست انکودینگ‌های مختلف
22
+ encodings = ['utf-8', 'windows-1252', 'latin1']
23
+ content = None
24
+ for encoding in encodings:
25
+ try:
26
+ with open(file.name, 'r', encoding=encoding) as f:
27
+ content = f.read()
28
+ break
29
+ except UnicodeDecodeError:
30
+ continue
31
+
32
+ if content is None:
33
+ raise ValueError("نمی‌تونم فایل رو با انکودینگ‌های رایج بخونم. لطفاً فایل رو با UTF-8 ذخیره کن.")
34
+
35
  blocks = re.split(r'\n\n', content.strip())
36
  subtitles = ["خروجی‌ها توسط هوش مصنوعی انجام شده\n"]
37
  for block in blocks: