Ezmary commited on
Commit
45f8390
·
verified ·
1 Parent(s): 128e6c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -15
app.py CHANGED
@@ -121,6 +121,7 @@ def enhance_animation_prompt():
121
  }
122
 
123
  prompt_lower = user_prompt_text.lower()
 
124
  if any(keyword in prompt_lower for keyword in FORBIDDEN_KEYWORDS):
125
  logging.warning(f"NSFW prompt blocked by local keyword filter: '{user_prompt_text}'")
126
  return jsonify({
@@ -132,7 +133,7 @@ def enhance_animation_prompt():
132
  image_bytes = image_file.read()
133
  pil_image = Image.open(io.BytesIO(image_bytes))
134
 
135
- # 3. تعریف پرامپت‌های سیستم کامل و بدون خلاصه سازی
136
  gemini_master_prompt_initial = f"""
137
  You are an expert AI Animation Planner. Your absolute highest priority is to faithfully and creatively execute the user's specific request. You are not just an artist; you are a technical problem solver.
138
 
@@ -249,7 +250,7 @@ Generate the following two keys in English.
249
  """
250
 
251
  final_system_prompt = gemini_master_prompt_extension if is_extension_request else gemini_master_prompt_initial
252
-
253
  # 4. منطق تلاش مجدد (Retry Logic) با انتخاب تصادفی کلید
254
  max_retries = 5
255
  gemini_response = None
@@ -279,13 +280,12 @@ Generate the following two keys in English.
279
 
280
  response_data = json.loads(response.text)
281
 
282
- # اگر پاسخ NSFW بود، بلافاصله برگردانیم (نیاز به تلاش مجدد نیست چون مشکل از پرامپت است)
283
  if response_data.get("reason") == "NSFW_DETECTED" or "error" in response_data:
284
  return jsonify(response_data), 400
285
 
286
  gemini_response = response_data
287
  logging.info("✅ Gemini Success!")
288
- break # موفقیت آمیز بود، خروج از حلقه
289
 
290
  except Exception as e:
291
  logging.warning(f"⚠️ Attempt {attempt + 1} failed: {str(e)}")
@@ -295,30 +295,35 @@ Generate the following two keys in English.
295
  logging.warning("⚠️ No Gemini keys configured. Skipping directly to fallback.")
296
 
297
  # 5. منطق فال‌بک (Fallback) به Deep Translator
298
- # اگر بعد از 5 تلاش جیمینای موفق نشد یا کلیدی نبود
299
  if not gemini_response:
300
- logging.info("🔄 Switching to Fallback: Deep Translator...")
301
  try:
302
- text_to_translate = user_prompt_text if user_prompt_text.strip() else "Cinematic motion, high quality"
303
-
304
- # استفاده از مترجم گوگل
 
 
 
305
  translator = GoogleTranslator(source='auto', target='en')
306
- translated_text = translator.translate(text_to_translate)
307
 
308
- # ساخت دستی JSON
309
  base_style = ", cinematic, photorealistic, high detail, smooth motion, 8k"
310
  extension_style = ", continuing smoothly from the last frame, seamless transition, cinematic, 8k"
311
  style_suffix = extension_style if is_extension_request else base_style
312
 
 
 
313
  gemini_response = {
314
- "animation_prompt": f"{translated_text}{style_suffix}",
315
  "negative_prompt": "ugly, deformed, noisy, blurry, distorted, grainy, shaking, jittery, flickering, unnatural movement, static image, watermark, text, signature, cartoon, anime, 3d render"
316
  }
317
- logging.info(f"✅ Fallback translation successful: {gemini_response['animation_prompt']}")
318
 
319
  except Exception as e:
320
- logging.error(f"❌ Fallback failed: {str(e)}")
321
- # آخرین سنگر: استفاده از متن خام انگلیسی (اگر کاربر انگلیسی زده باشد) یا یک متن پیش‌فرض
322
  gemini_response = {
323
  "animation_prompt": "High quality cinematic animation, smooth motion, photorealistic, 8k",
324
  "negative_prompt": "ugly, deformed, noisy, blurry, distorted"
 
121
  }
122
 
123
  prompt_lower = user_prompt_text.lower()
124
+
125
  if any(keyword in prompt_lower for keyword in FORBIDDEN_KEYWORDS):
126
  logging.warning(f"NSFW prompt blocked by local keyword filter: '{user_prompt_text}'")
127
  return jsonify({
 
133
  image_bytes = image_file.read()
134
  pil_image = Image.open(io.BytesIO(image_bytes))
135
 
136
+ # 3. پرامپت‌های سیستم (کامل و بدون تغییر)
137
  gemini_master_prompt_initial = f"""
138
  You are an expert AI Animation Planner. Your absolute highest priority is to faithfully and creatively execute the user's specific request. You are not just an artist; you are a technical problem solver.
139
 
 
250
  """
251
 
252
  final_system_prompt = gemini_master_prompt_extension if is_extension_request else gemini_master_prompt_initial
253
+
254
  # 4. منطق تلاش مجدد (Retry Logic) با انتخاب تصادفی کلید
255
  max_retries = 5
256
  gemini_response = None
 
280
 
281
  response_data = json.loads(response.text)
282
 
 
283
  if response_data.get("reason") == "NSFW_DETECTED" or "error" in response_data:
284
  return jsonify(response_data), 400
285
 
286
  gemini_response = response_data
287
  logging.info("✅ Gemini Success!")
288
+ break # موفقیت آمیز، خروج از حلقه
289
 
290
  except Exception as e:
291
  logging.warning(f"⚠️ Attempt {attempt + 1} failed: {str(e)}")
 
295
  logging.warning("⚠️ No Gemini keys configured. Skipping directly to fallback.")
296
 
297
  # 5. منطق فال‌بک (Fallback) به Deep Translator
298
+ # اگر بعد از 5 تلاش متغیر gemini_response همچنان خالی بود
299
  if not gemini_response:
300
+ logging.warning("🛑 All Gemini attempts failed. Initiating Fallback (Deep Translator).")
301
  try:
302
+ source_text = user_prompt_text.strip()
303
+ # اگر پرامپت خالی بود، یک متن پیشفرض برای ترجمه در نظر بگیر
304
+ if not source_text:
305
+ source_text = "Beautiful cinematic scene"
306
+
307
+ logging.info("Translating text via GoogleTranslator...")
308
  translator = GoogleTranslator(source='auto', target='en')
309
+ translated_text = translator.translate(source_text)
310
 
311
+ # افزودن استایل‌ها
312
  base_style = ", cinematic, photorealistic, high detail, smooth motion, 8k"
313
  extension_style = ", continuing smoothly from the last frame, seamless transition, cinematic, 8k"
314
  style_suffix = extension_style if is_extension_request else base_style
315
 
316
+ final_prompt = f"{translated_text}{style_suffix}"
317
+
318
  gemini_response = {
319
+ "animation_prompt": final_prompt,
320
  "negative_prompt": "ugly, deformed, noisy, blurry, distorted, grainy, shaking, jittery, flickering, unnatural movement, static image, watermark, text, signature, cartoon, anime, 3d render"
321
  }
322
+ logging.info(f"✅ Fallback translation successful. Prompt: {final_prompt}")
323
 
324
  except Exception as e:
325
+ logging.error(f"❌ Fallback translation failed: {str(e)}")
326
+ # آخرین گزینه در صورت شکست ترجمه
327
  gemini_response = {
328
  "animation_prompt": "High quality cinematic animation, smooth motion, photorealistic, 8k",
329
  "negative_prompt": "ugly, deformed, noisy, blurry, distorted"