ImageGeneration / app.py
PraneshJs's picture
fixed api call
6d9c208 verified
import gradio as gr
import requests
import random
import os
def process(prompt):
"""
Processes the user's text prompt and generates an image using Pollinations API,
while enforcing universal facts and realism in the output.
"""
# Universal fact / negative guidance prompt
universal_prompt = (
"Make it realistic, consistent with universal facts, and physically possible. "
"Don't generate blurry, low quality, distorted, watermark, text, cropped, "
"deformed, bad anatomy, extra limbs, missing limbs, duplicate objects, pixelated, "
"ugly, disfigured, grainy, noisy, extra moons, duplicate moons, impossible physics."
)
# Combine user prompt with universal guidance
final_prompt = f"{prompt}. {universal_prompt}"
# Generate filename
filename = str(random.randint(111111111, 999999999)) + ".png"
file_path = os.path.join(os.path.dirname(__file__), filename)
# Build API request
api_url = (
"https://image.pollinations.ai/prompt/"
+ final_prompt.replace(" ", "%20")
+ "?model=flux-realism"
+ "&width=2048&height=2048"
+ "&nologo=true"
+ "&seed=" + str(random.randint(0, 999999999))
)
response = requests.get(api_url)
if response.status_code == 200:
with open(file_path, "wb") as f:
f.write(response.content)
return file_path
else:
return f"Error: Could not retrieve image. Status code: {response.status_code}"
# Define the Gradio interface
title = "Pollinations Image Generator"
description = "This app generates images from text using the Pollinations API."
article = "Note: Universal facts and realism are enforced in the generated images."
iface = gr.Interface(
fn=process,
inputs=gr.Textbox(lines=2, placeholder="Enter your prompt here...", label="Prompt"),
outputs=gr.Image(type="filepath", label="Generated Image"),
title=title,
description=description,
article=article
)
iface.launch(server_name="0.0.0.0", server_port=7860, pwa=True)