Spaces:
Runtime error
Runtime error
Commit ·
2e8c265
1
Parent(s): 8a11eef
Premier déploiement - captionneur d'images CPU optimisé
Browse files- app.py +68 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
print("📥 Chargement du modèle léger (CPU optimisé)...")
|
| 7 |
+
start = time.time()
|
| 8 |
+
|
| 9 |
+
# Modèle BLIP-base (990M paramètres) - bon compromis qualité/vitesse
|
| 10 |
+
# Alternative plus rapide : "nlpconnect/vit-gpt2-image-captioning"
|
| 11 |
+
captioner = pipeline(
|
| 12 |
+
"image-to-text",
|
| 13 |
+
model="Salesforce/blip-image-captioning-base",
|
| 14 |
+
device=-1 # Force CPU
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
print(f"✅ Modèle chargé en {time.time() - start:.1f} secondes")
|
| 18 |
+
|
| 19 |
+
def generate_caption(image, prompt_text=""):
|
| 20 |
+
"""Génère une légende pour l'image"""
|
| 21 |
+
try:
|
| 22 |
+
# Convertir l'image PIL
|
| 23 |
+
if hasattr(image, 'size'):
|
| 24 |
+
pil_image = image
|
| 25 |
+
else:
|
| 26 |
+
pil_image = Image.fromarray(image.astype('uint8'))
|
| 27 |
+
|
| 28 |
+
# Générer la légende (max 50 tokens pour rester rapide)
|
| 29 |
+
result = captioner(pil_image, max_new_tokens=50)
|
| 30 |
+
caption = result[0]['generated_text']
|
| 31 |
+
|
| 32 |
+
# Ajouter le prompt personnalisé si fourni
|
| 33 |
+
if prompt_text and prompt_text.strip():
|
| 34 |
+
return f"{prompt_text} {caption}"
|
| 35 |
+
|
| 36 |
+
return caption
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return f"Erreur : {str(e)}"
|
| 39 |
+
|
| 40 |
+
# Interface simple et efficace
|
| 41 |
+
with gr.Blocks(title="Générateur de titres d'images (CPU optimisé)") as demo:
|
| 42 |
+
gr.Markdown("""
|
| 43 |
+
# 🖼️ Générateur de titres d'images
|
| 44 |
+
**Temps de réponse : 30-60 secondes par image**
|
| 45 |
+
- Modèle BLIP-base (990M paramètres)
|
| 46 |
+
- Optimisé pour le CPU gratuit
|
| 47 |
+
""")
|
| 48 |
+
|
| 49 |
+
with gr.Row():
|
| 50 |
+
with gr.Column(scale=1):
|
| 51 |
+
image_input = gr.Image(label="📸 Uploadez votre image", type="pil")
|
| 52 |
+
prompt = gr.Textbox(
|
| 53 |
+
label="Style de titre (optionnel)",
|
| 54 |
+
placeholder="Exemple: 'Titre court :' ou 'Description :'",
|
| 55 |
+
lines=1
|
| 56 |
+
)
|
| 57 |
+
btn = gr.Button("🚀 Générer le titre", variant="primary")
|
| 58 |
+
|
| 59 |
+
with gr.Column(scale=1):
|
| 60 |
+
output = gr.Textbox(label="🏷️ Titre généré", lines=4)
|
| 61 |
+
|
| 62 |
+
btn.click(
|
| 63 |
+
fn=generate_caption,
|
| 64 |
+
inputs=[image_input, prompt],
|
| 65 |
+
outputs=output
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
pillow
|