Update app.py
Browse files
app.py
CHANGED
|
@@ -6,46 +6,48 @@ import traceback
|
|
| 6 |
import threading
|
| 7 |
import os
|
| 8 |
import google.generativeai as genai
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
# --- دریافت کلید API (بدون تغییر) ---
|
| 11 |
GOOGLE_API_KEY = os.environ.get('GOOGLE_API_KEY')
|
| 12 |
model = None
|
| 13 |
if GOOGLE_API_KEY:
|
| 14 |
try:
|
| 15 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 16 |
model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
| 17 |
-
|
| 18 |
except Exception as e:
|
| 19 |
-
|
| 20 |
-
model = None
|
| 21 |
else:
|
| 22 |
-
|
| 23 |
|
| 24 |
-
# --- دیکشنری صداهای انگلیسی (همان
|
| 25 |
language_dict_persian_keys = {
|
| 26 |
'انگلیسی (آمریکا) - جنی (زن)': 'en-US-JennyNeural', 'انگلیسی (آمریکا) - گای (مرد)': 'en-US-GuyNeural',
|
| 27 |
-
'انگلیسی (آمریکا) - آنا (زن، کودک)': 'en-US-AnaNeural', 'انگلیسی (آمریکا) - آریا (زن)': 'en-US-AriaNeural',
|
| 28 |
'انگلیسی (آمریکا) - کریستوفر (مرد)': 'en-US-ChristopherNeural', 'انگلیسی (آمریکا) - اریک (مرد)': 'en-US-EricNeural',
|
| 29 |
'انگلیسی (آمریکا) - میشل (زن)': 'en-US-MichelleNeural', 'انگلیسی (آمریکا) - راجر (مرد)': 'en-US-RogerNeural',
|
| 30 |
'انگلیسی (آمریکا) - استفانی (زن)': 'en-US-SteffanNeural', 'انگلیسی (آمریکا) - جیسون (مرد)': 'en-US-JasonNeural',
|
| 31 |
'انگلیسی (آمریکا) - براندون (مرد)': 'en-US-BrandonNeural', 'انگلیسی (آمریکا) - الیزابت (زن)': 'en-US-ElizabethNeural',
|
| 32 |
-
'انگلیسی (آمریکا) - دیویس (مرد)': 'en-US-DavisNeural', 'انگلیسی (آمریکا) - جین (زن)': 'en-US-JaneNeural',
|
| 33 |
-
'انگلیسی (آمریکا) - نانسی (زن)': 'en-US-NancyNeural', 'انگلیسی (آمریکا) - سارا (زن)': 'en-US-SaraNeural',
|
| 34 |
-
'انگلیسی (آمریکا) - تونی (مرد)': 'en-US-TonyNeural',
|
| 35 |
'انگلیسی (بریتانیا) - لیبی (زن)': 'en-GB-LibbyNeural', 'انگلیسی (بریتانیا) - میزی (زن)': 'en-GB-MaisieNeural',
|
| 36 |
'انگلیسی (بریتانیا) - رایان (مرد)': 'en-GB-RyanNeural', 'انگلیسی (بریتانیا) - سونیا (زن)': 'en-GB-SoniaNeural',
|
| 37 |
-
'انگلیسی (بریتانیا) - توماس (مرد)': 'en-GB-ThomasNeural', 'انگلیسی (بریتانیا) - میا (زن)': 'en-GB-MiaNeural',
|
| 38 |
-
'انگلیسی (بریتانیا) - نوآ (مرد)': 'en-GB-NoahNeural', 'انگلیسی (بریتانیا) - الیور (مرد)': 'en-GB-OliverNeural',
|
| 39 |
-
'انگلیسی (بریتانیا) - اولیویا (زن)': 'en-GB-OliviaNeural',
|
| 40 |
'انگلیسی (استرالیا) - ناتاشا (زن)': 'en-AU-NatashaNeural', 'انگلیسی (استرالیا) - ویلیام (مرد)': 'en-AU-WilliamNeural',
|
| 41 |
-
'انگلیسی (استرالیا) - آنت (زن)': 'en-AU-AnnetteNeural', 'انگلیسی (استرالیا) - کارلی (زن)': 'en-AU-CarlyNeural',
|
| 42 |
-
'انگلیسی (استرالیا) - کن (مرد)': 'en-AU-KenNeural', 'انگلیسی (استرالیا) - کیمبرلی (زن)': 'en-AU-KimNeural',
|
| 43 |
-
'انگلیسی (استرالیا) - تینا (زن)': 'en-AU-TinaNeural',
|
| 44 |
'انگلیسی (کانادا) - کلارا (زن)': 'en-CA-ClaraNeural', 'انگلیسی (کانادا) - لیام (مرد)': 'en-CA-LiamNeural',
|
| 45 |
-
'انگلیسی (کانادا) - کوبی (مرد)': 'en-CA-CobiNeural', 'انگلیسی (کانادا) - ژان (مرد)': 'en-CA-JeanNeural',
|
| 46 |
'انگلیسی (ایرلند) - امیلی (زن)': 'en-IE-EmilyNeural', 'انگلیسی (ایرلند) - کانر (مرد)': 'en-IE-ConnorNeural',
|
| 47 |
'انگلیسی (هند) - نیرجا (زن)': 'en-IN-NeerjaNeural', 'انگلیسی (هند) - پرابهات (مرد)': 'en-IN-PrabhatNeural',
|
| 48 |
-
'انگلیسی (هند) - میتالی (زن)': 'en-IN-MithaliNeural',
|
| 49 |
'انگلیسی (آفریقای جنوبی) - لیا (زن)': 'en-ZA-LeahNeural', 'انگلیسی (آفریقای جنوبی) - لوک (مرد)': 'en-ZA-LukeNeural',
|
| 50 |
'انگلیسی (هنگ کنگ) - یان (زن)': 'en-HK-YanNeural', 'انگلیسی (هنگ کنگ) - سم (مرد)': 'en-HK-SamNeural',
|
| 51 |
'انگلیسی (نیوزیلند) - هیزل (زن)': 'en-NZ-HazelNeural', 'انگلیسی (نیوزیلند) - میچل (مرد)': 'en-NZ-MitchellNeural',
|
|
@@ -56,267 +58,381 @@ language_dict_persian_keys = {
|
|
| 56 |
'انگلیسی (تانزانیا) - ایمانی (زن)': 'en-TZ-ImaniNeural', 'انگلیسی (تانزانیا) - الیمو (مرد)': 'en-TZ-ElimuNeural',
|
| 57 |
}
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
if not
|
| 62 |
-
if not text or not text.strip(): return "خطا: متنی برای ترجمه وارد نشده.", None
|
| 63 |
try:
|
| 64 |
prompt = f"Translate the following Persian text to {target_language}. Provide only the translated English text, naturally and fluently, without any extra phrases, explanations, or markdown formatting. Be concise and accurate.\n\nPersian: \"{text}\"\n{target_language}:"
|
| 65 |
response = await model.generate_content_async(prompt)
|
| 66 |
translated_text = response.text.strip()
|
| 67 |
if translated_text.lower().startswith(f"{target_language.lower()}:"):
|
| 68 |
translated_text = translated_text[len(target_language)+1:].strip()
|
| 69 |
-
return "
|
| 70 |
-
except Exception as e: return f"
|
| 71 |
|
| 72 |
-
async def
|
|
|
|
|
|
|
|
|
|
| 73 |
try:
|
| 74 |
-
if not text_to_speak or not text_to_speak.strip(): return "خطای TTS: متن خالی.", None
|
| 75 |
-
voice_id = language_dict_persian_keys.get(tts_voice_key)
|
| 76 |
-
if voice_id is None: return f"خطای TTS: صدای '{tts_voice_key}' نامعتبر.", None
|
| 77 |
-
if not voice_id.startswith("en-"): return f"خطای TTS: صدای '{tts_voice_key}' انگلیسی نیست.", None
|
| 78 |
rate_str, volume_str, pitch_str = f"{int(rate):+g}%", f"{int(volume):+g}%", f"{int(pitch):+g}Hz"
|
| 79 |
communicate = edge_tts.Communicate(text_to_speak, voice_id, rate=rate_str, volume=volume_str, pitch=pitch_str)
|
| 80 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file: tmp_path = tmp_file.name
|
| 81 |
await communicate.save(tmp_path)
|
| 82 |
-
return "TTS
|
| 83 |
-
except
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
def
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
loop
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
def translate_and_speak_sync_wrapper(persian_text, english_tts_voice_key, rate, volume, pitch):
|
| 95 |
-
|
| 96 |
-
|
|
|
|
| 97 |
if not GOOGLE_API_KEY or not model:
|
| 98 |
-
|
|
|
|
| 99 |
if not persian_text or not persian_text.strip():
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
if not
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
FLY_WHITE_HEX = "#FFFFFF"
|
| 118 |
-
FLY_BORDER_COLOR_HEX = "#D1D5DB"
|
| 119 |
-
FLY_INPUT_BG_HEX = "#
|
|
|
|
| 120 |
|
| 121 |
app_theme = gr.themes.Soft(
|
| 122 |
primary_hue=gr.themes.colors.blue,
|
| 123 |
secondary_hue=gr.themes.colors.emerald,
|
| 124 |
neutral_hue=gr.themes.colors.slate,
|
| 125 |
font=[gr.themes.GoogleFont("Vazirmatn"), "Arial", "sans-serif"],
|
| 126 |
-
).set(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
custom_css = f"""
|
| 129 |
-
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@400;500;600;700&display=swap');
|
| 130 |
-
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap');
|
| 131 |
-
@import url('https://fonts.googleapis.com/css2?family=
|
| 132 |
-
|
| 133 |
:root {{
|
| 134 |
--fly-primary: {FLY_PRIMARY_COLOR_HEX}; --fly-secondary: {FLY_SECONDARY_COLOR_HEX};
|
| 135 |
--fly-accent: {FLY_ACCENT_COLOR_HEX}; --fly-text: {FLY_TEXT_COLOR_HEX};
|
| 136 |
-
--fly-
|
| 137 |
-
--fly-
|
| 138 |
-
--fly-
|
| 139 |
-
--
|
| 140 |
-
--
|
| 141 |
-
--
|
| 142 |
-
--
|
| 143 |
-
|
| 144 |
-
body {{
|
| 145 |
-
font-family: 'Vazirmatn', 'Rubik', 'Poppins', sans-serif; direction: rtl;
|
| 146 |
-
background-color: var(--fly-light-bg); color: var(--fly-text);
|
| 147 |
-
line-height: 1.6; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale;
|
| 148 |
-
padding: 0.5rem; /* پدینگ اولیه برای موبایل */
|
| 149 |
-
}}
|
| 150 |
-
.gradio-container {{ /* استایل پایه برای موبایل */
|
| 151 |
-
max-width: 100% !important; width: 100% !important;
|
| 152 |
-
margin: 0 auto !important; padding: 1rem 0.8rem !important; /* پدینگ کمتر برای موبایل */
|
| 153 |
-
border-radius: var(--fly-radius-lg) !important; box-shadow: var(--fly-shadow-md) !important;
|
| 154 |
-
background-color: var(--fly-white) !important; overflow: hidden;
|
| 155 |
}}
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
background-image: linear-gradient(130deg, var(--fly-primary) 0%, var(--fly-secondary) 100%);
|
| 159 |
-
color: white; border-radius: var(--fly-radius-md);
|
| 160 |
-
margin: -0.8rem -0.8rem 1.5rem -0.8rem; box-shadow: var(--fly-shadow-sm);
|
| 161 |
-
}}
|
| 162 |
-
.app-header h1 {{
|
| 163 |
-
color: white !important; font-size: 1.6em !important; font-weight: 600 !important;
|
| 164 |
-
margin: 0 0 0.3rem 0; font-family: 'Poppins', 'Vazirmatn', sans-serif;
|
| 165 |
-
}}
|
| 166 |
-
.app-header p {{ color: rgba(255,255,255,0.9) !important; font-size: 0.9em !important; margin-top: 0; font-weight: 400;}}
|
| 167 |
-
|
| 168 |
-
.gr-form {{ gap: 0.8rem !important; }} /* فاصله کمتر بین المانهای فرم در موبایل */
|
| 169 |
|
| 170 |
-
.
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
|
|
|
| 175 |
}}
|
| 176 |
-
.
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
}}
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
width: 100% !important; /* عرض کامل در موبایل */
|
| 185 |
}}
|
| 186 |
-
.gr-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
}}
|
| 192 |
-
.gr-input
|
|
|
|
|
|
|
| 193 |
border-color: var(--fly-primary) !important;
|
| 194 |
-
box-shadow: 0 0 0 3px rgba(
|
| 195 |
}}
|
| 196 |
-
.gr-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
}}
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
}}
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
| 209 |
}}
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
.api-warning-message {{
|
| 216 |
-
background-color: #FFFBEB !important; /* amber-50 */ color: #92400E !important; /* amber-700 */
|
| 217 |
-
padding: 0.7rem 1rem !important; border-radius: var(--fly-radius-sm) !important;
|
| 218 |
-
border: 1px solid #FDE68A !important; /* amber-200 */ text-align: center !important;
|
| 219 |
-
margin-bottom: 1rem !important; font-size: 0.85em !important;
|
| 220 |
}}
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
}}
|
| 227 |
-
|
| 228 |
-
|
|
|
|
| 229 |
}}
|
| 230 |
|
| 231 |
-
/*
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
|
|
|
| 239 |
}}
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
}}
|
| 249 |
"""
|
| 250 |
|
| 251 |
-
# --- انتخاب صدای پیشفرض انگلیسی (بدون تغییر) ---
|
| 252 |
default_english_tts_voice = 'انگلیسی (آمریکا) - جنی (زن)'
|
| 253 |
-
if not language_dict_persian_keys:
|
| 254 |
-
|
| 255 |
-
default_english_tts_voice = None
|
| 256 |
-
elif default_english_tts_voice not in language_dict_persian_keys:
|
| 257 |
-
default_english_tts_voice = list(language_dict_persian_keys.keys())[0]
|
| 258 |
|
| 259 |
-
# --- ساخت رابط کاربری Gradio (بدون تغییر در منطق، فقط کلاسها و ساختار HTML ممکن است کمی فرق کند) ---
|
| 260 |
with gr.Blocks(theme=app_theme, css=custom_css, title="آموزش زبان فلای") as demo:
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
value=default_english_tts_voice, label=dropdown_label, interactive=bool(language_dict_persian_keys))
|
| 286 |
-
|
| 287 |
-
with gr.Accordion("⚙️ تنظیمات پیشرفته صدا", open=False):
|
| 288 |
-
with gr.Row(): # برای چیدمان بهتر اسلایدرها در آکاردئون
|
| 289 |
-
rate_slider = gr.Slider(minimum=-100, maximum=100, step=1, value=0, label="سرعت", scale=1)
|
| 290 |
-
volume_slider = gr.Slider(minimum=-100, maximum=100, step=1, value=0, label="حجم", scale=1)
|
| 291 |
-
pitch_slider = gr.Slider(minimum=-50, maximum=50, step=1, value=0, label="گام", scale=2)
|
| 292 |
|
| 293 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
v1=voice_keys[0]; v2=voice_keys[min(1,num_voices-1)]; v3=voice_keys[min(8,num_voices-1)] # صدای بریتانیایی
|
| 306 |
-
example_list = [
|
| 307 |
-
["این یک کتاب جالب است.", v1, 0,0,0],
|
| 308 |
-
["فردا هوا چطور خواهد بود؟", v2, 0,0,0],
|
| 309 |
-
["من به کمک شما نیاز دارم.", v3, -10,0,0]]
|
| 310 |
-
|
| 311 |
-
if example_list:
|
| 312 |
-
gr.Examples(examples=example_list,
|
| 313 |
-
inputs=[input_text_persian, language_dropdown_tts_english, rate_slider, volume_slider, pitch_slider],
|
| 314 |
-
outputs=[output_text_translated, output_audio],
|
| 315 |
-
fn=translate_and_speak_sync_wrapper,
|
| 316 |
-
cache_examples=os.getenv("GRADIO_CACHE_EXAMPLES","False").lower()=="true", label="💡 نمونهها")
|
| 317 |
-
|
| 318 |
-
gr.Markdown("<p class='app-footer'>آموزش زبان فلای - یادگیری لذتبخش</p>")
|
| 319 |
|
| 320 |
if __name__ == "__main__":
|
| 321 |
-
if not language_dict_persian_keys:
|
| 322 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, debug=
|
|
|
|
| 6 |
import threading
|
| 7 |
import os
|
| 8 |
import google.generativeai as genai
|
| 9 |
+
import logging
|
| 10 |
+
|
| 11 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(threadName)s - %(message)s')
|
| 12 |
+
logging.info(f"Gradio version: {gr.__version__}")
|
| 13 |
|
|
|
|
| 14 |
GOOGLE_API_KEY = os.environ.get('GOOGLE_API_KEY')
|
| 15 |
model = None
|
| 16 |
if GOOGLE_API_KEY:
|
| 17 |
try:
|
| 18 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 19 |
model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
| 20 |
+
logging.info("سرویس Gemini با موفقیت پیکربندی شد.")
|
| 21 |
except Exception as e:
|
| 22 |
+
logging.error(f"خطا در پیکربندی Gemini: {e}", exc_info=True)
|
|
|
|
| 23 |
else:
|
| 24 |
+
logging.warning("کلید API گوگل (GOOGLE_API_KEY) تنظیم نشده است.")
|
| 25 |
|
| 26 |
+
# --- دیکشنری کامل صداهای انگلیسی (همانند قبل) ---
|
| 27 |
language_dict_persian_keys = {
|
| 28 |
'انگلیسی (آمریکا) - جنی (زن)': 'en-US-JennyNeural', 'انگلیسی (آمریکا) - گای (مرد)': 'en-US-GuyNeural',
|
| 29 |
+
'انگلیسی (آمریکا) - آنا (زن، صدای کودک)': 'en-US-AnaNeural', 'انگلیسی (آمریکا) - آریا (زن)': 'en-US-AriaNeural',
|
| 30 |
'انگلیسی (آمریکا) - کریستوفر (مرد)': 'en-US-ChristopherNeural', 'انگلیسی (آمریکا) - اریک (مرد)': 'en-US-EricNeural',
|
| 31 |
'انگلیسی (آمریکا) - میشل (زن)': 'en-US-MichelleNeural', 'انگلیسی (آمریکا) - راجر (مرد)': 'en-US-RogerNeural',
|
| 32 |
'انگلیسی (آمریکا) - استفانی (زن)': 'en-US-SteffanNeural', 'انگلیسی (آمریکا) - جیسون (مرد)': 'en-US-JasonNeural',
|
| 33 |
'انگلیسی (آمریکا) - براندون (مرد)': 'en-US-BrandonNeural', 'انگلیسی (آمریکا) - الیزابت (زن)': 'en-US-ElizabethNeural',
|
| 34 |
+
'انگلیسی (آمریکا) - دیویس (مرد، جدید)': 'en-US-DavisNeural', 'انگلیسی (آمریکا) - جین (زن، جدید)': 'en-US-JaneNeural',
|
| 35 |
+
'انگلیسی (آمریکا) - نانسی (زن، جدید)': 'en-US-NancyNeural', 'انگلیسی (آمریکا) - سارا (زن، جدید)': 'en-US-SaraNeural',
|
| 36 |
+
'انگلیسی (آمریکا) - تونی (مرد، جدید)': 'en-US-TonyNeural',
|
| 37 |
'انگلیسی (بریتانیا) - لیبی (زن)': 'en-GB-LibbyNeural', 'انگلیسی (بریتانیا) - میزی (زن)': 'en-GB-MaisieNeural',
|
| 38 |
'انگلیسی (بریتانیا) - رایان (مرد)': 'en-GB-RyanNeural', 'انگلیسی (بریتانیا) - سونیا (زن)': 'en-GB-SoniaNeural',
|
| 39 |
+
'انگلیسی (بریتانیا) - توماس (مرد)': 'en-GB-ThomasNeural', 'انگلیسی (بریتانیا) - میا (زن، جدید)': 'en-GB-MiaNeural',
|
| 40 |
+
'انگلیسی (بریتانیا) - نوآ (مرد، جدید)': 'en-GB-NoahNeural', 'انگلیسی (بریتانیا) - الیور (مرد، جدید)': 'en-GB-OliverNeural',
|
| 41 |
+
'انگلیسی (بریتانیا) - اولیویا (زن، جدید)': 'en-GB-OliviaNeural',
|
| 42 |
'انگلیسی (استرالیا) - ناتاشا (زن)': 'en-AU-NatashaNeural', 'انگلیسی (استرالیا) - ویلیام (مرد)': 'en-AU-WilliamNeural',
|
| 43 |
+
'انگلیسی (استرالیا) - آنت (زن، جدید)': 'en-AU-AnnetteNeural', 'انگلیسی (استرالیا) - کارلی (زن، جدید)': 'en-AU-CarlyNeural',
|
| 44 |
+
'انگلیسی (استرالیا) - کن (مرد، جدید)': 'en-AU-KenNeural', 'انگلیسی (استرالیا) - کیمبرلی (زن، جدید)': 'en-AU-KimNeural',
|
| 45 |
+
'انگلیسی (استرالیا) - تینا (زن، جدید)': 'en-AU-TinaNeural',
|
| 46 |
'انگلیسی (کانادا) - کلارا (زن)': 'en-CA-ClaraNeural', 'انگلیسی (کانادا) - لیام (مرد)': 'en-CA-LiamNeural',
|
| 47 |
+
'انگلیسی (کانادا) - کوبی (مرد، جدید)': 'en-CA-CobiNeural', 'انگلیسی (کانادا) - ژان (مرد، جدید)': 'en-CA-JeanNeural',
|
| 48 |
'انگلیسی (ایرلند) - امیلی (زن)': 'en-IE-EmilyNeural', 'انگلیسی (ایرلند) - کانر (مرد)': 'en-IE-ConnorNeural',
|
| 49 |
'انگلیسی (هند) - نیرجا (زن)': 'en-IN-NeerjaNeural', 'انگلیسی (هند) - پرابهات (مرد)': 'en-IN-PrabhatNeural',
|
| 50 |
+
'انگلیسی (هند) - میتالی (زن، جدید)': 'en-IN-MithaliNeural',
|
| 51 |
'انگلیسی (آفریقای جنوبی) - لیا (زن)': 'en-ZA-LeahNeural', 'انگلیسی (آفریقای جنوبی) - لوک (مرد)': 'en-ZA-LukeNeural',
|
| 52 |
'انگلیسی (هنگ کنگ) - یان (زن)': 'en-HK-YanNeural', 'انگلیسی (هنگ کنگ) - سم (مرد)': 'en-HK-SamNeural',
|
| 53 |
'انگلیسی (نیوزیلند) - هیزل (زن)': 'en-NZ-HazelNeural', 'انگلیسی (نیوزیلند) - میچل (مرد)': 'en-NZ-MitchellNeural',
|
|
|
|
| 58 |
'انگلیسی (تانزانیا) - ایمانی (زن)': 'en-TZ-ImaniNeural', 'انگلیسی (تانزانیا) - الیمو (مرد)': 'en-TZ-ElimuNeural',
|
| 59 |
}
|
| 60 |
|
| 61 |
+
async def translate_text_gemini_async(text, target_language="English"):
|
| 62 |
+
if not model: logging.error("Gemini model not loaded."); return "Error: Translation service unavailable.", None
|
| 63 |
+
if not text or not text.strip(): logging.warning("Translate: Empty input text."); return "Error: Input text is empty.", None
|
|
|
|
| 64 |
try:
|
| 65 |
prompt = f"Translate the following Persian text to {target_language}. Provide only the translated English text, naturally and fluently, without any extra phrases, explanations, or markdown formatting. Be concise and accurate.\n\nPersian: \"{text}\"\n{target_language}:"
|
| 66 |
response = await model.generate_content_async(prompt)
|
| 67 |
translated_text = response.text.strip()
|
| 68 |
if translated_text.lower().startswith(f"{target_language.lower()}:"):
|
| 69 |
translated_text = translated_text[len(target_language)+1:].strip()
|
| 70 |
+
return "Translation successful", translated_text
|
| 71 |
+
except Exception as e: logging.error(f"Gemini translation error: {e}", exc_info=True); return f"Translation error: {type(e).__name__}", None
|
| 72 |
|
| 73 |
+
async def text_to_speech_edge_tts_async(text_to_speak, tts_voice_key, rate, volume, pitch):
|
| 74 |
+
if not text_to_speak or not text_to_speak.strip(): logging.warning("TTS: Empty input text."); return "TTS Error: Input text is empty.", None
|
| 75 |
+
voice_id = language_dict_persian_keys.get(tts_voice_key)
|
| 76 |
+
if voice_id is None: logging.error(f"TTS: Voice key '{tts_voice_key}' not found."); return f"TTS Error: Voice '{tts_voice_key}' not found.", None
|
| 77 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
rate_str, volume_str, pitch_str = f"{int(rate):+g}%", f"{int(volume):+g}%", f"{int(pitch):+g}Hz"
|
| 79 |
communicate = edge_tts.Communicate(text_to_speak, voice_id, rate=rate_str, volume=volume_str, pitch=pitch_str)
|
| 80 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file: tmp_path = tmp_file.name
|
| 81 |
await communicate.save(tmp_path)
|
| 82 |
+
return "TTS successful", tmp_path
|
| 83 |
+
except Exception as e: logging.error(f"Edge-TTS error: {e}", exc_info=True); return f"TTS Error: {type(e).__name__}", None
|
| 84 |
+
|
| 85 |
+
_thread_local = threading.local()
|
| 86 |
+
|
| 87 |
+
def get_event_loop():
|
| 88 |
+
if not hasattr(_thread_local, 'loop') or _thread_local.loop.is_closed(): # Check if closed
|
| 89 |
+
_thread_local.loop = asyncio.new_event_loop()
|
| 90 |
+
logging.info(f"Created new event loop for thread {threading.get_ident()}")
|
| 91 |
+
|
| 92 |
+
try:
|
| 93 |
+
asyncio.set_event_loop(_thread_local.loop)
|
| 94 |
+
except RuntimeError as e:
|
| 95 |
+
if "cannot be called from a running event loop" not in str(e).lower() and \
|
| 96 |
+
"There is no current event loop in thread" not in str(e): # More specific check
|
| 97 |
+
logging.warning(f"RuntimeError setting event loop (possibly already running or other issue): {e}")
|
| 98 |
+
# Ensure our loop is the current one if possible, especially if one wasn't set.
|
| 99 |
+
if asyncio.get_event_loop_policy().get_event_loop() is not _thread_local.loop:
|
| 100 |
+
try:
|
| 101 |
+
asyncio.set_event_loop(_thread_local.loop)
|
| 102 |
+
except RuntimeError as e_retry: # Catch if setting again also fails
|
| 103 |
+
logging.warning(f"Retry setting event loop failed: {e_retry}")
|
| 104 |
+
return _thread_local.loop
|
| 105 |
|
| 106 |
def translate_and_speak_sync_wrapper(persian_text, english_tts_voice_key, rate, volume, pitch):
|
| 107 |
+
logging.info(f"Wrapper called. Text: '{persian_text[:30]}...' Voice: {english_tts_voice_key}")
|
| 108 |
+
loop = get_event_loop()
|
| 109 |
+
|
| 110 |
if not GOOGLE_API_KEY or not model:
|
| 111 |
+
msg = "خطا: سرویس ترجمه پیکربندی نشده است. لطفاً از تنظیم GOOGLE_API_KEY در Secrets اطمینان حاصل کنید."
|
| 112 |
+
logging.error(msg); return msg, None
|
| 113 |
if not persian_text or not persian_text.strip():
|
| 114 |
+
msg = "لطفاً متن فارسی را برای ترجمه وارد کنید."
|
| 115 |
+
logging.warning(msg); return msg, None
|
| 116 |
+
try:
|
| 117 |
+
translation_status_msg, translated_text = loop.run_until_complete(
|
| 118 |
+
translate_text_gemini_async(persian_text)
|
| 119 |
+
)
|
| 120 |
+
if "error" in translation_status_msg.lower() or not translated_text or "خطا" in translation_status_msg.lower() :
|
| 121 |
+
err_msg = f"ترجمه ناموفق: {translation_status_msg} {translated_text or ''}"
|
| 122 |
+
logging.error(err_msg); return err_msg, None
|
| 123 |
+
|
| 124 |
+
tts_status_msg, audio_path = loop.run_until_complete(
|
| 125 |
+
text_to_speech_edge_tts_async(translated_text, english_tts_voice_key, rate, volume, pitch)
|
| 126 |
+
)
|
| 127 |
+
if "error" in tts_status_msg.lower() or not audio_path or "خطا" in tts_status_msg.lower():
|
| 128 |
+
err_msg = f"{translated_text}\n(خطای TTS: {tts_status_msg})"
|
| 129 |
+
logging.error(err_msg); return err_msg, None
|
| 130 |
+
|
| 131 |
+
logging.info(f"عملیات موفق. مسیر صوت: {audio_path}")
|
| 132 |
+
return translated_text, audio_path
|
| 133 |
+
except RuntimeError as e: # Catch specific runtime errors from asyncio
|
| 134 |
+
if "cannot be started when it is already running" in str(e) or \
|
| 135 |
+
"This event loop is already running" in str(e):
|
| 136 |
+
logging.error(f"Asyncio runtime error (loop already running): {e}", exc_info=True)
|
| 137 |
+
# Attempt to run in a new loop for this specific case, as a fallback
|
| 138 |
+
# This is a bit of a workaround for complex Gradio/asyncio interactions
|
| 139 |
+
new_loop = asyncio.new_event_loop()
|
| 140 |
+
asyncio.set_event_loop(new_loop)
|
| 141 |
+
try:
|
| 142 |
+
logging.info("Retrying with a new event loop due to runtime error.")
|
| 143 |
+
translation_status_msg, translated_text = new_loop.run_until_complete(
|
| 144 |
+
translate_text_gemini_async(persian_text)
|
| 145 |
+
)
|
| 146 |
+
if "error" in translation_status_msg.lower() or not translated_text or "خطا" in translation_status_msg.lower() :
|
| 147 |
+
err_msg = f"ترجمه ناموفق (retry): {translation_status_msg} {translated_text or ''}"
|
| 148 |
+
logging.error(err_msg); return err_msg, None
|
| 149 |
+
|
| 150 |
+
tts_status_msg, audio_path = new_loop.run_until_complete(
|
| 151 |
+
text_to_speech_edge_tts_async(translated_text, english_tts_voice_key, rate, volume, pitch)
|
| 152 |
+
)
|
| 153 |
+
if "error" in tts_status_msg.lower() or not audio_path or "خطا" in tts_status_msg.lower():
|
| 154 |
+
err_msg = f"{translated_text}\n(خطای TTS (retry): {tts_status_msg})"
|
| 155 |
+
logging.error(err_msg); return err_msg, None
|
| 156 |
+
|
| 157 |
+
logging.info(f"عملیات موفق (retry). مسیر صوت: {audio_path}")
|
| 158 |
+
return translated_text, audio_path
|
| 159 |
+
finally:
|
| 160 |
+
new_loop.close()
|
| 161 |
+
else:
|
| 162 |
+
logging.error(f"خطای غیرمنتظره در wrapper (RuntimeError): {e}", exc_info=True)
|
| 163 |
+
return f"خطای داخلی سرور (RuntimeError): {type(e).__name__}", None
|
| 164 |
+
except Exception as e:
|
| 165 |
+
logging.error(f"خطای غیرمنتظره در wrapper: {e}", exc_info=True)
|
| 166 |
+
return f"خطای داخلی سرور: {type(e).__name__}", None
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
FLY_PRIMARY_COLOR_HEX = "#2563EB"
|
| 170 |
+
FLY_SECONDARY_COLOR_HEX = "#059669"
|
| 171 |
+
FLY_ACCENT_COLOR_HEX = "#EA580C" # Brighter orange for more pop
|
| 172 |
+
FLY_TEXT_COLOR_HEX = "#1F2937"
|
| 173 |
+
FLY_SUBTLE_TEXT_HEX = "#4B5563" # Darker for better contrast
|
| 174 |
+
FLY_LIGHT_BACKGROUND_HEX = "#F3F4F6" # Slightly off-white
|
| 175 |
FLY_WHITE_HEX = "#FFFFFF"
|
| 176 |
+
FLY_BORDER_COLOR_HEX = "#D1D5DB"
|
| 177 |
+
FLY_INPUT_BG_HEX = "#FFFFFF"
|
| 178 |
+
FLY_PANEL_BG_HEX = "#E0F2FE" # Light blue panel for translated text
|
| 179 |
|
| 180 |
app_theme = gr.themes.Soft(
|
| 181 |
primary_hue=gr.themes.colors.blue,
|
| 182 |
secondary_hue=gr.themes.colors.emerald,
|
| 183 |
neutral_hue=gr.themes.colors.slate,
|
| 184 |
font=[gr.themes.GoogleFont("Vazirmatn"), "Arial", "sans-serif"],
|
| 185 |
+
).set(
|
| 186 |
+
body_background_fill=FLY_LIGHT_BACKGROUND_HEX,
|
| 187 |
+
# Adding some more theme variables for consistency
|
| 188 |
+
input_background_fill="white",
|
| 189 |
+
button_primary_background_fill=FLY_ACCENT_COLOR_HEX,
|
| 190 |
+
button_primary_text_color="white",
|
| 191 |
+
)
|
| 192 |
|
| 193 |
custom_css = f"""
|
| 194 |
+
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;600;700&display=swap');
|
| 195 |
+
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap');
|
| 196 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
|
|
|
|
| 197 |
:root {{
|
| 198 |
--fly-primary: {FLY_PRIMARY_COLOR_HEX}; --fly-secondary: {FLY_SECONDARY_COLOR_HEX};
|
| 199 |
--fly-accent: {FLY_ACCENT_COLOR_HEX}; --fly-text: {FLY_TEXT_COLOR_HEX};
|
| 200 |
+
--fly-subtle-text: {FLY_SUBTLE_TEXT_HEX}; --fly-light-bg: {FLY_LIGHT_BACKGROUND_HEX};
|
| 201 |
+
--fly-white: {FLY_WHITE_HEX}; --fly-border: {FLY_BORDER_COLOR_HEX};
|
| 202 |
+
--fly-input-bg: {FLY_INPUT_BG_HEX}; --fly-panel-bg: {FLY_PANEL_BG_HEX};
|
| 203 |
+
--global-font: 'Vazirmatn', 'Inter', 'Poppins', Arial, sans-serif;
|
| 204 |
+
--english-font: 'Poppins', 'Inter', Arial, sans-serif;
|
| 205 |
+
--border-radius-sm: 6px; --border-radius-md: 10px; --border-radius-lg: 16px; /* Added border radius variables */
|
| 206 |
+
--shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
|
| 207 |
+
--shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
}}
|
| 209 |
+
body {{font-family: var(--global-font); direction: rtl; background-color: var(--fly-light-bg); color: var(--fly-text); line-height: 1.7; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; font-size: 15px;}}
|
| 210 |
+
.gradio-container {{max-width: 100% !important; width: 100% !important; min-height: 100vh; margin: 0 auto !important; padding: 0 !important; border-radius: 0 !important; box-shadow: none !important; background-color: var(--fly-light-bg) !important; display: flex; flex-direction: column;}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
+
.app-header-fly {{
|
| 213 |
+
text-align: center; padding: 24px 15px; /* Increased padding */
|
| 214 |
+
background-image: linear-gradient(135deg, var(--fly-primary) 0%, var(--fly-secondary) 100%);
|
| 215 |
+
color: white; margin: 0 0 1.5rem 0; /* Increased bottom margin */
|
| 216 |
+
border-bottom-left-radius: var(--border-radius-lg); border-bottom-right-radius: var(--border-radius-lg); /* Rounded bottom corners */
|
| 217 |
+
box-shadow: var(--shadow-md);
|
| 218 |
}}
|
| 219 |
+
.app-header-fly h1 {{font-size: 1.8em !important; font-weight: 700 !important; margin: 0 0 6px 0; font-family: var(--english-font); letter-spacing: -0.5px;}}
|
| 220 |
+
.app-header-fly p {{font-size: 0.95em !important; margin-top: 4px; font-weight: 400; color: rgba(255, 255, 255, 0.9) !important;}}
|
| 221 |
+
|
| 222 |
+
.main-content-area-fly {{
|
| 223 |
+
flex-grow: 1; padding: 0 1rem; /* Add horizontal padding to content area */
|
| 224 |
+
max-width: 700px; /* Max width for content on larger screens */
|
| 225 |
+
width: 100%;
|
| 226 |
+
margin: 0 auto; /* Center content area */
|
| 227 |
}}
|
| 228 |
+
|
| 229 |
+
/* --- Input and Dropdown Styling --- */
|
| 230 |
+
.gr-input > label > span, .gr-dropdown > label > span, .gr-slider > label > span {{ /* Target label text */
|
| 231 |
+
font-weight: 600 !important; color: var(--fly-subtle-text) !important; font-size: 0.9em !important; margin-bottom: 6px !important; display: inline-block;
|
|
|
|
| 232 |
}}
|
| 233 |
+
.gr-input > label + div > textarea,
|
| 234 |
+
.gr-dropdown > label + div > div > input, /* For editable dropdowns if any, and the display part */
|
| 235 |
+
.gr-dropdown select {{ /* Target actual select */
|
| 236 |
+
border-radius: var(--border-radius-md) !important;
|
| 237 |
+
border: 1px solid var(--fly-border) !important;
|
| 238 |
+
font-size: 0.95em !important;
|
| 239 |
+
background-color: var(--fly-input-bg) !important;
|
| 240 |
+
padding: 10px 14px !important;
|
| 241 |
+
line-height: 1.6;
|
| 242 |
+
width: 100% !important;
|
| 243 |
+
box-sizing: border-box !important;
|
| 244 |
+
font-family: var(--global-font) !important;
|
| 245 |
+
color: var(--fly-text) !important;
|
| 246 |
+
transition: border-color 0.2s ease, box-shadow 0.2s ease;
|
| 247 |
}}
|
| 248 |
+
.gr-input > label + div > textarea:focus,
|
| 249 |
+
.gr-dropdown > label + div > div > input:focus,
|
| 250 |
+
.gr-dropdown select:focus {{
|
| 251 |
border-color: var(--fly-primary) !important;
|
| 252 |
+
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.2) !important;
|
| 253 |
}}
|
| 254 |
+
.gr-dropdown > label + div {{ position: relative; }} /* Container for potential custom arrow */
|
| 255 |
+
.gr-dropdown > label + div::after {{ /* Basic custom arrow (optional, can be tricky) */
|
| 256 |
+
/* content: '▼'; font-size: 0.7em; color: var(--fly-subtle-text);
|
| 257 |
+
position: absolute; left: 12px; top: 50%; transform: translateY(-50%); pointer-events: none; */
|
| 258 |
}}
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
/* Special styling for translated text output */
|
| 262 |
+
.translated-text-output .gr-input > label + div > textarea {{
|
| 263 |
+
background-color: var(--fly-panel-bg) !important;
|
| 264 |
+
border-color: #A5D5FE !important;
|
| 265 |
+
min-height: 100px;
|
| 266 |
+
font-family: var(--english-font) !important;
|
| 267 |
+
font-size: 1em !important;
|
| 268 |
+
color: var(--fly-text) !important;
|
| 269 |
+
box-shadow: inset 0 1px 2px rgba(0,0,0,0.05);
|
| 270 |
}}
|
| 271 |
+
|
| 272 |
+
/* Accordion for voice settings */
|
| 273 |
+
.gr-accordion.gradio-accordion > button.gr-button {{ /* Target accordion button */
|
| 274 |
+
font-weight: 500 !important; padding: 10px 12px !important; border-radius: var(--border-radius-md) !important;
|
| 275 |
+
background-color: #E5E7EB !important; color: var(--fly-text) !important;
|
| 276 |
+
border: 1px solid #D1D5DB !important; font-size: 0.9em;
|
| 277 |
+
margin-bottom: 0.5rem; /* Space before panel opens */
|
| 278 |
}}
|
| 279 |
+
.gr-accordion.gradio-accordion > div.gr-panel {{ /* Panel inside accordion */
|
| 280 |
+
border-radius: var(--border-radius-md) !important; border: 1px solid var(--fly-border) !important;
|
| 281 |
+
background-color: var(--fly-white) !important;
|
| 282 |
+
padding: 1rem !important;
|
| 283 |
+
margin-top: 0 !important; box-shadow: var(--shadow-sm);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
}}
|
| 285 |
+
|
| 286 |
+
.gr-slider {{ padding: 4px 0 !important;}}
|
| 287 |
+
.gr-slider label span {{font-size: 0.85em !important; color: var(--fly-subtle-text);}}
|
| 288 |
+
|
| 289 |
+
/* Main Submit Button */
|
| 290 |
+
.fly-submit-button-container .gr-button {{
|
| 291 |
+
background: linear-gradient(135deg, var(--fly-accent), #F97316) !important; /* Orange gradient */
|
| 292 |
+
margin-top: 1rem !important; padding: 12px 24px !important;
|
| 293 |
+
transition: all 0.25s ease-in-out !important; color: white !important; font-weight: 600 !important;
|
| 294 |
+
border-radius: var(--border-radius-md) !important; border: none !important;
|
| 295 |
+
box-shadow: var(--shadow-md) !important; width: 100% !important;
|
| 296 |
+
font-size: 1em !important; display: flex; align-items: center; justify-content: center;
|
| 297 |
+
letter-spacing: 0.5px;
|
| 298 |
}}
|
| 299 |
+
.fly-submit-button-container .gr-button:hover {{
|
| 300 |
+
background: linear-gradient(135deg, #F97316, var(--fly-accent)) !important; /* Slightly different on hover */
|
| 301 |
+
transform: translateY(-2px) !important; box-shadow: 0 6px 12px -2px rgba(234, 88, 12, 0.4) !important;
|
| 302 |
}}
|
| 303 |
|
| 304 |
+
/* Examples Section */
|
| 305 |
+
.examples-section-fly .gr-button.gr-button-tool {{ /* Example buttons */
|
| 306 |
+
background-color: #E0E7FF !important; color: var(--fly-primary) !important;
|
| 307 |
+
border-radius: var(--border-radius-sm) !important; font-size: 0.8em !important; padding: 6px 10px !important;
|
| 308 |
+
border: 1px solid #C7D2FE !important; margin: 2px !important;
|
| 309 |
+
transition: background-color 0.2s ease, transform 0.1s ease;
|
| 310 |
+
}}
|
| 311 |
+
.examples-section-fly .gr-button.gr-button-tool:hover {{
|
| 312 |
+
background-color: #C7D2FE !important; transform: translateY(-1px);
|
| 313 |
}}
|
| 314 |
+
.examples-section-fly > .gr-label > span {{ /* Label for "💡 نمونه" */
|
| 315 |
+
font-size: 1.1em !important; font-weight: 600; color: var(--fly-text);
|
| 316 |
+
margin-bottom: 0.5rem !important;
|
| 317 |
+
}}
|
| 318 |
+
|
| 319 |
+
|
| 320 |
+
.custom-hr-fly {{height: 1px; background-color: var(--fly-border); margin: 1.5rem 0; border: none;}}
|
| 321 |
+
.api-warning-message-fly {{background-color: #FFFBEB !important; color: #92400E !important; padding: 10px 12px !important; border-radius: var(--border-radius-sm) !important; border: 1px solid #FDE68A !important; text-align: center !important; margin: 0 0 1rem 0 !important; font-size: 0.9em !important;}}
|
| 322 |
+
.app-footer-fly {{text-align:center; font-size:0.8em; color: var(--fly-subtle-text); margin-top:2rem; padding-bottom: 1rem;}}
|
| 323 |
+
footer, .gradio-footer {{display: none !important;}}
|
| 324 |
+
|
| 325 |
+
/* Audio player adjustments */
|
| 326 |
+
.gr-audio audio {{ border-radius: var(--border-radius-md); box-shadow: var(--shadow-sm); }}
|
| 327 |
+
|
| 328 |
+
@media (min-width: 640px) {{
|
| 329 |
+
body {{font-size: 16px;}}
|
| 330 |
+
.gradio-container {{ /* On desktop, container has a card-like feel */
|
| 331 |
+
/* max-width: is handled by main-content-area-fly */
|
| 332 |
+
padding: 0 !important; /* No padding on overall container */
|
| 333 |
+
margin: 0 auto !important;
|
| 334 |
+
background-color: var(--fly-light-bg) !important;
|
| 335 |
+
border-radius: 0 !important;
|
| 336 |
+
box-shadow: none !important;
|
| 337 |
+
}}
|
| 338 |
+
.app-header-fly {{ /* Header still full-width or constrained by its parent */
|
| 339 |
+
border-bottom-left-radius: var(--border-radius-lg);
|
| 340 |
+
border-bottom-right-radius: var(--border-radius-lg);
|
| 341 |
+
margin-left: auto; margin-right: auto; /* Centering if it has a max-width */
|
| 342 |
+
}}
|
| 343 |
+
.app-header-fly h1 {{ font-size: 2em !important; }} .app-header-fly p {{ font-size: 1em !important; }}
|
| 344 |
+
|
| 345 |
+
.main-content-area-fly {{padding: 0 1.5rem 1.5rem 1.5rem;}} /* More padding on desktop */
|
| 346 |
+
|
| 347 |
+
.api-warning-message-fly {{font-size: 0.9em;}}
|
| 348 |
+
|
| 349 |
+
.main-inputs-row-fly {{ display: flex; flex-direction: row; gap: 1.5rem; align-items: flex-start;}}
|
| 350 |
+
.main-inputs-row-fly > .gr-column:nth-child(1) {{ flex: 3; }} /* Input column wider */
|
| 351 |
+
.main-inputs-row-fly > .gr-column:nth-child(2) {{ flex: 2; }} /* Output column */
|
| 352 |
+
|
| 353 |
+
.fly-submit-button-container .gr-button {{ width: auto !important; align-self: flex-start; padding: 12px 30px !important; font-size: 1.05em !important;}}
|
| 354 |
+
|
| 355 |
+
.gr-input > label + div > textarea, .gr-dropdown select, .translated-text-output .gr-input > label + div > textarea {{ font-size: 1em; }}
|
| 356 |
+
.gr-input > label > span, .gr-dropdown > label > span, .gr-slider > label > span {{ font-size: 0.95em; }}
|
| 357 |
+
.app-footer-fly {{font-size:0.85em;}}
|
| 358 |
}}
|
| 359 |
"""
|
| 360 |
|
|
|
|
| 361 |
default_english_tts_voice = 'انگلیسی (آمریکا) - جنی (زن)'
|
| 362 |
+
if not language_dict_persian_keys or default_english_tts_voice not in language_dict_persian_keys:
|
| 363 |
+
default_english_tts_voice = list(language_dict_persian_keys.keys())[0] if language_dict_persian_keys else "No voices"
|
|
|
|
|
|
|
|
|
|
| 364 |
|
|
|
|
| 365 |
with gr.Blocks(theme=app_theme, css=custom_css, title="آموزش زبان فلای") as demo:
|
| 366 |
+
# Use an elem_id or elem_classes for the main container if you want to style it uniquely
|
| 367 |
+
# For example: with gr.Blocks(..., elem_id="fly-app-container")
|
| 368 |
+
|
| 369 |
+
gr.HTML(f"""<div class="app-header-fly"><h1>Fly Language Learning</h1><p>ترجمه فارسی به انگلیسی و تلفظ با لهجه دلخواه</p></div>""")
|
| 370 |
+
|
| 371 |
+
with gr.Column(elem_classes=["main-content-area-fly"]): # Central content area
|
| 372 |
+
if not GOOGLE_API_KEY or not model:
|
| 373 |
+
missing_key_msg = "⚠️ **ترجمه غیرفعال:** "
|
| 374 |
+
if not GOOGLE_API_KEY: missing_key_msg += "کلید API گوگل تنظیم نشده."
|
| 375 |
+
elif not model: missing_key_msg += "خطا در مدل Gemini."
|
| 376 |
+
gr.Markdown(f"<div class='api-warning-message-fly'>{missing_key_msg} لطفاً Secrets را بررسی کنید.</div>")
|
| 377 |
|
| 378 |
+
with gr.Row(elem_classes=["main-inputs-row-fly"]): # Row for input and output columns
|
| 379 |
+
with gr.Column(scale=3): # Input column (takes more space)
|
| 380 |
+
input_text_persian = gr.Textbox(
|
| 381 |
+
lines=4, label="📝 متن فارسی خود را وارد کنید:", placeholder="مثال: سلام، حال شما چطور است؟",
|
| 382 |
+
elem_id="persian-input-textbox" # For potential specific styling
|
| 383 |
+
)
|
| 384 |
+
language_dropdown_tts_english = gr.Dropdown(
|
| 385 |
+
choices=list(language_dict_persian_keys.keys()) if language_dict_persian_keys else ["لیست صداها خالی است"],
|
| 386 |
+
value=default_english_tts_voice, label="🗣️ گوینده و لهجه انگلیسی را انتخاب کنید:", interactive=bool(language_dict_persian_keys)
|
| 387 |
+
)
|
| 388 |
+
with gr.Accordion("⚙️ تنظیمات پیشرفته صدا", open=False):
|
| 389 |
+
rate_slider = gr.Slider(-100, 100, 0, step=10, label="سرعت گفتار")
|
| 390 |
+
volume_slider = gr.Slider(-100, 100, 0, step=10, label="بلندی صدا")
|
| 391 |
+
pitch_slider = gr.Slider(-50, 50, 0, step=5, label="زیر و بمی صدا (Pitch)")
|
| 392 |
+
|
| 393 |
+
with gr.Group(elem_classes=["fly-submit-button-container"]):
|
| 394 |
+
submit_button = gr.Button("🚀 ترجمه و پخش صدا", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
|
| 396 |
+
with gr.Column(scale=2): # Output column
|
| 397 |
+
output_text_translated = gr.Textbox(
|
| 398 |
+
label="📜 ترجمه انگلیسی:", interactive=False, lines=5, placeholder="اینجا نتیجه ترجمه نمایش داده میشود...",
|
| 399 |
+
elem_classes=["translated-text-output"] # Class for specific styling
|
| 400 |
+
)
|
| 401 |
+
output_audio = gr.Audio(label="🎧 فایل صوتی:", type="filepath", format="mp3", interactive=False)
|
| 402 |
|
| 403 |
+
gr.HTML("<hr class='custom-hr-fly'>")
|
| 404 |
+
|
| 405 |
+
example_list = []
|
| 406 |
+
if language_dict_persian_keys and default_english_tts_voice != "No voices" :
|
| 407 |
+
num_voices = len(language_dict_persian_keys)
|
| 408 |
+
voice_keys = list(language_dict_persian_keys.keys())
|
| 409 |
+
# Try to pick diverse examples
|
| 410 |
+
voice1_idx, voice2_idx, voice3_idx = 0, min(num_voices // 2, num_voices-1), min(num_voices-1, num_voices-1) # Ensure valid indices
|
| 411 |
+
example_list = [
|
| 412 |
+
["سلام، قیمت این چقدر است؟", voice_keys[voice1_idx], 0, 0, 0],
|
| 413 |
+
["میتوانید نزدیکترین ایستگاه اتوبوس را به من نشان دهید؟", voice_keys[voice2_idx], 0, 0, 0],
|
| 414 |
+
["ببخشید، من انگلیسی خوب صحبت نمیکنم.", voice_keys[voice3_idx], -10, 0, 0],
|
| 415 |
+
]
|
| 416 |
+
|
| 417 |
+
if example_list:
|
| 418 |
+
# Added elem_classes for styling the examples section
|
| 419 |
+
gr.Examples(
|
| 420 |
+
examples=example_list,
|
| 421 |
+
inputs=[input_text_persian, language_dropdown_tts_english, rate_slider, volume_slider, pitch_slider],
|
| 422 |
+
outputs=[output_text_translated, output_audio],
|
| 423 |
+
fn=translate_and_speak_sync_wrapper,
|
| 424 |
+
label="💡 چند نمونه برای شروع:", # More descriptive label
|
| 425 |
+
elem_classes=["examples-section-fly"]
|
| 426 |
+
)
|
| 427 |
+
|
| 428 |
+
gr.Markdown("<p class='app-footer-fly'>ساخته شده با ❤️ برای یادگیری زبان فلای © ۲۰۲۴</p>")
|
| 429 |
|
| 430 |
+
submit_button.click(
|
| 431 |
+
fn=translate_and_speak_sync_wrapper,
|
| 432 |
+
inputs=[input_text_persian, language_dropdown_tts_english, rate_slider, volume_slider, pitch_slider],
|
| 433 |
+
outputs=[output_text_translated, output_audio]
|
| 434 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
|
| 436 |
if __name__ == "__main__":
|
| 437 |
+
if not language_dict_persian_keys: logging.critical("دیکشنری صداها خالی است! برنامه ممکن است به درستی کار نکند.")
|
| 438 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, debug=True)
|