Spaces:
Sleeping
Sleeping
import gradio as gr | |
import random | |
from PIL import Image | |
import requests | |
from io import BytesIO | |
import os | |
# Token Hugging Face | |
HF_TOKEN = "hf_..." # Assurez-vous que ce token est correct | |
# Styles disponibles | |
STYLES = { | |
"signage": "Signalétique Professionnelle", | |
"poster": "Affiche Artistique", | |
"silhouette": "Silhouette Décorative" | |
} | |
# Formats disponibles | |
FORMATS = ["A4", "A3", "A2", "A1"] | |
# Matériaux disponibles | |
MATERIALS = ["Lisse", "Ondulé", "Kraft"] | |
# Suggestions de prompts par style | |
PROMPT_SUGGESTIONS = { | |
"signage": [ | |
"Panneau directionnel moderne en carton recyclé, style minimaliste", | |
"Présentoir écologique pour boutique, motifs naturels", | |
"Signalétique murale en carton kraft, design épuré" | |
], | |
"poster": [ | |
"Affiche artistique sur carton texturé, thème environnemental", | |
"Poster graphique moderne, motifs géométriques en relief", | |
"Design abstrait sur carton ondulé, couleurs naturelles" | |
], | |
"silhouette": [ | |
"Découpe décorative florale en carton, style organique", | |
"Silhouette architecturale moderne en carton recyclé", | |
"Motif géométrique découpé, effet d'ombre et lumière" | |
] | |
} | |
def generate_image(prompt, style, format_size, material): | |
""" | |
Génération d'image via l'API Hugging Face | |
""" | |
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0" | |
headers = {"Authorization": f"Bearer {HF_TOKEN}"} | |
style_prompts = { | |
"signage": "professional signage design, minimalist, clean, photorealistic", | |
"poster": "artistic poster design, creative, expressive, highly detailed", | |
"silhouette": "decorative silhouette, elegant cutout design, clear shape" | |
} | |
enhanced_prompt = f"a {material} cardboard {style} design showing {prompt}, {style_prompts[style]}, professional photography, 8k uhd, detailed" | |
print(f"Envoi du prompt: {enhanced_prompt}") | |
payload = { | |
"inputs": enhanced_prompt, | |
"negative_prompt": "low quality, blurry, bad anatomy, distorted, disfigured, pixelated", | |
"num_inference_steps": 40, | |
"guidance_scale": 7.5, | |
"width": 768, | |
"height": 768 | |
} | |
try: | |
print("Envoi de la requête à l'API...") | |
response = requests.post(API_URL, headers=headers, json=payload) | |
print(f"Status code: {response.status_code}") | |
if response.status_code == 200: | |
print("Image générée avec succès") | |
return Image.open(BytesIO(response.content)) | |
else: | |
print(f"Erreur API - Response: {response.text}") | |
error_image = Image.new('RGB', (512, 512), color='red') | |
return error_image | |
except requests.exceptions.RequestException as e: | |
print(f"Erreur de requête : {str(e)}") | |
error_image = Image.new('RGB', (512, 512), color='orange') | |
return error_image | |
except Exception as e: | |
print(f"Exception inattendue : {str(e)}") | |
error_image = Image.new('RGB', (512, 512), color='yellow') | |
return error_image | |
def update_suggestions(style): | |
""" | |
Mise à jour des suggestions de prompts selon le style | |
""" | |
return gr.Dropdown(choices=PROMPT_SUGGESTIONS.get(style, [])) | |
with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
gr.Markdown( | |
""" | |
# 🎨 Equity Creation Studio | |
### Studio de Design Éco-responsable | |
Transformez vos idées en créations durables avec notre générateur d'images IA | |
""" | |
) | |
with gr.Row(): | |
with gr.Column(scale=1): | |
style = gr.Radio( | |
choices=list(STYLES.keys()), | |
value="signage", | |
label="Style de Création", | |
info="Choisissez le type de design souhaité" | |
) | |
format_size = gr.Radio( | |
choices=FORMATS, | |
value="A4", | |
label="Format", | |
info="Sélectionnez la taille de votre création" | |
) | |
material = gr.Radio( | |
choices=MATERIALS, | |
value="Lisse", | |
label="Type de Carton", | |
info="Choisissez votre matériau" | |
) | |
prompt = gr.Textbox( | |
lines=3, | |
label="Description", | |
placeholder="Décrivez votre vision..." | |
) | |
suggestions = gr.Dropdown( | |
choices=PROMPT_SUGGESTIONS["signage"], | |
label="Suggestions de prompts", | |
info="Sélectionnez une suggestion ou écrivez votre propre description" | |
) | |
generate_btn = gr.Button("🪄 Générer", variant="primary") | |
with gr.Column(scale=2): | |
output = gr.Image(label="Image générée") | |
style.change( | |
fn=update_suggestions, | |
inputs=[style], | |
outputs=[suggestions] | |
) | |
suggestions.change( | |
fn=lambda x: x, | |
inputs=[suggestions], | |
outputs=[prompt] | |
) | |
generate_btn.click( | |
fn=generate_image, | |
inputs=[prompt, style, format_size, material], | |
outputs=[output] | |
) | |
gr.Markdown( | |
""" | |
### 🌿 Design Éco-responsable | |
Equity Creation Studio transforme vos déchets carton en œuvres d'art | |
""" | |
) | |
demo.launch() |