|
import gradio as gr |
|
import os |
|
from PIL import Image |
|
import requests |
|
import io |
|
import gc |
|
import json |
|
from typing import Tuple, Optional, Dict, Any |
|
import logging |
|
from dotenv import load_dotenv |
|
|
|
|
|
logging.basicConfig(level=logging.INFO) |
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
ART_STYLES = { |
|
"Art Moderne": { |
|
"prompt_prefix": "modern art style poster, professional design", |
|
"negative_prompt": "traditional, photorealistic, cluttered, busy design" |
|
}, |
|
"Neo Vintage": { |
|
"prompt_prefix": "vintage style advertising poster, retro design", |
|
"negative_prompt": "modern, digital, contemporary style" |
|
}, |
|
"Pop Art": { |
|
"prompt_prefix": "pop art style poster, bold design", |
|
"negative_prompt": "subtle, realistic, traditional art" |
|
}, |
|
"Minimaliste": { |
|
"prompt_prefix": "minimalist design poster, clean composition", |
|
"negative_prompt": "complex, detailed, ornate, busy" |
|
} |
|
} |
|
|
|
|
|
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0" |
|
|
|
def generate_image(params: Dict[str, Any]) -> Tuple[Optional[Image.Image], str]: |
|
"""Génère une image via l'API Hugging Face""" |
|
try: |
|
headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_TOKEN')}"} |
|
|
|
style = ART_STYLES[params["style"]] |
|
prompt = f"{style['prompt_prefix']}, {params['subject']}" |
|
|
|
|
|
payload = { |
|
"inputs": prompt, |
|
"parameters": { |
|
"negative_prompt": style["negative_prompt"], |
|
"num_inference_steps": 30, |
|
"guidance_scale": 7.5, |
|
"width": 768, |
|
"height": 768 |
|
} |
|
} |
|
|
|
response = requests.post(API_URL, headers=headers, json=payload) |
|
|
|
if response.status_code == 200: |
|
image = Image.open(io.BytesIO(response.content)) |
|
return image, "✨ Création réussie!" |
|
else: |
|
return None, f"⚠️ Erreur {response.status_code}: {response.text}" |
|
|
|
except Exception as e: |
|
logger.error(f"Erreur: {str(e)}") |
|
return None, f"⚠️ Erreur: {str(e)}" |
|
|
|
def create_interface(): |
|
"""Crée l'interface Gradio""" |
|
with gr.Blocks() as app: |
|
gr.HTML(""" |
|
<h1 style='text-align: center'>🎨 Generart</h1> |
|
<p style='text-align: center'>Créez des affiches artistiques avec l'IA</p> |
|
""") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
style = gr.Dropdown( |
|
choices=list(ART_STYLES.keys()), |
|
value="Neo Vintage", |
|
label="Style artistique" |
|
) |
|
|
|
subject = gr.Textbox( |
|
label="Description", |
|
placeholder="Décrivez votre vision..." |
|
) |
|
|
|
generate_btn = gr.Button("✨ Générer") |
|
|
|
with gr.Column(): |
|
image_output = gr.Image(label="Résultat") |
|
status = gr.Textbox(label="Statut") |
|
|
|
def on_generate(style_val, subject_val): |
|
return generate_image({ |
|
"style": style_val, |
|
"subject": subject_val |
|
}) |
|
|
|
generate_btn.click( |
|
fn=on_generate, |
|
inputs=[style, subject], |
|
outputs=[image_output, status] |
|
) |
|
|
|
return app |
|
|
|
if __name__ == "__main__": |
|
app = create_interface() |
|
app.launch() |