| 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 |
| |
| |
| 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 |
| ) |
|
|
| |
| iface.launch() |
|
|