File size: 5,495 Bytes
e1dd612 900b5d3 e1dd612 900b5d3 e1dd612 900b5d3 01a28d5 e1dd612 01a28d5 39431ec 01a28d5 39431ec 01a28d5 39431ec 01a28d5 39431ec 01a28d5 39431ec 01a28d5 39431ec 01a28d5 39431ec 01a28d5 900b5d3 e1dd612 01a28d5 7cd6338 01a28d5 39431ec 01a28d5 91143ad 01a28d5 39431ec 01a28d5 39431ec 01a28d5 39431ec 01a28d5 39431ec 01a28d5 91143ad 01a28d5 e1dd612 01a28d5 91143ad 01a28d5 7cd6338 01a28d5 e1dd612 01a28d5 39431ec 91143ad 01a28d5 e1dd612 01a28d5 91143ad 01a28d5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | import os
import gradio as gr
try:
from huggingface_hub import InferenceClient
except ImportError:
InferenceClient = None
STYLES = [
"Cinematic",
"Photorealistic",
"Digital Art",
"Fantasy",
"Anime",
"3D Render",
"Editorial Photography",
"Concept Art",
"Minimalist",
"Oil Painting",
]
def local_prompt(idea, style, aspect, mood, detail, camera):
idea = idea.strip()
style = style or "Cinematic"
mood = mood or "Atmospheric"
detail = detail or "highly detailed"
camera = camera or "natural perspective"
return f"""Create a {style.lower()} image of {idea}.
Subject and scene: Clearly depict the central subject with believable anatomy, proportions, materials, and environmental context. Build a coherent setting around the subject with foreground, midground, and background elements that support the story.
Composition: {camera}, strong visual hierarchy, intentional framing, balanced negative space, convincing depth and perspective.
Lighting and atmosphere: {mood.lower()} lighting, natural light falloff, dimensional shadows, subtle atmospheric depth, realistic highlights and reflections where appropriate.
Visual treatment: {detail}, rich textures, refined edges, nuanced surfaces, coherent color relationships, cinematic visual storytelling.
Quality: high visual fidelity, crisp focal detail, realistic spatial relationships, polished professional finish, no accidental text, no watermarks.
Aspect ratio: {aspect}.
"""
def generate(idea, style, aspect, mood, detail, camera, use_ai):
if not idea or not idea.strip():
return "Enter an image idea first."
token = os.getenv("HF_TOKEN")
model = os.getenv("PROMPT_MODEL", "Qwen/Qwen2.5-7B-Instruct")
if use_ai and token and InferenceClient:
try:
client = InferenceClient(model=model, token=token)
system = """You are an expert image-prompt architect. Expand a user's idea into one detailed,
production-ready prompt for an image-generation model. Preserve the user's core intent.
Do not invent major subjects. Include subject, environment, composition, lighting, style,
materials/textures, mood, camera/lens language when useful, quality, and aspect ratio.
Return only the finished prompt, with no preamble or quotation marks."""
user = f"""Idea: {idea}
Style: {style}
Aspect ratio: {aspect}
Mood: {mood}
Detail level: {detail}
Camera/composition: {camera}"""
result = client.chat_completion(
messages=[
{"role": "system", "content": system},
{"role": "user", "content": user},
],
max_tokens=900,
temperature=0.7,
)
return result.choices[0].message.content.strip()
except Exception as e:
return local_prompt(idea, style, aspect, mood, detail, camera) + \
f"\n\n[AI enhancement unavailable; used built-in generator. Error: {type(e).__name__}]"
return local_prompt(idea, style, aspect, mood, detail, camera)
with gr.Blocks(title="Free Image Prompt Generator", theme=gr.themes.Soft()) as demo:
gr.Markdown(
"""# ✦ Free Image Prompt Generator
### Turn a simple idea into a detailed, descriptive image prompt.
Describe what you want to create. The generator expands it into a structured prompt suitable for image-generation models."""
)
with gr.Row():
with gr.Column(scale=2):
idea = gr.Textbox(
label="Your idea",
placeholder="e.g. A lone astronaut exploring an abandoned underwater city",
lines=5,
)
with gr.Row():
style = gr.Dropdown(STYLES, value="Cinematic", label="Style")
aspect = gr.Dropdown(
["1:1", "4:5", "3:4", "16:9", "9:16", "21:9"],
value="16:9",
label="Aspect ratio",
)
with gr.Row():
mood = gr.Textbox(value="Atmospheric", label="Mood / atmosphere")
detail = gr.Textbox(value="highly detailed", label="Detail level")
camera = gr.Textbox(
value="medium-wide cinematic perspective",
label="Camera / composition",
)
use_ai = gr.Checkbox(
label="Use HF AI enhancement when HF_TOKEN is configured",
value=True,
)
generate_btn = gr.Button("✨ Generate Prompt", variant="primary")
with gr.Column(scale=2):
output = gr.Textbox(
label="Generated prompt",
lines=20,
show_copy_button=True,
)
generate_btn.click(
generate,
inputs=[idea, style, aspect, mood, detail, camera, use_ai],
outputs=output,
)
gr.Examples(
examples=[
["A futuristic Brisbane skyline after a tropical storm", "Cinematic", "16:9", "moody and atmospheric", "highly detailed", "wide establishing shot"],
["A tiny robot repairing an old mechanical clock", "3D Render", "1:1", "warm and whimsical", "ultra detailed", "close-up macro composition"],
["A medieval library hidden inside a giant tree", "Fantasy", "4:5", "magical and serene", "richly detailed", "symmetrical interior composition"],
],
inputs=[idea, style, aspect, mood, detail, camera],
)
demo.launch()
|