File size: 1,438 Bytes
cab1b96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import gradio as gr

import numpy as np
import re
from pathlib import Path
from api import load_annoy_index, analyze_image

annoy_index, mappings = load_annoy_index()

def get_article_text():
    article = Path("README.md").read_text()
    # Remove the HuggingFace Space app information from the README
    article = re.sub(r"^---.+---\s+", "", article, flags=re.MULTILINE + re.DOTALL)
    return article



def find_matching_images(input_img, n_matches: int = 10):
    results = analyze_image(input_img, annoy_index, n_matches=n_matches)
    

    indexes = results[0]
    # scores = results[1]

    images = []

    for i in range(len(indexes)):
        index = str(indexes[i])

        mapping = mappings[index]

        url = mapping["url"]
        if url != "":
          images.append(url)

    return images

iface = gr.Interface(
    find_matching_images,
    title="類似貼り込み資料検索",
    description="""類似する貼り込み資料を検索します。 Upload a picture and find out!
    Give it a shot or try one of the sample images below.
    
    Built with ❤️ using great open-source libraries such as PyTorch and Annoy.""",
    article=get_article_text(),
    inputs=[
        gr.inputs.Image(shape=None, label="Your image"),
    ],
    outputs=gr.Gallery(label="類似する貼り込み資料"),
    examples=[
        ["images/025_0085.jpg"],
        ["images/046_0051.jpg"],
    ],
)

iface.launch()