File size: 1,964 Bytes
1c161f6
 
 
 
d33db62
ace8a03
1c161f6
9d5405f
7c967fe
2b9a2c4
9d5405f
2b9a2c4
1c161f6
 
8e811a5
9d5405f
8e811a5
 
 
ace8a03
8e811a5
2b9a2c4
8e811a5
2b9a2c4
1c161f6
 
 
e778a6c
ace8a03
 
e778a6c
 
ace8a03
 
 
 
9d5405f
 
1c161f6
ace8a03
1c161f6
9d5405f
1c161f6
 
9d5405f
1c161f6
ace8a03
9d5405f
1c161f6
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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()