Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,12 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import random
|
| 3 |
-
import time
|
| 4 |
from PIL import Image
|
| 5 |
import requests
|
| 6 |
from io import BytesIO
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Styles disponibles
|
| 9 |
STYLES = {
|
|
@@ -37,16 +40,39 @@ PROMPT_SUGGESTIONS = {
|
|
| 37 |
]
|
| 38 |
}
|
| 39 |
|
| 40 |
-
def
|
| 41 |
"""
|
| 42 |
-
|
| 43 |
"""
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
def update_suggestions(style):
|
| 52 |
"""
|
|
@@ -54,13 +80,6 @@ def update_suggestions(style):
|
|
| 54 |
"""
|
| 55 |
return gr.Dropdown(choices=PROMPT_SUGGESTIONS.get(style, []))
|
| 56 |
|
| 57 |
-
def get_example_prompt(style):
|
| 58 |
-
"""
|
| 59 |
-
Obtenir un prompt exemple du style sélectionné
|
| 60 |
-
"""
|
| 61 |
-
suggestions = PROMPT_SUGGESTIONS.get(style, [])
|
| 62 |
-
return random.choice(suggestions) if suggestions else ""
|
| 63 |
-
|
| 64 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 65 |
gr.Markdown(
|
| 66 |
"""
|
|
@@ -71,9 +90,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 71 |
)
|
| 72 |
|
| 73 |
with gr.Row():
|
| 74 |
-
# Panneau de gauche - Contrôles
|
| 75 |
with gr.Column(scale=1):
|
| 76 |
-
# Section Style
|
| 77 |
style = gr.Radio(
|
| 78 |
choices=list(STYLES.keys()),
|
| 79 |
value="signage",
|
|
@@ -81,7 +98,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 81 |
info="Choisissez le type de design souhaité"
|
| 82 |
)
|
| 83 |
|
| 84 |
-
# Section Format
|
| 85 |
format_size = gr.Radio(
|
| 86 |
choices=FORMATS,
|
| 87 |
value="A4",
|
|
@@ -89,7 +105,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 89 |
info="Sélectionnez la taille de votre création"
|
| 90 |
)
|
| 91 |
|
| 92 |
-
# Section Matériau
|
| 93 |
material = gr.Radio(
|
| 94 |
choices=MATERIALS,
|
| 95 |
value="Lisse",
|
|
@@ -97,28 +112,23 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 97 |
info="Choisissez votre matériau"
|
| 98 |
)
|
| 99 |
|
| 100 |
-
# Section Prompt
|
| 101 |
prompt = gr.Textbox(
|
| 102 |
lines=3,
|
| 103 |
label="Description",
|
| 104 |
placeholder="Décrivez votre vision..."
|
| 105 |
)
|
| 106 |
|
| 107 |
-
# Suggestions de prompts
|
| 108 |
suggestions = gr.Dropdown(
|
| 109 |
choices=PROMPT_SUGGESTIONS["signage"],
|
| 110 |
label="Suggestions de prompts",
|
| 111 |
info="Sélectionnez une suggestion ou écrivez votre propre description"
|
| 112 |
)
|
| 113 |
|
| 114 |
-
# Bouton de génération
|
| 115 |
generate_btn = gr.Button("🪄 Générer", variant="primary")
|
| 116 |
|
| 117 |
-
# Panneau de droite - Prévisualisation
|
| 118 |
with gr.Column(scale=2):
|
| 119 |
output = gr.Image(label="Image générée")
|
| 120 |
|
| 121 |
-
# Interactions
|
| 122 |
style.change(
|
| 123 |
fn=update_suggestions,
|
| 124 |
inputs=[style],
|
|
@@ -132,7 +142,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 132 |
)
|
| 133 |
|
| 134 |
generate_btn.click(
|
| 135 |
-
fn=
|
| 136 |
inputs=[prompt, style, format_size, material],
|
| 137 |
outputs=[output]
|
| 138 |
)
|
|
@@ -144,5 +154,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 144 |
"""
|
| 145 |
)
|
| 146 |
|
| 147 |
-
# Lancement de l'interface
|
| 148 |
demo.launch()
|
|
|
|
| 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 - À remplacer par votre token
|
| 9 |
+
HF_TOKEN = "hf_..." # Mettez votre token ici
|
| 10 |
|
| 11 |
# Styles disponibles
|
| 12 |
STYLES = {
|
|
|
|
| 40 |
]
|
| 41 |
}
|
| 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/runwayml/stable-diffusion-v1-5"
|
| 48 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 49 |
+
|
| 50 |
+
# Style prompts pour améliorer la génération
|
| 51 |
+
style_prompts = {
|
| 52 |
+
"signage": "professional signage design, minimalist, clean",
|
| 53 |
+
"poster": "artistic poster design, creative, expressive",
|
| 54 |
+
"silhouette": "decorative silhouette, elegant cutout design"
|
| 55 |
+
}
|
| 56 |
|
| 57 |
+
# Enrichir le prompt
|
| 58 |
+
enhanced_prompt = f"{prompt}, {style_prompts[style]}, made of {material} cardboard, eco-friendly design, high quality, professional, detailed"
|
| 59 |
+
|
| 60 |
+
payload = {
|
| 61 |
+
"inputs": enhanced_prompt,
|
| 62 |
+
"negative_prompt": "low quality, blurry, bad anatomy, distorted, pixelated",
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
try:
|
| 66 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 67 |
+
if response.status_code == 200:
|
| 68 |
+
image_bytes = response.content
|
| 69 |
+
return Image.open(BytesIO(image_bytes))
|
| 70 |
+
else:
|
| 71 |
+
print(f"Erreur API : {response.status_code}")
|
| 72 |
+
return Image.new('RGB', (512, 512), color='lightgray')
|
| 73 |
+
except Exception as e:
|
| 74 |
+
print(f"Erreur de génération : {e}")
|
| 75 |
+
return Image.new('RGB', (512, 512), color='lightgray')
|
| 76 |
|
| 77 |
def update_suggestions(style):
|
| 78 |
"""
|
|
|
|
| 80 |
"""
|
| 81 |
return gr.Dropdown(choices=PROMPT_SUGGESTIONS.get(style, []))
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 84 |
gr.Markdown(
|
| 85 |
"""
|
|
|
|
| 90 |
)
|
| 91 |
|
| 92 |
with gr.Row():
|
|
|
|
| 93 |
with gr.Column(scale=1):
|
|
|
|
| 94 |
style = gr.Radio(
|
| 95 |
choices=list(STYLES.keys()),
|
| 96 |
value="signage",
|
|
|
|
| 98 |
info="Choisissez le type de design souhaité"
|
| 99 |
)
|
| 100 |
|
|
|
|
| 101 |
format_size = gr.Radio(
|
| 102 |
choices=FORMATS,
|
| 103 |
value="A4",
|
|
|
|
| 105 |
info="Sélectionnez la taille de votre création"
|
| 106 |
)
|
| 107 |
|
|
|
|
| 108 |
material = gr.Radio(
|
| 109 |
choices=MATERIALS,
|
| 110 |
value="Lisse",
|
|
|
|
| 112 |
info="Choisissez votre matériau"
|
| 113 |
)
|
| 114 |
|
|
|
|
| 115 |
prompt = gr.Textbox(
|
| 116 |
lines=3,
|
| 117 |
label="Description",
|
| 118 |
placeholder="Décrivez votre vision..."
|
| 119 |
)
|
| 120 |
|
|
|
|
| 121 |
suggestions = gr.Dropdown(
|
| 122 |
choices=PROMPT_SUGGESTIONS["signage"],
|
| 123 |
label="Suggestions de prompts",
|
| 124 |
info="Sélectionnez une suggestion ou écrivez votre propre description"
|
| 125 |
)
|
| 126 |
|
|
|
|
| 127 |
generate_btn = gr.Button("🪄 Générer", variant="primary")
|
| 128 |
|
|
|
|
| 129 |
with gr.Column(scale=2):
|
| 130 |
output = gr.Image(label="Image générée")
|
| 131 |
|
|
|
|
| 132 |
style.change(
|
| 133 |
fn=update_suggestions,
|
| 134 |
inputs=[style],
|
|
|
|
| 142 |
)
|
| 143 |
|
| 144 |
generate_btn.click(
|
| 145 |
+
fn=generate_image,
|
| 146 |
inputs=[prompt, style, format_size, material],
|
| 147 |
outputs=[output]
|
| 148 |
)
|
|
|
|
| 154 |
"""
|
| 155 |
)
|
| 156 |
|
|
|
|
| 157 |
demo.launch()
|