Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import random
|
| 3 |
from PIL import Image
|
| 4 |
import requests
|
| 5 |
from io import BytesIO
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
# Token Hugging Face - Assurez-vous que ce token est correct et a les bonnes permissions
|
| 9 |
HF_TOKEN = "hf_..." # Remplacez par votre vrai token
|
|
@@ -42,59 +40,56 @@ PROMPT_SUGGESTIONS = {
|
|
| 42 |
|
| 43 |
def generate_image(prompt, style, format_size, material):
|
| 44 |
"""
|
| 45 |
-
Génération d'image via l'API Hugging Face
|
| 46 |
"""
|
| 47 |
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
|
| 48 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 49 |
-
|
| 50 |
-
style_prompts = {
|
| 51 |
-
"signage": "professional signage design, minimalist, clean, photorealistic",
|
| 52 |
-
"poster": "artistic poster design, creative, expressive, highly detailed",
|
| 53 |
-
"silhouette": "decorative silhouette, elegant cutout design, clear shape"
|
| 54 |
-
}
|
| 55 |
-
|
| 56 |
-
enhanced_prompt = f"a {material} cardboard {style} design showing {prompt}, {style_prompts[style]}, professional photography, 8k uhd, detailed"
|
| 57 |
-
|
| 58 |
-
print(f"Envoi du prompt: {enhanced_prompt}")
|
| 59 |
-
|
| 60 |
-
payload = {
|
| 61 |
-
"inputs": enhanced_prompt,
|
| 62 |
-
"negative_prompt": "low quality, blurry, bad anatomy, distorted, disfigured, pixelated",
|
| 63 |
-
"num_inference_steps": 40,
|
| 64 |
-
"guidance_scale": 7.5,
|
| 65 |
-
"width": 512,
|
| 66 |
-
"height": 512
|
| 67 |
-
}
|
| 68 |
|
| 69 |
try:
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
response = requests.post(API_URL, headers=headers, json=payload)
|
| 72 |
-
|
| 73 |
-
|
| 74 |
if response.status_code == 200:
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
| 77 |
else:
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
except requests.exceptions.RequestException as e:
|
| 84 |
-
print(f"Erreur de requête : {str(e)}")
|
| 85 |
-
error_message = f"Erreur de requête : {str(e)}"
|
| 86 |
-
error_image = Image.new('RGB', (512, 512), color='orange')
|
| 87 |
-
return gr.Image.update(value=error_image, label=error_message)
|
| 88 |
except Exception as e:
|
| 89 |
-
|
| 90 |
-
error_message = f"Erreur inattendue : {str(e)}"
|
| 91 |
-
error_image = Image.new('RGB', (512, 512), color='yellow')
|
| 92 |
-
return gr.Image.update(value=error_image, label=error_message)
|
| 93 |
|
| 94 |
def update_suggestions(style):
|
| 95 |
-
"""
|
| 96 |
-
Mise à jour des suggestions de prompts selon le style
|
| 97 |
-
"""
|
| 98 |
return gr.Dropdown(choices=PROMPT_SUGGESTIONS.get(style, []))
|
| 99 |
|
| 100 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
@@ -102,73 +97,12 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 102 |
"""
|
| 103 |
# 🎨 Equity Creation Studio
|
| 104 |
### Studio de Design Éco-responsable
|
| 105 |
-
Transformez vos idées en créations durables avec notre générateur d'images IA
|
| 106 |
"""
|
| 107 |
)
|
| 108 |
-
|
| 109 |
with gr.Row():
|
| 110 |
with gr.Column(scale=1):
|
| 111 |
style = gr.Radio(
|
| 112 |
choices=list(STYLES.keys()),
|
| 113 |
-
value="signage",
|
| 114 |
-
label="Style de Création",
|
| 115 |
-
info="Choisissez le type de design souhaité"
|
| 116 |
-
)
|
| 117 |
-
|
| 118 |
-
format_size = gr.Radio(
|
| 119 |
-
choices=FORMATS,
|
| 120 |
-
value="A4",
|
| 121 |
-
label="Format",
|
| 122 |
-
info="Sélectionnez la taille de votre création"
|
| 123 |
-
)
|
| 124 |
-
|
| 125 |
-
material = gr.Radio(
|
| 126 |
-
choices=MATERIALS,
|
| 127 |
-
value="Lisse",
|
| 128 |
-
label="Type de Carton",
|
| 129 |
-
info="Choisissez votre matériau"
|
| 130 |
-
)
|
| 131 |
-
|
| 132 |
-
prompt = gr.Textbox(
|
| 133 |
-
lines=3,
|
| 134 |
-
label="Description",
|
| 135 |
-
placeholder="Décrivez votre vision..."
|
| 136 |
-
)
|
| 137 |
-
|
| 138 |
-
suggestions = gr.Dropdown(
|
| 139 |
-
choices=PROMPT_SUGGESTIONS["signage"],
|
| 140 |
-
label="Suggestions de prompts",
|
| 141 |
-
info="Sélectionnez une suggestion ou écrivez votre propre description"
|
| 142 |
-
)
|
| 143 |
-
|
| 144 |
-
generate_btn = gr.Button("🪄 Générer", variant="primary")
|
| 145 |
-
|
| 146 |
-
with gr.Column(scale=2):
|
| 147 |
-
output = gr.Image(label="Image générée")
|
| 148 |
-
|
| 149 |
-
style.change(
|
| 150 |
-
fn=update_suggestions,
|
| 151 |
-
inputs=[style],
|
| 152 |
-
outputs=[suggestions]
|
| 153 |
-
)
|
| 154 |
-
|
| 155 |
-
suggestions.change(
|
| 156 |
-
fn=lambda x: x,
|
| 157 |
-
inputs=[suggestions],
|
| 158 |
-
outputs=[prompt]
|
| 159 |
-
)
|
| 160 |
-
|
| 161 |
-
generate_btn.click(
|
| 162 |
-
fn=generate_image,
|
| 163 |
-
inputs=[prompt, style, format_size, material],
|
| 164 |
-
outputs=[output]
|
| 165 |
-
)
|
| 166 |
-
|
| 167 |
-
gr.Markdown(
|
| 168 |
-
"""
|
| 169 |
-
### 🌿 Design Éco-responsable
|
| 170 |
-
Equity Creation Studio transforme vos déchets carton en œuvres d'art
|
| 171 |
-
"""
|
| 172 |
-
)
|
| 173 |
-
|
| 174 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
import requests
|
| 4 |
from io import BytesIO
|
|
|
|
| 5 |
|
| 6 |
# Token Hugging Face - Assurez-vous que ce token est correct et a les bonnes permissions
|
| 7 |
HF_TOKEN = "hf_..." # Remplacez par votre vrai token
|
|
|
|
| 40 |
|
| 41 |
def generate_image(prompt, style, format_size, material):
|
| 42 |
"""
|
| 43 |
+
Génération d'image via l'API Hugging Face avec gestion des erreurs.
|
| 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 |
+
# Validation des inputs
|
| 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 |
+
# Gestion des différents codes de statut
|
| 76 |
if response.status_code == 200:
|
| 77 |
+
return Image.open(BytesIO(response.content)), None # Pas d'erreur
|
| 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 |
+
return None, f"Erreur inattendue. Code {response.status_code} - {response.text}"
|
| 84 |
+
|
| 85 |
+
except requests.exceptions.RequestException as req_err:
|
| 86 |
+
return None, f"Erreur de connexion : {req_err}"
|
| 87 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
except Exception as e:
|
| 89 |
+
return None, f"Erreur système : {e}"
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
def update_suggestions(style):
|
| 92 |
+
"""Mise à jour des suggestions de prompts selon le style."""
|
|
|
|
|
|
|
| 93 |
return gr.Dropdown(choices=PROMPT_SUGGESTIONS.get(style, []))
|
| 94 |
|
| 95 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
|
| 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 |
with gr.Row():
|
| 105 |
with gr.Column(scale=1):
|
| 106 |
style = gr.Radio(
|
| 107 |
choices=list(STYLES.keys()),
|
| 108 |
+
value="signage",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|