ab / app.py
imparsa's picture
Upload 2 files
b403111 verified
import gradio as gr
import requests
import random
import tempfile
from googletrans import Translator
translator = Translator()
def process(Prompt):
try:
# ترجمه متن فارسی به انگلیسی
text_en = translator.translate(Prompt, src='fa', dest='en').text
# درخواست به Pollinations API
response = requests.get(
f"https://image.pollinations.ai/prompt/{text_en}?model=flux-realism&width=1024&height=1024&nologo=true&seed={random.randint(0,999999999)}",
timeout=20
)
# بررسی اینکه پاسخ واقعاً تصویر باشه
content_type = response.headers.get("Content-Type", "")
if "image" not in content_type:
return f"❌ API پاسخ تصویر نداد. ممکنه متن یا سرور مشکل داشته باشه."
# ذخیره تصویر در فایل موقت
tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
with open(tmp_file.name, "wb") as f:
f.write(response.content)
return tmp_file.name
except Exception as e:
return f"❌ خطا: {e}"
title = "Pollinations Image Generator"
description = "Pollinations API + Auto-Translate Persian to English + Randomizer"
iface = gr.Interface(
fn=process,
inputs=gr.Textbox(lines=2, placeholder="متن خود را وارد کنید..."),
outputs="image",
title=title,
description=description
)
# اجرا روی Hugging Face Space
iface.launch()