Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
|
| 6 |
+
# Define style tokens or modifiers
|
| 7 |
+
styles = {
|
| 8 |
+
"None": " ",
|
| 9 |
+
"Hyper-Realistic": " hyper-realistic",
|
| 10 |
+
"Realistic": " realistic",
|
| 11 |
+
"Flux": " flux",
|
| 12 |
+
"Abstract": " abstract",
|
| 13 |
+
"Cartoon": " cartoon",
|
| 14 |
+
"Ghibli": " ghibli style",
|
| 15 |
+
"Anime": " anime style",
|
| 16 |
+
"Watercolor": " watercolor painting",
|
| 17 |
+
"Pixel Art": " pixel art",
|
| 18 |
+
"Sketch": " sketch",
|
| 19 |
+
"Digital Art": " digital art",
|
| 20 |
+
"Oil Painting": " oil painting",
|
| 21 |
+
"Pastel": " pastel painting",
|
| 22 |
+
"Photorealistic": " photorealistic",
|
| 23 |
+
"Sci-Fi": " sci-fi",
|
| 24 |
+
"Fantasy": " fantasy",
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
def generate_image(prompt, style):
|
| 28 |
+
# Append the style token to the prompt
|
| 29 |
+
style_token = styles.get(style, "")
|
| 30 |
+
full_prompt = prompt + style_token
|
| 31 |
+
|
| 32 |
+
# Set image parameters
|
| 33 |
+
width = 1024
|
| 34 |
+
height = 1024
|
| 35 |
+
seed = 42
|
| 36 |
+
model = "flux" # You can adjust the model if needed
|
| 37 |
+
|
| 38 |
+
# Construct the URL for pollinations.ai
|
| 39 |
+
image_url = f"https://pollinations.ai/p/{full_prompt}?width={width}&height={height}&seed={seed}&model={model}"
|
| 40 |
+
|
| 41 |
+
# Download the image using requests
|
| 42 |
+
response = requests.get(image_url)
|
| 43 |
+
if response.status_code == 200:
|
| 44 |
+
image = Image.open(BytesIO(response.content))
|
| 45 |
+
return image
|
| 46 |
+
else:
|
| 47 |
+
return None
|
| 48 |
+
|
| 49 |
+
def crop_watermark(pil_img):
|
| 50 |
+
"""
|
| 51 |
+
Crop the image from the bottom to remove the watermark.
|
| 52 |
+
This function assumes the generated image is 1024x1024,
|
| 53 |
+
and crops it to a height of 960 pixels (removing the bottom 64 pixels).
|
| 54 |
+
"""
|
| 55 |
+
width, height = pil_img.size # Should be 1024 x 1024
|
| 56 |
+
# Crop from the top left (0,0) to (width, 960)
|
| 57 |
+
cropped_img = pil_img.crop((0, 0, width, 960))
|
| 58 |
+
return cropped_img
|
| 59 |
+
|
| 60 |
+
# Gradio interface function
|
| 61 |
+
def interface(prompt, style):
|
| 62 |
+
image = generate_image(prompt, style)
|
| 63 |
+
if image is None:
|
| 64 |
+
return "Error generating image."
|
| 65 |
+
# Crop out the bottom portion with the watermark
|
| 66 |
+
image_cropped = crop_watermark(image)
|
| 67 |
+
return image_cropped
|
| 68 |
+
|
| 69 |
+
# Define style options for the radio buttons
|
| 70 |
+
style_options = list(styles.keys())
|
| 71 |
+
|
| 72 |
+
# Build Gradio interface using Blocks
|
| 73 |
+
with gr.Blocks() as demo:
|
| 74 |
+
gr.Markdown("## Canvasio: AI Image Generator")
|
| 75 |
+
with gr.Row():
|
| 76 |
+
prompt_input = gr.Textbox(label="Enter your prompt", placeholder="A beautiful landscape")
|
| 77 |
+
with gr.Row():
|
| 78 |
+
style_choice = gr.Radio(choices=style_options, label="Select image style", value="Flux")
|
| 79 |
+
generate_btn = gr.Button("Generate Image")
|
| 80 |
+
output_image = gr.Image(label="Generated Image")
|
| 81 |
+
|
| 82 |
+
generate_btn.click(fn=interface, inputs=[prompt_input, style_choice], outputs=output_image)
|
| 83 |
+
|
| 84 |
+
demo.launch()
|