Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,83 @@ from PIL import Image, ImageDraw, ImageFont
|
|
4 |
import requests
|
5 |
import io
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
CUSTOM_CSS = """
|
8 |
.container { max-width: 1200px; margin: auto; }
|
9 |
.welcome { text-align: center; margin: 20px 0; padding: 20px; background: #1e293b; border-radius: 10px; }
|
@@ -11,57 +88,58 @@ CUSTOM_CSS = """
|
|
11 |
"""
|
12 |
|
13 |
def create_interface():
|
14 |
-
def generate_image(format_size, orientation, prompt, style, quality, creativity,
|
15 |
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
|
16 |
headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_TOKEN')}"}
|
17 |
|
18 |
-
# Dimensions de base optimisées
|
19 |
-
base_width = 1024
|
20 |
-
base_height = 1024
|
21 |
-
|
22 |
-
if orientation == "Portrait":
|
23 |
-
if format_size in ["A4", "A3"]:
|
24 |
-
base_width = 768
|
25 |
-
base_height = 1024
|
26 |
-
else:
|
27 |
-
if format_size in ["A4", "A3"]:
|
28 |
-
base_width = 1024
|
29 |
-
base_height = 768
|
30 |
-
|
31 |
try:
|
32 |
-
#
|
33 |
-
|
34 |
-
|
35 |
-
"Artistique": "artistic poster composition, creative design",
|
36 |
-
"Corporate": "professional business poster, corporate style",
|
37 |
-
"Impact": "bold impactful poster design, attention-grabbing"
|
38 |
-
}
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
payload = {
|
45 |
"inputs": enhanced_prompt,
|
46 |
"parameters": {
|
47 |
-
"negative_prompt":
|
48 |
-
"num_inference_steps":
|
49 |
-
"guidance_scale":
|
50 |
"width": base_width,
|
51 |
"height": base_height
|
52 |
}
|
53 |
}
|
54 |
|
55 |
-
print(f"
|
56 |
|
57 |
-
response = requests.post(API_URL, headers=headers, json=payload, timeout=
|
58 |
|
59 |
if response.status_code == 200:
|
60 |
image = Image.open(io.BytesIO(response.content))
|
61 |
-
return image, "✨
|
62 |
else:
|
63 |
print(f"Erreur API: {response.text}")
|
64 |
-
return None, f"⚠️ Erreur {response.status_code}:
|
65 |
|
66 |
except Exception as e:
|
67 |
print(f"Exception: {str(e)}")
|
@@ -98,23 +176,21 @@ def create_interface():
|
|
98 |
with gr.Row():
|
99 |
with gr.Column():
|
100 |
style = gr.Dropdown(
|
101 |
-
choices=
|
102 |
-
value="
|
103 |
-
label="Style
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
)
|
105 |
|
106 |
prompt = gr.Textbox(
|
107 |
lines=3,
|
108 |
label="Description",
|
109 |
-
placeholder="Décrivez
|
110 |
-
value="professional poster design with clean layout"
|
111 |
-
)
|
112 |
-
|
113 |
-
# Ajout du champ negative_prompt
|
114 |
-
negative_prompt = gr.Textbox(
|
115 |
-
label="Éléments à éviter (optionnel)",
|
116 |
-
placeholder="Ex: flou, texte illisible, distorsions...",
|
117 |
-
lines=2
|
118 |
)
|
119 |
|
120 |
with gr.Group(elem_classes="quality-controls"):
|
@@ -161,7 +237,7 @@ def create_interface():
|
|
161 |
style,
|
162 |
quality,
|
163 |
creativity,
|
164 |
-
|
165 |
],
|
166 |
outputs=[image_output, status]
|
167 |
)
|
|
|
4 |
import requests
|
5 |
import io
|
6 |
|
7 |
+
# Configuration des styles artistiques
|
8 |
+
ART_STYLES = {
|
9 |
+
"Renaissance": {
|
10 |
+
"prompt_prefix": "ultra-detailed renaissance style artwork, masterful anatomical details, dramatic chiaroscuro lighting",
|
11 |
+
"guidance": 15.0,
|
12 |
+
"steps": 80,
|
13 |
+
"negative_prompt": "blurry, low quality, flat lighting, cartoon, modern"
|
14 |
+
},
|
15 |
+
"Impressionnisme": {
|
16 |
+
"prompt_prefix": "impressionist art style, vibrant brushstrokes, atmospheric lighting, monet-like technique",
|
17 |
+
"guidance": 12.0,
|
18 |
+
"steps": 65,
|
19 |
+
"negative_prompt": "sharp details, photorealistic, digital art"
|
20 |
+
},
|
21 |
+
"Art Moderne": {
|
22 |
+
"prompt_prefix": "modern art composition, bold geometric shapes, abstract elements, strong visual impact",
|
23 |
+
"guidance": 11.0,
|
24 |
+
"steps": 60,
|
25 |
+
"negative_prompt": "realistic, traditional, detailed texture"
|
26 |
+
},
|
27 |
+
"Illustration Pro": {
|
28 |
+
"prompt_prefix": "professional editorial illustration, sophisticated art style, high-end magazine quality",
|
29 |
+
"guidance": 13.5,
|
30 |
+
"steps": 70,
|
31 |
+
"negative_prompt": "amateur, basic, photographic"
|
32 |
+
},
|
33 |
+
"Néo Vintage": {
|
34 |
+
"prompt_prefix": "vintage poster design, retro aesthetic, limited color palette, screen printing style",
|
35 |
+
"guidance": 12.5,
|
36 |
+
"steps": 65,
|
37 |
+
"negative_prompt": "modern, digital, photorealistic"
|
38 |
+
},
|
39 |
+
"Encre Artistique": {
|
40 |
+
"prompt_prefix": "artistic ink illustration, dynamic brushwork, high contrast black and white, expressive strokes",
|
41 |
+
"guidance": 14.0,
|
42 |
+
"steps": 75,
|
43 |
+
"negative_prompt": "color, photographic, digital"
|
44 |
+
},
|
45 |
+
"Pop Art": {
|
46 |
+
"prompt_prefix": "pop art style, bold colors, halftone dots, comic book aesthetic, Warhol-inspired",
|
47 |
+
"guidance": 11.5,
|
48 |
+
"steps": 60,
|
49 |
+
"negative_prompt": "realistic, painterly, subtle"
|
50 |
+
},
|
51 |
+
"Dark Fantasy": {
|
52 |
+
"prompt_prefix": "dark fantasy art, atmospheric mood, dramatic lighting, mythological elements",
|
53 |
+
"guidance": 13.0,
|
54 |
+
"steps": 70,
|
55 |
+
"negative_prompt": "bright, cheerful, realistic photo"
|
56 |
+
}
|
57 |
+
}
|
58 |
+
|
59 |
+
# Collections thématiques
|
60 |
+
COLLECTIONS = {
|
61 |
+
"Nature Artistique": {
|
62 |
+
"prompts": ["artistic nature illustration", "stylized botanical elements"],
|
63 |
+
"styles": ["organic composition", "natural textures", "dynamic flow"],
|
64 |
+
"negative": "artificial, synthetic, urban"
|
65 |
+
},
|
66 |
+
"Portraits Expressifs": {
|
67 |
+
"prompts": ["expressive portrait art", "emotional character illustration"],
|
68 |
+
"styles": ["dramatic lighting", "powerful expression", "artistic interpretation"],
|
69 |
+
"negative": "flat lighting, emotionless, stiff pose"
|
70 |
+
},
|
71 |
+
"Urbain Stylisé": {
|
72 |
+
"prompts": ["stylized urban art", "city aesthetic interpretation"],
|
73 |
+
"styles": ["architectural elements", "urban textures", "street art influence"],
|
74 |
+
"negative": "rural, natural, realistic photo"
|
75 |
+
},
|
76 |
+
"Fantaisie Épique": {
|
77 |
+
"prompts": ["epic fantasy artwork", "mythological interpretation"],
|
78 |
+
"styles": ["magical elements", "dramatic atmosphere", "legendary creatures"],
|
79 |
+
"negative": "mundane, realistic, modern"
|
80 |
+
}
|
81 |
+
}
|
82 |
+
|
83 |
+
# CSS personnalisé
|
84 |
CUSTOM_CSS = """
|
85 |
.container { max-width: 1200px; margin: auto; }
|
86 |
.welcome { text-align: center; margin: 20px 0; padding: 20px; background: #1e293b; border-radius: 10px; }
|
|
|
88 |
"""
|
89 |
|
90 |
def create_interface():
|
91 |
+
def generate_image(format_size, orientation, prompt, style, quality, creativity, collection=None):
|
92 |
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
|
93 |
headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_TOKEN')}"}
|
94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
try:
|
96 |
+
# Dimensions de base optimisées
|
97 |
+
base_width = 1024
|
98 |
+
base_height = 1024
|
|
|
|
|
|
|
|
|
99 |
|
100 |
+
if orientation == "Portrait":
|
101 |
+
if format_size in ["A4", "A3"]:
|
102 |
+
base_width = 768
|
103 |
+
base_height = 1024
|
104 |
+
else:
|
105 |
+
if format_size in ["A4", "A3"]:
|
106 |
+
base_width = 1024
|
107 |
+
base_height = 768
|
108 |
+
|
109 |
+
# Récupération des paramètres du style
|
110 |
+
style_config = ART_STYLES[style]
|
111 |
+
enhanced_prompt = f"{style_config['prompt_prefix']}, {prompt}"
|
112 |
|
113 |
+
if collection and collection in COLLECTIONS:
|
114 |
+
collection_data = COLLECTIONS[collection]
|
115 |
+
collection_elements = collection_data["prompts"] + collection_data["styles"]
|
116 |
+
enhanced_prompt += f", {', '.join(collection_elements)}"
|
117 |
+
negative_prompt = f"{style_config['negative_prompt']}, {collection_data['negative']}"
|
118 |
+
else:
|
119 |
+
negative_prompt = style_config['negative_prompt']
|
120 |
+
|
121 |
+
# Configuration de la génération
|
122 |
payload = {
|
123 |
"inputs": enhanced_prompt,
|
124 |
"parameters": {
|
125 |
+
"negative_prompt": negative_prompt,
|
126 |
+
"num_inference_steps": int(style_config['steps'] * (quality/100)),
|
127 |
+
"guidance_scale": style_config['guidance'] * (creativity/10),
|
128 |
"width": base_width,
|
129 |
"height": base_height
|
130 |
}
|
131 |
}
|
132 |
|
133 |
+
print(f"Génération avec paramètres: {payload}")
|
134 |
|
135 |
+
response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
|
136 |
|
137 |
if response.status_code == 200:
|
138 |
image = Image.open(io.BytesIO(response.content))
|
139 |
+
return image, f"✨ Création {style} réussie!"
|
140 |
else:
|
141 |
print(f"Erreur API: {response.text}")
|
142 |
+
return None, f"⚠️ Erreur {response.status_code}: Ajustez les paramètres"
|
143 |
|
144 |
except Exception as e:
|
145 |
print(f"Exception: {str(e)}")
|
|
|
176 |
with gr.Row():
|
177 |
with gr.Column():
|
178 |
style = gr.Dropdown(
|
179 |
+
choices=list(ART_STYLES.keys()),
|
180 |
+
value="Renaissance",
|
181 |
+
label="Style artistique"
|
182 |
+
)
|
183 |
+
|
184 |
+
collection = gr.Dropdown(
|
185 |
+
choices=list(COLLECTIONS.keys()),
|
186 |
+
label="Collection (optionnel)",
|
187 |
+
info="Thèmes artistiques spécialisés"
|
188 |
)
|
189 |
|
190 |
prompt = gr.Textbox(
|
191 |
lines=3,
|
192 |
label="Description",
|
193 |
+
placeholder="Décrivez votre vision artistique..."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
)
|
195 |
|
196 |
with gr.Group(elem_classes="quality-controls"):
|
|
|
237 |
style,
|
238 |
quality,
|
239 |
creativity,
|
240 |
+
collection
|
241 |
],
|
242 |
outputs=[image_output, status]
|
243 |
)
|