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