File size: 817 Bytes
fcb253a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
from PIL import Image
from io import BytesIO
import numpy as np
import gradio as gr
from transformers import pipeline

image_to_text = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")

def process_image(image):

    if isinstance(image, str):  # Si l'entrée est une URL
        response = requests.get(image)
        image = Image.open(BytesIO(response.content))
    elif isinstance(image, np.ndarray):  # Si l'entrée est un tableau numpy
        image = Image.fromarray(image)
    else:  # Si l'entrée est un fichier local
        image = Image.open(image.name)

    result = image_to_text(image)

    text_description = result[0]["generated_text"]
    return text_description

imager = gr.Interface(process_image, inputs="image", outputs="text")
imager.launch(debug=True)