Spaces:
Runtime error
Runtime error
ABANDA-OMGBA-Ulrich-Michel
commited on
Commit
•
fcb253a
1
Parent(s):
8b28c83
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from PIL import Image
|
3 |
+
from io import BytesIO
|
4 |
+
import numpy as np
|
5 |
+
import gradio as gr
|
6 |
+
from transformers import pipeline
|
7 |
+
|
8 |
+
image_to_text = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
9 |
+
|
10 |
+
def process_image(image):
|
11 |
+
|
12 |
+
if isinstance(image, str): # Si l'entrée est une URL
|
13 |
+
response = requests.get(image)
|
14 |
+
image = Image.open(BytesIO(response.content))
|
15 |
+
elif isinstance(image, np.ndarray): # Si l'entrée est un tableau numpy
|
16 |
+
image = Image.fromarray(image)
|
17 |
+
else: # Si l'entrée est un fichier local
|
18 |
+
image = Image.open(image.name)
|
19 |
+
|
20 |
+
result = image_to_text(image)
|
21 |
+
|
22 |
+
text_description = result[0]["generated_text"]
|
23 |
+
return text_description
|
24 |
+
|
25 |
+
imager = gr.Interface(process_image, inputs="image", outputs="text")
|
26 |
+
imager.launch(debug=True)
|
27 |
+
|