ssaeeeeed commited on
Commit
44fa9d3
·
verified ·
1 Parent(s): 75a9ed1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -12
app.py CHANGED
@@ -3,10 +3,10 @@ import requests
3
  import os
4
  import io
5
  from PIL import Image
 
6
 
7
  HF_TOKEN = os.getenv("HF_TOKEN")
8
 
9
- # لیست مدل‌های تضمین‌شده که در ۹۹٪ مواقع فعال هستند
10
  MODELS = {
11
  "Flux (کیفیت فوق‌العاده)": "black-forest-labs/FLUX.1-schnell",
12
  "SDXL (استاندارد و باکیفیت)": "stabilityai/stable-diffusion-xl-base-1.0",
@@ -18,35 +18,36 @@ def generate_image(prompt, model_name):
18
  return None, "❌ خطا: توکن تنظیم نشده است."
19
 
20
  try:
 
 
 
 
21
  model_id = MODELS.get(model_name)
22
- # آدرس جدید روتر
23
  api_url = f"https://router.huggingface.co/hf-inference/models/{model_id}"
24
  headers = {"Authorization": f"Bearer {HF_TOKEN}"}
25
 
26
- response = requests.post(api_url, headers=headers, json={"inputs": prompt}, timeout=60)
27
 
28
  if response.status_code == 200:
29
  image = Image.open(io.BytesIO(response.content))
30
- return image, f"✅ عالی! تصویر با مدل {model_name} ساخته شد."
31
-
32
- elif response.status_code == 503:
33
- return None, "⏳ سرور مدل کمی شلوغ است. ۵ ثانیه دیگر دوباره تلاش کنید."
34
  else:
35
  return None, f"❓ خطا از سرور: {response.status_code}"
36
 
37
  except Exception as e:
38
  return None, f"💥 خطای سیستمی: {str(e)}"
39
 
 
40
  with gr.Blocks(css="footer {visibility: hidden}", theme=gr.themes.Soft()) as demo:
41
- gr.Markdown("# 🚀 پنل نهایی تولید تصویر")
42
  with gr.Row():
43
  with gr.Column():
44
- input_text = gr.Textbox(label="متن توصیفی شما به انگلیسی")
45
- model_drop = gr.Dropdown(choices=list(MODELS.keys()), label="انتخاب هوش مصنوعی", value="Flux (کیفیت فوق‌العاده)")
46
- btn = gr.Button("تولید تصویر", variant="primary")
47
  with gr.Column():
48
  output_img = gr.Image(label="خروجی")
49
- status_text = gr.Textbox(label="گزارش", interactive=False)
50
 
51
  btn.click(fn=generate_image, inputs=[input_text, model_drop], outputs=[output_img, status_text])
52
 
 
3
  import os
4
  import io
5
  from PIL import Image
6
+ from deep_translator import GoogleTranslator # اضافه کردن مترجم
7
 
8
  HF_TOKEN = os.getenv("HF_TOKEN")
9
 
 
10
  MODELS = {
11
  "Flux (کیفیت فوق‌العاده)": "black-forest-labs/FLUX.1-schnell",
12
  "SDXL (استاندارد و باکیفیت)": "stabilityai/stable-diffusion-xl-base-1.0",
 
18
  return None, "❌ خطا: توکن تنظیم نشده است."
19
 
20
  try:
21
+ # ترجمه خودکار از هر زبانی (از جمله فارسی) به انگلیسی
22
+ translated_prompt = GoogleTranslator(source='auto', target='en').translate(prompt)
23
+ print(f"Original: {prompt} | Translated: {translated_prompt}")
24
+
25
  model_id = MODELS.get(model_name)
 
26
  api_url = f"https://router.huggingface.co/hf-inference/models/{model_id}"
27
  headers = {"Authorization": f"Bearer {HF_TOKEN}"}
28
 
29
+ response = requests.post(api_url, headers=headers, json={"inputs": translated_prompt}, timeout=60)
30
 
31
  if response.status_code == 200:
32
  image = Image.open(io.BytesIO(response.content))
33
+ return image, f"✅ ترجمه شد به: {translated_prompt}"
 
 
 
34
  else:
35
  return None, f"❓ خطا از سرور: {response.status_code}"
36
 
37
  except Exception as e:
38
  return None, f"💥 خطای سیستمی: {str(e)}"
39
 
40
+ # رابط کاربری با پشتیبانی از راست‌به‌چپ
41
  with gr.Blocks(css="footer {visibility: hidden}", theme=gr.themes.Soft()) as demo:
42
+ gr.Markdown("# 🎨 تولید تصویر با پشتیبانی از زبان فارسی")
43
  with gr.Row():
44
  with gr.Column():
45
+ input_text = gr.Textbox(label="توضیح تصویر (فارسی یا انگلیسی)", placeholder="مثلاً: یک گربه فضانورد روی ماه...")
46
+ model_drop = gr.Dropdown(choices=list(MODELS.keys()), label="انتخاب مدل", value="Flux (کیفیت فوق‌العاده)")
47
+ btn = gr.Button("🚀 تولید تصویر", variant="primary")
48
  with gr.Column():
49
  output_img = gr.Image(label="خروجی")
50
+ status_text = gr.Textbox(label="وضعیت و ترجمه", interactive=False)
51
 
52
  btn.click(fn=generate_image, inputs=[input_text, model_drop], outputs=[output_img, status_text])
53