Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,108 +1,64 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
| 3 |
import requests
|
| 4 |
from io import BytesIO
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
HF_TOKEN = "hf_..." # Remplacez par votre vrai token
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
STYLES = {
|
| 11 |
-
"signage": "Signalétique Professionnelle",
|
| 12 |
-
"poster": "Affiche Artistique",
|
| 13 |
-
"silhouette": "Silhouette Décorative"
|
| 14 |
-
}
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
# Suggestions de prompts par style
|
| 23 |
-
PROMPT_SUGGESTIONS = {
|
| 24 |
-
"signage": [
|
| 25 |
-
"Panneau directionnel moderne en carton recyclé, style minimaliste",
|
| 26 |
-
"Présentoir écologique pour boutique, motifs naturels",
|
| 27 |
-
"Signalétique murale en carton kraft, design épuré"
|
| 28 |
-
],
|
| 29 |
-
"poster": [
|
| 30 |
-
"Affiche artistique sur carton texturé, thème environnemental",
|
| 31 |
-
"Poster graphique moderne, motifs géométriques en relief",
|
| 32 |
-
"Design abstrait sur carton ondulé, couleurs naturelles"
|
| 33 |
-
],
|
| 34 |
-
"silhouette": [
|
| 35 |
-
"Découpe décorative florale en carton, style organique",
|
| 36 |
-
"Silhouette architecturale moderne en carton recyclé",
|
| 37 |
-
"Motif géométrique découpé, effet d'ombre et lumière"
|
| 38 |
-
]
|
| 39 |
-
}
|
| 40 |
|
| 41 |
def generate_image(prompt, style, format_size, material):
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
|
| 46 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
try:
|
| 49 |
-
|
| 50 |
-
if not prompt or len(prompt.strip()) < 5:
|
| 51 |
-
return None, "Le prompt est trop court. Veuillez décrire plus en détail."
|
| 52 |
-
|
| 53 |
-
# Préparation du prompt
|
| 54 |
-
style_prompts = {
|
| 55 |
-
"signage": "professional signage design, minimalist, clean, photorealistic",
|
| 56 |
-
"poster": "artistic poster design, creative, expressive, highly detailed",
|
| 57 |
-
"silhouette": "decorative silhouette, elegant cutout design, clear shape"
|
| 58 |
-
}
|
| 59 |
-
|
| 60 |
-
enhanced_prompt = f"a {material} cardboard {style} design showing {prompt}, {style_prompts.get(style, '')}, professional photography, 8k uhd, detailed"
|
| 61 |
-
|
| 62 |
-
# Payload de la requête
|
| 63 |
-
payload = {
|
| 64 |
-
"inputs": enhanced_prompt,
|
| 65 |
-
"negative_prompt": "low quality, blurry, bad anatomy, distorted, disfigured, pixelated",
|
| 66 |
-
"num_inference_steps": 40,
|
| 67 |
-
"guidance_scale": 7.5,
|
| 68 |
-
"width": 512,
|
| 69 |
-
"height": 512
|
| 70 |
-
}
|
| 71 |
-
|
| 72 |
-
# Envoi de la requête
|
| 73 |
response = requests.post(API_URL, headers=headers, json=payload)
|
| 74 |
-
|
| 75 |
-
|
| 76 |
if response.status_code == 200:
|
| 77 |
-
return Image.open(BytesIO(response.content))
|
| 78 |
-
elif response.status_code == 401:
|
| 79 |
-
return None, "Erreur d'authentification. Vérifiez votre token."
|
| 80 |
-
elif response.status_code == 429:
|
| 81 |
-
return None, "Quota API dépassé. Réessayez plus tard."
|
| 82 |
else:
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
except Exception as e:
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
"""Mise à jour des suggestions de prompts selon le style."""
|
| 93 |
-
return gr.Dropdown(choices=PROMPT_SUGGESTIONS.get(style, []))
|
| 94 |
|
| 95 |
-
|
| 96 |
-
gr.Markdown(
|
| 97 |
-
"""
|
| 98 |
-
# 🎨 Equity Creation Studio
|
| 99 |
-
### Studio de Design Éco-responsable
|
| 100 |
-
Transformez vos idées en créations durables avec notre générateur d'images IA.
|
| 101 |
-
"""
|
| 102 |
-
)
|
| 103 |
|
| 104 |
-
|
| 105 |
-
with gr.Column(scale=1):
|
| 106 |
-
style = gr.Radio(
|
| 107 |
-
choices=list(STYLES.keys()),
|
| 108 |
-
value="signage",
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 4 |
import requests
|
| 5 |
from io import BytesIO
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
|
|
| 9 |
|
| 10 |
+
# Le reste des constantes reste inchangé...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
def create_error_image(error_message):
|
| 13 |
+
img = Image.new('RGB', (512, 512), color='lightgray')
|
| 14 |
+
draw = ImageDraw.Draw(img)
|
| 15 |
+
font = ImageFont.load_default()
|
| 16 |
+
draw.text((10, 10), error_message, fill='black', font=font)
|
| 17 |
+
return img
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
def generate_image(prompt, style, format_size, material):
|
| 20 |
+
if not HF_TOKEN:
|
| 21 |
+
return create_error_image("Token HF manquant. Vérifiez la configuration.")
|
| 22 |
+
|
| 23 |
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
|
| 24 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 25 |
+
|
| 26 |
+
style_prompts = {
|
| 27 |
+
"signage": "professional signage design, minimalist, clean, photorealistic",
|
| 28 |
+
"poster": "artistic poster design, creative, expressive, highly detailed",
|
| 29 |
+
"silhouette": "decorative silhouette, elegant cutout design, clear shape"
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
enhanced_prompt = f"a {material} cardboard {style} design showing {prompt}, {style_prompts[style]}, professional photography, 8k uhd, detailed"
|
| 33 |
+
|
| 34 |
+
print(f"Envoi du prompt: {enhanced_prompt}")
|
| 35 |
+
|
| 36 |
+
payload = {
|
| 37 |
+
"inputs": enhanced_prompt,
|
| 38 |
+
"negative_prompt": "low quality, blurry, bad anatomy, distorted, disfigured, pixelated",
|
| 39 |
+
"num_inference_steps": 30,
|
| 40 |
+
"guidance_scale": 7.5,
|
| 41 |
+
"width": 512,
|
| 42 |
+
"height": 512
|
| 43 |
+
}
|
| 44 |
|
| 45 |
try:
|
| 46 |
+
print("Envoi de la requête à l'API...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
response = requests.post(API_URL, headers=headers, json=payload)
|
| 48 |
+
print(f"Status code: {response.status_code}")
|
| 49 |
+
|
| 50 |
if response.status_code == 200:
|
| 51 |
+
return Image.open(BytesIO(response.content))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
else:
|
| 53 |
+
error_message = f"Erreur API - Status: {response.status_code}, Response: {response.text}"
|
| 54 |
+
print(error_message)
|
| 55 |
+
return create_error_image(error_message)
|
| 56 |
+
|
|
|
|
| 57 |
except Exception as e:
|
| 58 |
+
error_message = f"Exception lors de la génération: {str(e)}"
|
| 59 |
+
print(error_message)
|
| 60 |
+
return create_error_image(error_message)
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
# Le reste du code reste inchangé...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
demo.launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|