Spaces:
Runtime error
Runtime error
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) | |