Rex4u commited on
Commit
0872885
·
verified ·
1 Parent(s): 3774935

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ from typing import Tuple
4
+ import torch
5
+ from transformers import pipeline
6
+
7
+ # Lightweight captioning model for free CPU tier
8
+ captioner = pipeline(
9
+ "image-to-text",
10
+ model="Salesforce/blip-image-captioning-base",
11
+ device_map="auto"
12
+ )
13
+
14
+ STYLE_PRESETS = {
15
+ "Cinematic Realism (Default)": {
16
+ "mood": "moody, atmospheric, high drama",
17
+ "camera": "full-frame DSLR, 85mm prime, f/1.8, ISO 200, 1/160s",
18
+ "lighting": "cinematic key light, soft rim light, natural bounce, volumetric haze",
19
+ "composition": "rule of thirds, shallow depth of field, subject isolation, bokeh background",
20
+ "render": "Ultra-realistic, 8K, photoreal skin texture, detailed fabric, microcontrast",
21
+ "transform": "no AI distortions, no extra fingers, no artifacts"
22
+ },
23
+ "Clean Editorial": {
24
+ "mood": "elegant, clean, minimal",
25
+ "camera": "medium-format look, 80mm, f/4, ISO 100, 1/125s",
26
+ "lighting": "soft studio key, large diffuser, gentle fill, crisp highlights",
27
+ "composition": "centered portrait, negative space, symmetrical balance",
28
+ "render": "magazine editorial, ultra-sharp, color accurate, calibrated white balance",
29
+ "transform": "no over-smoothing, no plastic skin"
30
+ },
31
+ "Neon Night": {
32
+ "mood": "vibrant, cyberpunk glow, dusk city vibes",
33
+ "camera": "35mm, f/1.4, ISO 800, 1/60s, handheld",
34
+ "lighting": "neon signage, mixed practicals, wet streets reflections",
35
+ "composition": "leading lines, dynamic angle, motion blur trails",
36
+ "render": "high dynamic range, punchy contrast, vivid color separation",
37
+ "transform": "no banding, no color bleed"
38
+ }
39
+ }
40
+
41
+ def build_prompt(caption:str, face_ref:bool, preset_key:str, subject_override:str) -> str:
42
+ preset = STYLE_PRESETS.get(preset_key, STYLE_PRESETS["Cinematic Realism (Default)"])
43
+ subject = subject_override.strip() if subject_override else caption
44
+ face_line = "Use the uploaded photo strictly as the facial reference; match identity 100% with no changes." if face_ref else ""
45
+ template = f"""
46
+ # Image-to-Prompt (Cinematic Style) — for Nano
47
+ Subject & Action: {subject}.
48
+ Context: Derived directly from the uploaded image; maintain scene fidelity and realism.
49
+
50
+ Identity: {face_line}
51
+ Mood & Atmosphere: {preset['mood']}.
52
+ Camera & Lens: {preset['camera']}.
53
+ Lighting: {preset['lighting']}.
54
+ Composition: {preset['composition']}.
55
+ Detail & Rendering: {preset['render']}.
56
+ Quality Flags: {preset['transform']}.
57
+
58
+ Output Requirements:
59
+ - Hyper-detailed yet concise single-paragraph prompt (no line breaks).
60
+ - Avoid generic words like "beautiful", "nice"; use specific visual cues instead.
61
+ - No camera model names; keep it brand-agnostic.
62
+ - Do not add elements not present in the image unless implied by context.
63
+ - If the photo is a close portrait, emphasize skin texture, catchlights, and depth of field.
64
+ - If the photo is a full body or scene, emphasize environment, pose, and lighting interactions.
65
+
66
+ Final Prompt (one paragraph):
67
+ """
68
+ final = f"{subject}. Mood: {preset['mood']}. Camera: {preset['camera']}. Lighting: {preset['lighting']}. Composition: {preset['composition']}. Rendering: {preset['render']}. {face_line}".strip()
69
+ return template.strip(), final
70
+
71
+ def infer(image:Image.Image, face_ref:bool, preset:str, subject_override:str) -> Tuple[str, str]:
72
+ if image is None:
73
+ return "Please upload an image.", ""
74
+ result = captioner(image)[0]["generated_text"]
75
+ header, final_prompt = build_prompt(result, face_ref, preset, subject_override)
76
+ return header, final_prompt
77
+
78
+ with gr.Blocks(css="footer {visibility: hidden}") as demo:
79
+ gr.Markdown("## 🖼️ Image → Prompt (Free) — Mobile Friendly\nUpload a photo and get a polished cinematic-style prompt instantly.")
80
+ with gr.Row():
81
+ with gr.Column():
82
+ img = gr.Image(type="pil", label="Upload Photo", sources=["upload", "clipboard"], height=320)
83
+ face_ref = gr.Checkbox(label="Use uploaded face as identity reference", value=True)
84
+ preset = gr.Dropdown(label="Style preset", choices=list(STYLE_PRESETS.keys()), value="Cinematic Realism (Default)")
85
+ subject_override = gr.Textbox(label="Optional: Replace subject with your own wording", placeholder="e.g., A confident young man in a leather jacket under moody studio light")
86
+ btn = gr.Button("Generate Prompt")
87
+ with gr.Column():
88
+ header = gr.Textbox(label="Instructions (copy if needed)", lines=14)
89
+ final_prompt = gr.Textbox(label="Final One-Paragraph Prompt", lines=6)
90
+ btn.click(infer, inputs=[img, face_ref, preset, subject_override], outputs=[header, final_prompt])
91
+ gr.Markdown("Tip: For richer captions, switch model to 'Salesforce/blip-image-captioning-large' inside the code.")
92
+
93
+ if __name__ == "__main__":
94
+ demo.launch()