|
import gradio as gr |
|
import os |
|
from PIL import Image, ImageDraw, ImageFont |
|
import requests |
|
import io |
|
|
|
CUSTOM_CSS = """ |
|
.container { max-width: 1200px; margin: auto; } |
|
.welcome { text-align: center; margin: 20px 0; padding: 20px; background: #1e293b; border-radius: 10px; } |
|
.quality-controls { margin: 10px 0; padding: 10px; background: #2d3748; border-radius: 5px; } |
|
""" |
|
|
|
def create_interface(): |
|
def generate_image(format_size, orientation, prompt, style, quality, creativity, negative_prompt=""): |
|
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0" |
|
headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_TOKEN')}"} |
|
|
|
|
|
base_width = 1024 |
|
base_height = 1024 |
|
|
|
if orientation == "Portrait": |
|
if format_size in ["A4", "A3"]: |
|
base_width = 768 |
|
base_height = 1024 |
|
else: |
|
if format_size in ["A4", "A3"]: |
|
base_width = 1024 |
|
base_height = 768 |
|
|
|
try: |
|
|
|
style_prompts = { |
|
"Moderne": "modern clean poster design, professional layout", |
|
"Artistique": "artistic poster composition, creative design", |
|
"Corporate": "professional business poster, corporate style", |
|
"Impact": "bold impactful poster design, attention-grabbing" |
|
} |
|
|
|
enhanced_prompt = f"{style_prompts[style]}, {prompt}, high quality, detailed" |
|
default_negative = "blurry, low quality, distorted, ugly, bad proportions, weird colors" |
|
final_negative = negative_prompt if negative_prompt.strip() else default_negative |
|
|
|
payload = { |
|
"inputs": enhanced_prompt, |
|
"parameters": { |
|
"negative_prompt": final_negative, |
|
"num_inference_steps": min(max(30, int(quality)), 50), |
|
"guidance_scale": min(max(5, creativity), 15), |
|
"width": base_width, |
|
"height": base_height |
|
} |
|
} |
|
|
|
print(f"Envoi de la requête avec les paramètres: {payload}") |
|
|
|
response = requests.post(API_URL, headers=headers, json=payload, timeout=30) |
|
|
|
if response.status_code == 200: |
|
image = Image.open(io.BytesIO(response.content)) |
|
return image, "✨ Image générée avec succès!" |
|
else: |
|
print(f"Erreur API: {response.text}") |
|
return None, f"⚠️ Erreur {response.status_code}: Essayez de modifier vos paramètres" |
|
|
|
except Exception as e: |
|
print(f"Exception: {str(e)}") |
|
return None, f"⚠️ Erreur: {str(e)}" |
|
|
|
with gr.Blocks(css=CUSTOM_CSS) as app: |
|
gr.HTML(""" |
|
<div class="welcome"> |
|
<h1>🤖 Equity Artisan 3.0</h1> |
|
<p>Créez des affiches professionnelles étape par étape avec notre assistant créatif augmenté.</p> |
|
</div> |
|
""") |
|
|
|
with gr.Column(elem_classes="container"): |
|
|
|
with gr.Group(): |
|
gr.Markdown("### 📏 Étape 1: Format et Orientation") |
|
with gr.Row(): |
|
format_choice = gr.Dropdown( |
|
choices=["A4", "A3", "A2", "A1", "A0"], |
|
value="A4", |
|
label="Format" |
|
) |
|
orientation = gr.Radio( |
|
choices=["Portrait", "Paysage"], |
|
value="Portrait", |
|
label="Orientation" |
|
) |
|
|
|
|
|
with gr.Group(): |
|
gr.Markdown("### 🎨 Étape 2: Création Visuelle") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
style = gr.Dropdown( |
|
choices=["Moderne", "Artistique", "Corporate", "Impact"], |
|
value="Moderne", |
|
label="Style visuel" |
|
) |
|
|
|
prompt = gr.Textbox( |
|
lines=3, |
|
label="Description", |
|
placeholder="Décrivez l'affiche que vous souhaitez...", |
|
value="professional poster design with clean layout" |
|
) |
|
|
|
|
|
negative_prompt = gr.Textbox( |
|
label="Éléments à éviter (optionnel)", |
|
placeholder="Ex: flou, texte illisible, distorsions...", |
|
lines=2 |
|
) |
|
|
|
with gr.Group(elem_classes="quality-controls"): |
|
quality = gr.Slider( |
|
minimum=30, |
|
maximum=50, |
|
value=35, |
|
step=5, |
|
label="Qualité", |
|
info="Influence le niveau de détail" |
|
) |
|
|
|
creativity = gr.Slider( |
|
minimum=5, |
|
maximum=15, |
|
value=7.5, |
|
step=0.5, |
|
label="Créativité", |
|
info="Balance entre fidélité et créativité" |
|
) |
|
|
|
generate_btn = gr.Button("✨ Générer", variant="primary") |
|
clear_btn = gr.Button("🗑️ Effacer", variant="secondary") |
|
|
|
image_output = gr.Image( |
|
label="Aperçu", |
|
type="pil" |
|
) |
|
|
|
|
|
status = gr.Textbox( |
|
label="Status", |
|
interactive=False, |
|
value="Prêt à générer" |
|
) |
|
|
|
|
|
generate_btn.click( |
|
generate_image, |
|
inputs=[ |
|
format_choice, |
|
orientation, |
|
prompt, |
|
style, |
|
quality, |
|
creativity, |
|
negative_prompt |
|
], |
|
outputs=[image_output, status] |
|
) |
|
|
|
clear_btn.click( |
|
lambda: (None, "Image effacée"), |
|
outputs=[image_output, status] |
|
) |
|
|
|
return app |
|
|
|
if __name__ == "__main__": |
|
app = create_interface() |
|
app.launch() |