File size: 3,709 Bytes
c290e43
f29baad
76ec96d
2bc8286
 
 
3adc659
2bc8286
 
 
89f7e0d
2bc8286
 
 
89f7e0d
2bc8286
 
3adc659
2bc8286
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89f7e0d
2bc8286
 
 
 
 
 
 
89f7e0d
2bc8286
 
89f7e0d
2bc8286
 
 
 
 
 
 
 
 
 
89f7e0d
 
2bc8286
2e9811d
2bc8286
 
 
 
 
76ec96d
2bc8286
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89f7e0d
2bc8286
 
 
76ec96d
89f7e0d
2bc8286
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c290e43
 
2bc8286
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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

# Configuration du logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Chargement des variables d'environnement
load_dotenv()

# Styles artistiques
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"
    }
}

# Configuration de l'API
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']}"
        
        # Configuration de la requête
        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()