Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,21 +5,20 @@ from io import BytesIO
|
|
5 |
import os
|
6 |
from deep_translator import GoogleTranslator
|
7 |
|
8 |
-
# آدرس API مدل
|
9 |
API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
|
10 |
headers = {
|
11 |
-
"Authorization": f"Bearer {os.environ.get('HF_TOKEN')}"
|
12 |
}
|
13 |
|
14 |
def generate_image(prompt):
|
15 |
try:
|
16 |
-
# ترجمه
|
17 |
translated_prompt = GoogleTranslator(source='auto', target='en').translate(prompt)
|
18 |
print(f"🈯 Prompt ترجمهشده: {translated_prompt}")
|
19 |
except Exception as e:
|
20 |
return f"❌ خطا در ترجمه پرامپت:\n{str(e)}"
|
21 |
|
22 |
-
# ساخت بدنه درخواست
|
23 |
payload = {
|
24 |
"inputs": translated_prompt,
|
25 |
"options": {"wait_for_model": True}
|
@@ -29,28 +28,24 @@ def generate_image(prompt):
|
|
29 |
# ارسال درخواست به API
|
30 |
response = requests.post(API_URL, headers=headers, json=payload)
|
31 |
content_type = response.headers.get("content-type", "")
|
32 |
-
|
33 |
-
# چاپ در لاگ برای بررسی بیشتر
|
34 |
print("🔍 Content-Type:", content_type)
|
35 |
-
if "image" not in content_type:
|
36 |
-
print("🧾 پاسخ متنی:", response.text[:1000])
|
37 |
|
38 |
-
# بررسی اگر پاسخ تصویر بود
|
39 |
if "image" in content_type:
|
40 |
image = Image.open(BytesIO(response.content))
|
41 |
return image
|
42 |
else:
|
43 |
-
|
|
|
44 |
except Exception as e:
|
45 |
return f"❌ خطا در ارتباط با API:\n{str(e)}"
|
46 |
|
47 |
-
# رابط
|
48 |
iface = gr.Interface(
|
49 |
fn=generate_image,
|
50 |
-
inputs=gr.Textbox(label="
|
51 |
outputs="image",
|
52 |
title="🎨 تولید تصویر دیجی نورون | www.diginoron.com",
|
53 |
-
description="
|
54 |
)
|
55 |
|
56 |
iface.launch()
|
|
|
5 |
import os
|
6 |
from deep_translator import GoogleTranslator
|
7 |
|
8 |
+
# آدرس صحیح API مدل قابل اجرا در Hugging Face
|
9 |
API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
|
10 |
headers = {
|
11 |
+
"Authorization": f"Bearer {os.environ.get('HF_TOKEN')}"
|
12 |
}
|
13 |
|
14 |
def generate_image(prompt):
|
15 |
try:
|
16 |
+
# ترجمه فارسی به انگلیسی
|
17 |
translated_prompt = GoogleTranslator(source='auto', target='en').translate(prompt)
|
18 |
print(f"🈯 Prompt ترجمهشده: {translated_prompt}")
|
19 |
except Exception as e:
|
20 |
return f"❌ خطا در ترجمه پرامپت:\n{str(e)}"
|
21 |
|
|
|
22 |
payload = {
|
23 |
"inputs": translated_prompt,
|
24 |
"options": {"wait_for_model": True}
|
|
|
28 |
# ارسال درخواست به API
|
29 |
response = requests.post(API_URL, headers=headers, json=payload)
|
30 |
content_type = response.headers.get("content-type", "")
|
|
|
|
|
31 |
print("🔍 Content-Type:", content_type)
|
|
|
|
|
32 |
|
|
|
33 |
if "image" in content_type:
|
34 |
image = Image.open(BytesIO(response.content))
|
35 |
return image
|
36 |
else:
|
37 |
+
print("🧾 پاسخ متنی:", response.text[:1000])
|
38 |
+
return f"⚠️ پاسخ دریافتی تصویر نبود یا مدل هنوز آماده نیست:\n\n{response.text[:1000]}"
|
39 |
except Exception as e:
|
40 |
return f"❌ خطا در ارتباط با API:\n{str(e)}"
|
41 |
|
42 |
+
# رابط کاربری Gradio
|
43 |
iface = gr.Interface(
|
44 |
fn=generate_image,
|
45 |
+
inputs=gr.Textbox(label="توضیحت را بنویس", placeholder="مثلاً: یک روباه در جنگل..."),
|
46 |
outputs="image",
|
47 |
title="🎨 تولید تصویر دیجی نورون | www.diginoron.com",
|
48 |
+
description="توضیحی بنویس تا یک تصویر خلق کنیم! از مدل Stable Diffusion نسخه 1.5 استفاده میشود."
|
49 |
)
|
50 |
|
51 |
iface.launch()
|