ssaeeeeed commited on
Commit
deef217
·
verified ·
1 Parent(s): 049a941

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -68
app.py CHANGED
@@ -5,85 +5,50 @@ import io
5
  from PIL import Image
6
  from deep_translator import GoogleTranslator
7
 
8
- # دریافت توکن از تنظیمات اسپیس
9
  HF_TOKEN = os.getenv("HF_TOKEN")
10
 
11
- # آدرس مدل‌ها
12
- QWEN_URL = "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-7B-Instruct"
13
- IMAGE_MODELS = {
14
- "Flux Schnell (کیفیت بالا)": "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell",
15
- "Stable Diffusion XL": "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
16
- }
17
 
18
- headers = {"Authorization": f"Bearer {HF_TOKEN}"}
19
-
20
- # --- بخش اول: مهندسی پرامپت با Qwen 2.5 ---
21
- def enhance_prompt(user_text):
22
- if not user_text: return "لطفا ابتدا ایده خود را بنویسید."
23
- try:
24
- # ترجمه خودکار به انگلیسی
25
- en_text = GoogleTranslator(source='auto', target='en').translate(user_text)
26
-
27
- # ساخت دستور برای Qwen
28
- prompt_instruction = f"Convert this simple idea into a high-quality AI image generation prompt: '{en_text}'. Focus on lighting, 8k resolution, and artistic style. Provide ONLY the final prompt in English."
29
-
30
- response = requests.post(QWEN_URL, headers=headers, json={"inputs": prompt_instruction}, timeout=15)
31
-
32
- if response.status_code == 200:
33
- result = response.json()[0]['generated_text']
34
- # تمیز کردن خروجی
35
- final_p = result.split("Prompt:")[-1].replace(prompt_instruction, "").strip()
36
- return final_p
37
- return en_text
38
- except Exception as e:
39
- return f"Error: {str(e)}"
40
-
41
- # --- بخش دوم: تولید تصویر ---
42
- def generate_image(prompt, model_key):
43
- if not prompt: return None, "پرامپت خالی است."
44
 
45
  try:
46
- api_url = IMAGE_MODELS[model_key]
47
- response = requests.post(api_url, headers=headers, json={"inputs": prompt}, timeout=60)
 
 
 
 
48
 
49
  if response.status_code == 200:
50
- return Image.open(io.BytesIO(response.content)), "✅ تصویر با موفقیت خلق شد!"
51
- elif response.status_code == 503:
52
- return None, "⏳ مدل در حال بیدار شدن است، چند لحظه دیگر دوباره بزنید."
53
  else:
54
- return None, f"❌ خطا: {response.status_code}"
 
55
  except Exception as e:
56
- return None, f"💥 خطای سیستم: {str(e)}"
57
 
58
- # --- طراحی رابط کاربری ---
59
- with gr.Blocks(theme=gr.themes.Soft(), css="footer {visibility: hidden}") as demo:
60
- gr.Markdown("# 🎨 استودیو تصویرسازی هوشمند (Qwen 2.5 + Flux)")
 
61
 
62
- with gr.Tab("Step 1: آزمایشگاه پرامپت"):
63
- with gr.Row():
64
- with gr.Column():
65
- input_idea = gr.Textbox(label="ایده شما (به فارسی یا انگلیسی)", placeholder="مثلا: شهری در آینده زیر باران")
66
- btn_enhance = gr.Button("✨ مهندسی و غنی‌سازی با Qwen 2.5", variant="primary")
67
- with gr.Column():
68
- output_prompt = gr.Textbox(label="پرامپت حرفه‌ای ساخته شده", lines=5)
69
- btn_transfer = gr.Button("🚀 انتقال به مرحله تولید تصویر", variant="secondary")
70
-
71
- with gr.Tab("Step 2: کارگاه نقاشی"):
72
- with gr.Row():
73
- with gr.Column():
74
- final_prompt = gr.Textbox(label="پرامپت نهایی برای اجرا", lines=5)
75
- model_choice = gr.Dropdown(choices=list(IMAGE_MODELS.keys()), label="انتخاب موتور تصویرساز", value="Flux Schnell (کیفیت بالا)")
76
- btn_gen = gr.Button("🎨 شروع تولید تصویر", variant="primary")
77
- with gr.Column():
78
- output_img = gr.Image(label="نتیجه هنری")
79
- status_msg = gr.Textbox(label="وضعیت", interactive=False)
80
 
81
- # عملکرد دکمه‌ها
82
- btn_enhance.click(fn=enhance_prompt, inputs=input_idea, outputs=output_prompt)
83
-
84
- # دکمه انتقال خودکار بین تب‌ها
85
- btn_transfer.click(lambda x: x, inputs=output_prompt, outputs=final_prompt)
86
-
87
- btn_gen.click(fn=generate_image, inputs=[final_prompt, model_choice], outputs=[output_img, status_msg])
88
 
89
  demo.launch()
 
5
  from PIL import Image
6
  from deep_translator import GoogleTranslator
7
 
8
+ # توکن هاگینگ فیس شما (تنها چیزی که نیاز داری)
9
  HF_TOKEN = os.getenv("HF_TOKEN")
10
 
11
+ # آدرس مدل تصویری که کیفیتی در حد Gemini 2.5 دارد
12
+ MODEL_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
 
 
 
 
13
 
14
+ def gemini_style_gen(user_input):
15
+ if not user_input:
16
+ return None, "لطفاً چیزی بنویسید..."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  try:
19
+ # ۱. ترجمه خودکار (مثل جمینی که همه زبان‌ها را می‌فهمد)
20
+ translated = GoogleTranslator(source='auto', target='en').translate(user_input)
21
+
22
+ # ۲. ارسال مستقیم به قوی‌ترین مدل رایگان فعلی
23
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"}
24
+ response = requests.post(MODEL_URL, headers=headers, json={"inputs": translated}, timeout=60)
25
 
26
  if response.status_code == 200:
27
+ return Image.open(io.BytesIO(response.content)), "✨ تصویر با موفقیت تولید شد."
 
 
28
  else:
29
+ return None, f"خطا در ارتباط: {response.status_code}"
30
+
31
  except Exception as e:
32
+ return None, f"خطای سیستم: {str(e)}"
33
 
34
+ # طراحی محیط ساده و شیک (شبیه به چت جمینی)
35
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
36
+ gr.Markdown("# 🍌 Nano-Interface (Powered by Qwen Logic)")
37
+ gr.Markdown("بدون نیاز به API گوگل، مستقیماً تصویر بسازید.")
38
 
39
+ with gr.Row():
40
+ with gr.Column():
41
+ input_text = gr.Textbox(
42
+ label="از من بخواه هر تصویری که دوست داری بسازم...",
43
+ placeholder="مثلاً: یک موز فضایی با عینک آفتابی",
44
+ lines=3
45
+ )
46
+ submit_btn = gr.Button("تولید تصویر (Gemini Mode)", variant="primary")
47
+
48
+ with gr.Column():
49
+ output_img = gr.Image(label="نتیجه خروجی")
50
+ status_msg = gr.Textbox(label="وضعیت", interactive=False)
 
 
 
 
 
 
51
 
52
+ submit_btn.click(fn=gemini_style_gen, inputs=input_text, outputs=[output_img, status_msg])
 
 
 
 
 
 
53
 
54
  demo.launch()