import gradio as gr 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 STYLE = """ """ 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 = [] HTML = "" for i in range(len(indexes)): index = str(indexes[i]) mapping = mappings[index] url = mapping["url"] if url != "": images.append(url) score = round((1 - scores[i]) * 100, 1) HTML += f"""
{mapping[
{mapping["name"]}

Rank: {i+1}, Similarity: {score} %

View at 「電子展示『捃拾帖』(拡張版)」

""" return f"""{STYLE}
{HTML}
""" 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 great open-source libraries such as PyTorch and Annoy.""", article=get_article_text(), inputs=[ gr.inputs.Image(shape=None, label="Your image"), gr.inputs.Slider(minimum=1, maximum=30, step=1, default=10, label="Number of matches"), ], # outputs=gr.Gallery(label="類似する貼り込み資料").style(height="100px"), outputs=gr.HTML(label="類似する貼り込み資料"), examples=[ ["images/025_0085.jpg", 10], ["images/046_0051.jpg", 10], ], ) iface.launch()