Upload 2 files
Browse files- app.py +47 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import random
|
| 4 |
+
import tempfile
|
| 5 |
+
from googletrans import Translator
|
| 6 |
+
|
| 7 |
+
translator = Translator()
|
| 8 |
+
|
| 9 |
+
def process(Prompt):
|
| 10 |
+
try:
|
| 11 |
+
# ترجمه متن فارسی به انگلیسی
|
| 12 |
+
text_en = translator.translate(Prompt, src='fa', dest='en').text
|
| 13 |
+
|
| 14 |
+
# درخواست به Pollinations API
|
| 15 |
+
response = requests.get(
|
| 16 |
+
f"https://image.pollinations.ai/prompt/{text_en}?model=flux-realism&width=1024&height=1024&nologo=true&seed={random.randint(0,999999999)}",
|
| 17 |
+
timeout=20
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# بررسی اینکه پاسخ واقعاً تصویر باشه
|
| 21 |
+
content_type = response.headers.get("Content-Type", "")
|
| 22 |
+
if "image" not in content_type:
|
| 23 |
+
return f"❌ API پاسخ تصویر نداد. ممکنه متن یا سرور مشکل داشته باشه."
|
| 24 |
+
|
| 25 |
+
# ذخیره تصویر در فایل موقت
|
| 26 |
+
tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 27 |
+
with open(tmp_file.name, "wb") as f:
|
| 28 |
+
f.write(response.content)
|
| 29 |
+
|
| 30 |
+
return tmp_file.name
|
| 31 |
+
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"❌ خطا: {e}"
|
| 34 |
+
|
| 35 |
+
title = "Pollinations Image Generator"
|
| 36 |
+
description = "Pollinations API + Auto-Translate Persian to English + Randomizer"
|
| 37 |
+
|
| 38 |
+
iface = gr.Interface(
|
| 39 |
+
fn=process,
|
| 40 |
+
inputs=gr.Textbox(lines=2, placeholder="متن خود را وارد کنید..."),
|
| 41 |
+
outputs="image",
|
| 42 |
+
title=title,
|
| 43 |
+
description=description
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# اجرا روی Hugging Face Space
|
| 47 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==3.22.1
|
| 2 |
+
requests
|
| 3 |
+
googletrans==4.0.0-rc1
|