Spaces:
Runtime error
Runtime error
Commit
·
7d41d39
1
Parent(s):
afe6a3f
update
Browse files- app.py +28 -12
- requirements.txt +3 -1
app.py
CHANGED
|
@@ -6,7 +6,8 @@ import httpx
|
|
| 6 |
import io
|
| 7 |
from PIL import Image
|
| 8 |
import PIL
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
HF_MODEL_PATH = (
|
| 12 |
"ImageIN/levit-192_finetuned_on_unlabelled_IA_with_snorkel_labels"
|
|
@@ -19,9 +20,6 @@ classif_pipeline = pipeline(
|
|
| 19 |
"image-classification", model=classif_model, feature_extractor=feature_extractor
|
| 20 |
)
|
| 21 |
|
| 22 |
-
OUTPUT_SENTENCE = "This image is {result}."
|
| 23 |
-
|
| 24 |
-
|
| 25 |
def load_manifest(inputs):
|
| 26 |
with requests.get(inputs) as r:
|
| 27 |
return r.json()
|
|
@@ -53,26 +51,44 @@ async def get_images(urls):
|
|
| 53 |
async with httpx.AsyncClient() as client:
|
| 54 |
tasks = [asyncio.ensure_future(get_image(client, url)) for url in urls]
|
| 55 |
images = await asyncio.gather(*tasks)
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
|
| 59 |
def predict(inputs):
|
| 60 |
data = load_manifest(inputs)
|
| 61 |
urls = get_image_urls_from_manifest(data)
|
| 62 |
resized_urls = [resize_iiif_urls(url) for url in urls]
|
| 63 |
-
|
| 64 |
predicted_images = []
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
| 67 |
if top_pred['label'] == 'illustrated':
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
demo = gr.Interface(
|
| 73 |
fn=predict,
|
| 74 |
inputs=gr.Text(),
|
| 75 |
-
outputs=
|
| 76 |
title="ImageIN",
|
| 77 |
description="Identify illustrations in pages of historical books!",
|
| 78 |
)
|
|
|
|
| 6 |
import io
|
| 7 |
from PIL import Image
|
| 8 |
import PIL
|
| 9 |
+
from toolz import pluck
|
| 10 |
+
from piffle.image import IIIFImageClient
|
| 11 |
|
| 12 |
HF_MODEL_PATH = (
|
| 13 |
"ImageIN/levit-192_finetuned_on_unlabelled_IA_with_snorkel_labels"
|
|
|
|
| 20 |
"image-classification", model=classif_model, feature_extractor=feature_extractor
|
| 21 |
)
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
def load_manifest(inputs):
|
| 24 |
with requests.get(inputs) as r:
|
| 25 |
return r.json()
|
|
|
|
| 51 |
async with httpx.AsyncClient() as client:
|
| 52 |
tasks = [asyncio.ensure_future(get_image(client, url)) for url in urls]
|
| 53 |
images = await asyncio.gather(*tasks)
|
| 54 |
+
assert len(images) == len(urls)
|
| 55 |
+
image_url_tuples = []
|
| 56 |
+
for url, image in zip(urls, images):
|
| 57 |
+
if image is not None:
|
| 58 |
+
image_url_tuples.append((url, image))
|
| 59 |
+
return image_url_tuples
|
| 60 |
+
# return [image for image in images if image is not None]
|
| 61 |
|
| 62 |
|
| 63 |
def predict(inputs):
|
| 64 |
data = load_manifest(inputs)
|
| 65 |
urls = get_image_urls_from_manifest(data)
|
| 66 |
resized_urls = [resize_iiif_urls(url) for url in urls]
|
| 67 |
+
images_urls = asyncio.run(get_images(resized_urls))
|
| 68 |
predicted_images = []
|
| 69 |
+
images = list(pluck(1, images_urls))
|
| 70 |
+
urls = list(pluck(0, images_urls))
|
| 71 |
+
predictions = classif_pipeline(images, top_k=1)
|
| 72 |
+
for url, pred in zip(urls,predictions):
|
| 73 |
+
top_pred = pred[0]
|
| 74 |
if top_pred['label'] == 'illustrated':
|
| 75 |
+
image_url = IIIFImageClient.init_from_url(url)
|
| 76 |
+
image_url = image_url.canonicalize()
|
| 77 |
+
predicted_images.append((image_url.__str__(), f"Confidence: {top_pred['score']}, page: {10}"))
|
| 78 |
+
return predicted_images
|
| 79 |
+
# for image in images:
|
| 80 |
+
# top_pred = classif_pipeline(image, top_k=1)[0]
|
| 81 |
+
# if top_pred['label'] == 'illustrated':
|
| 82 |
+
# predicted_images.append((image, top_pred['score']))
|
| 83 |
+
# return predicted_images
|
| 84 |
+
|
| 85 |
+
gallery = gr.Gallery()
|
| 86 |
+
gallery.style(grid=3)
|
| 87 |
|
| 88 |
demo = gr.Interface(
|
| 89 |
fn=predict,
|
| 90 |
inputs=gr.Text(),
|
| 91 |
+
outputs=gallery,
|
| 92 |
title="ImageIN",
|
| 93 |
description="Identify illustrations in pages of historical books!",
|
| 94 |
)
|
requirements.txt
CHANGED
|
@@ -1,3 +1,5 @@
|
|
| 1 |
transformers
|
| 2 |
httpx
|
| 3 |
-
torch
|
|
|
|
|
|
|
|
|
| 1 |
transformers
|
| 2 |
httpx
|
| 3 |
+
torch
|
| 4 |
+
toolz
|
| 5 |
+
piffle
|