Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Chargement des pipelines
|
| 5 |
+
captioneur_blip = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 6 |
+
captioneur_git = pipeline("image-to-text", model="microsoft/git-base")
|
| 7 |
+
translateur = pipeline("translation_en_to_fr", model="Helsinki-NLP/opus-mt-en-fr")
|
| 8 |
+
|
| 9 |
+
# Fonction : Captioning en anglais
|
| 10 |
+
def caption_en_anglais(image, nom_model):
|
| 11 |
+
if nom_model == "BLIP":
|
| 12 |
+
resultat = captioneur_blip(image)
|
| 13 |
+
else:
|
| 14 |
+
resultat = captioneur_git(image)
|
| 15 |
+
return resultat[0]['generated_text']
|
| 16 |
+
|
| 17 |
+
# Fonction : Captioning en anglais + traduction
|
| 18 |
+
def caption_en_francais(image, model_name):
|
| 19 |
+
caption_ang = caption_en_anglais(image, model_name)
|
| 20 |
+
caption_fra = translateur(caption_ang)[0]['translation_text']
|
| 21 |
+
return caption_fra
|
| 22 |
+
|
| 23 |
+
# Interface 1 : Captioning en anglais
|
| 24 |
+
anglais_interface = gr.Interface(
|
| 25 |
+
fn=caption_en_anglais,
|
| 26 |
+
inputs=[
|
| 27 |
+
gr.Image(type="pil", label="Téléverser une image"),
|
| 28 |
+
gr.Radio(["BLIP", "GIT"], label="Choisir un modèle", value="BLIP")
|
| 29 |
+
],
|
| 30 |
+
outputs=gr.Textbox(label="Légende en anglais"),
|
| 31 |
+
title="Image Captioning - Anglais",
|
| 32 |
+
description="Téléversez une image et choisissez un modèle pour obtenir une description en anglais."
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Interface 2 : Captioning en français
|
| 36 |
+
francais_interface = gr.Interface(
|
| 37 |
+
fn=caption_en_francais,
|
| 38 |
+
inputs=[
|
| 39 |
+
gr.Image(type="pil", label="Téléverser une image"),
|
| 40 |
+
gr.Radio(["BLIP", "GIT"], label="Choisir un modèle", value="BLIP")
|
| 41 |
+
],
|
| 42 |
+
outputs=gr.Textbox(label="Légende traduite en français"),
|
| 43 |
+
title="Image Captioning - Français",
|
| 44 |
+
description="Téléversez une image, obtenez une légende en anglais automatiquement traduite en français."
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# Lancer l'application avec navigation entre les deux
|
| 48 |
+
demo = gr.TabbedInterface(
|
| 49 |
+
interface_list=[anglais_interface, francais_interface],
|
| 50 |
+
tab_names=["Captioning Anglais", "Captioning Français"],
|
| 51 |
+
theme='JohnSmith9982/small_and_pretty'
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
demo.launch()
|