Spaces:
Build error
Build error
File size: 4,567 Bytes
2396bb6 4d75a35 2396bb6 2bb7882 4d75a35 2396bb6 4d75a35 2396bb6 4d75a35 2396bb6 4d75a35 2396bb6 4d75a35 2396bb6 60d8e68 2396bb6 4d75a35 2396bb6 68c4225 2396bb6 60d8e68 68c4225 60d8e68 4d75a35 68c4225 60d8e68 68c4225 60d8e68 2960be9 60d8e68 68c4225 4d75a35 2396bb6 4d75a35 2396bb6 4d75a35 68c4225 4d75a35 2396bb6 2bb7882 4d75a35 2bb7882 4d75a35 2bb7882 4d75a35 2396bb6 a3920dd 4d75a35 2396bb6 60d8e68 2396bb6 4d75a35 2bb7882 2396bb6 2bb7882 4d75a35 2396bb6 4d75a35 68c4225 2396bb6 60d8e68 2bb7882 2396bb6 4d75a35 2bb7882 2396bb6 68c4225 2396bb6 60d8e68 4d75a35 60d8e68 2396bb6 | 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | #!/usr/bin/env python3
"""
Lambert AI Image Generator - Hugging Face Inference API
Fiable et rapide sur HF Spaces
"""
import gradio as gr
import requests
import io
import os
from PIL import Image
from functools import wraps
import time
# ========== CONFIGURATION ==========
API_KEY = os.environ.get("API_KEY")
HF_TOKEN = os.environ.get("HF_TOKEN") # Token Hugging Face
if not HF_TOKEN:
print("⚠️ HF_TOKEN non configuré dans les secrets")
def check_auth(api_key):
return api_key == API_KEY
def require_auth(func):
@wraps(func)
def wrapper(api_key, *args, **kwargs):
if not check_auth(api_key):
return None, "🔒 Clé API invalide"
return func(*args, **kwargs)
return wrapper
# ========== GÉNÉRATION VIA API HUGGING FACE ==========
@require_auth
def generate_image(prompt, style, width, height, seed):
try:
# Styles
styles = {
"✨ Général": "",
"📸 Photo réaliste": "photorealistic, realistic, 8k, sharp focus, detailed",
"🎨 Artistique": "digital art, artistic, vibrant colors, masterpiece, trending on artstation",
"🎬 Cinématique": "cinematic, dramatic lighting, epic composition, movie poster"
}
style_suffix = styles.get(style, "")
if style_suffix:
final_prompt = f"{prompt}, {style_suffix}"
else:
final_prompt = prompt
print(f"🎨 Génération: {final_prompt[:50]}...")
# API Hugging Face - Modèle SDXL Turbo (rapide)
API_URL = "https://api-inference.huggingface.co/models/stabilityai/sdxl-turbo"
headers = {
"Authorization": f"Bearer {HF_TOKEN}"
}
payload = {
"inputs": final_prompt,
"parameters": {
"negative_prompt": "blurry, ugly, low quality, deformed",
"num_inference_steps": 4,
"guidance_scale": 0.0
}
}
response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
if response.status_code == 200:
# Convertir la réponse en image
image = Image.open(io.BytesIO(response.content))
temp_path = "/tmp/generated_image.png"
image.save(temp_path)
return temp_path, f"✅ Image générée ! Prompt: {final_prompt[:80]}"
else:
return None, f"❌ Erreur API: {response.status_code} - {response.text}"
except requests.exceptions.Timeout:
return None, "⏰ Timeout. Réessayez."
except Exception as e:
print(f"❌ Erreur: {e}")
return None, f"❌ {str(e)}"
# ========== INTERFACE GRADIO ==========
with gr.Blocks(title="Lambert AI - Image Generator") as demo:
gr.Markdown("""
# 🎨 Lambert AI - Image Generator
### Propulsé par Hugging Face Inference API
""")
with gr.Row():
with gr.Column(scale=1):
api_key_input = gr.Textbox(
label="🔒 Clé API Lambert",
type="password",
placeholder="Votre clé API"
)
prompt = gr.Textbox(
label="📝 Description",
lines=3,
placeholder="Ex: un chat mignon avec un chapeau de magicien"
)
style = gr.Dropdown(
choices=["✨ Général", "📸 Photo réaliste", "🎨 Artistique", "🎬 Cinématique"],
value="✨ Général",
label="🎭 Style"
)
with gr.Row():
width = gr.Dropdown(choices=[512, 768, 1024], value=768, label="Largeur")
height = gr.Dropdown(choices=[512, 768, 1024], value=768, label="Hauteur")
seed = gr.Number(value=-1, precision=0, label="🎲 Seed (-1 = aléatoire)")
btn = gr.Button("🚀 Générer l'image", variant="primary")
with gr.Column(scale=1):
output = gr.Image(label="🖼️ Image générée", height=450)
status = gr.Markdown("💡 Entrez votre clé API")
btn.click(
fn=generate_image,
inputs=[api_key_input, prompt, style, width, height, seed],
outputs=[output, status]
)
if __name__ == "__main__":
print("=" * 50)
print("✨ Lambert AI - Hugging Face API")
print("🎨 Modèle: SDXL Turbo")
print("⚡ Génération rapide et fiable")
print("=" * 50)
demo.launch(server_name="0.0.0.0", server_port=7860) |