Spaces:
Runtime error
Runtime error
Commit ·
0709174
1
Parent(s): a42bfc7
Fix: add text parameter to image-text-to-text pipeline
Browse files
app.py
CHANGED
|
@@ -6,10 +6,8 @@ import time
|
|
| 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-text-to-text",
|
| 13 |
model="Salesforce/blip-image-captioning-base",
|
| 14 |
device=-1
|
| 15 |
)
|
|
@@ -19,25 +17,37 @@ print(f"✅ Modèle chargé en {time.time() - start:.1f} secondes")
|
|
| 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 |
-
#
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
|
@@ -51,7 +61,7 @@ with gr.Blocks(title="Générateur de titres d'images (CPU optimisé)") as demo:
|
|
| 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")
|
|
|
|
| 6 |
print("📥 Chargement du modèle léger (CPU optimisé)...")
|
| 7 |
start = time.time()
|
| 8 |
|
|
|
|
|
|
|
| 9 |
captioner = pipeline(
|
| 10 |
+
"image-text-to-text",
|
| 11 |
model="Salesforce/blip-image-captioning-base",
|
| 12 |
device=-1
|
| 13 |
)
|
|
|
|
| 17 |
def generate_caption(image, prompt_text=""):
|
| 18 |
"""Génère une légende pour l'image"""
|
| 19 |
try:
|
|
|
|
| 20 |
if hasattr(image, 'size'):
|
| 21 |
pil_image = image
|
| 22 |
else:
|
| 23 |
pil_image = Image.fromarray(image.astype('uint8'))
|
| 24 |
|
| 25 |
+
# Texte par défaut si aucun prompt fourni
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
if prompt_text and prompt_text.strip():
|
| 27 |
+
user_prompt = prompt_text
|
| 28 |
+
else:
|
| 29 |
+
user_prompt = "Describe this image in a short sentence:"
|
| 30 |
+
|
| 31 |
+
# Appel avec image ET texte
|
| 32 |
+
result = captioner(
|
| 33 |
+
pil_image,
|
| 34 |
+
text=user_prompt,
|
| 35 |
+
max_new_tokens=50
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Extraire la réponse
|
| 39 |
+
if isinstance(result, list) and len(result) > 0:
|
| 40 |
+
if 'generated_text' in result[0]:
|
| 41 |
+
caption = result[0]['generated_text']
|
| 42 |
+
else:
|
| 43 |
+
caption = str(result[0])
|
| 44 |
+
else:
|
| 45 |
+
caption = str(result)
|
| 46 |
|
| 47 |
+
return caption.strip()
|
| 48 |
except Exception as e:
|
| 49 |
return f"Erreur : {str(e)}"
|
| 50 |
|
|
|
|
| 51 |
with gr.Blocks(title="Générateur de titres d'images (CPU optimisé)") as demo:
|
| 52 |
gr.Markdown("""
|
| 53 |
# 🖼️ Générateur de titres d'images
|
|
|
|
| 61 |
image_input = gr.Image(label="📸 Uploadez votre image", type="pil")
|
| 62 |
prompt = gr.Textbox(
|
| 63 |
label="Style de titre (optionnel)",
|
| 64 |
+
placeholder="Exemple: 'Titre court :' ou 'Description détaillée :'",
|
| 65 |
lines=1
|
| 66 |
)
|
| 67 |
btn = gr.Button("🚀 Générer le titre", variant="primary")
|