import gradio as gr import requests from PIL import Image from io import BytesIO import os from deep_translator import GoogleTranslator # آدرس صحیح API مدل قابل اجرا در Hugging Face API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5" headers = { "Authorization": f"Bearer {os.environ.get('HF_TOKEN')}" } def generate_image(prompt): try: # ترجمه فارسی به انگلیسی translated_prompt = GoogleTranslator(source='auto', target='en').translate(prompt) print(f"🈯 Prompt ترجمه‌شده: {translated_prompt}") except Exception as e: return f"❌ خطا در ترجمه پرامپت:\n{str(e)}" payload = { "inputs": translated_prompt, "options": {"wait_for_model": True} } try: # ارسال درخواست به API response = requests.post(API_URL, headers=headers, json=payload) content_type = response.headers.get("content-type", "") print("🔍 Content-Type:", content_type) if "image" in content_type: image = Image.open(BytesIO(response.content)) return image else: print("🧾 پاسخ متنی:", response.text[:1000]) return f"⚠️ پاسخ دریافتی تصویر نبود یا مدل هنوز آماده نیست:\n\n{response.text[:1000]}" except Exception as e: return f"❌ خطا در ارتباط با API:\n{str(e)}" # رابط کاربری Gradio iface = gr.Interface( fn=generate_image, inputs=gr.Textbox(label="توضیحت را بنویس", placeholder="مثلاً: یک روباه در جنگل..."), outputs="image", title="🎨 تولید تصویر دیجی نورون | www.diginoron.com", description="توضیحی بنویس تا یک تصویر خلق کنیم! از مدل Stable Diffusion نسخه 1.5 استفاده می‌شود." ) iface.launch()