Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from util import download | |
| import os | |
| from dotenv import load_dotenv | |
| from photo_search import PhotoSearch | |
| from PIL import Image | |
| load_dotenv() | |
| tree_path = download.gdrive(os.getenv('TREE_GDRIVE_ID'), dir='temp') | |
| data_path = download.gdrive(os.getenv('DATA_GDRIVE_ID'), dir='temp') | |
| # tree_path= 'temp/trees-pocari.ann' | |
| # data_path = 'temp/drive_ids_pocari.json' | |
| ai = PhotoSearch(tree_path, data_path) | |
| htmlPref = '<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Image Gallery</title><style>body{font-family:Arial,sans-serif;margin:0;padding:0}.gallery{display:grid;grid-template-columns:repeat(3,1fr);gap:10px}.gallery img{width:100%;height:auto;display:block}</style></head><body>' | |
| def search_photo(image): | |
| try: | |
| found_ids = ai.search(image) | |
| if len(found_ids) == 0: | |
| return ''' | |
| <h2>Foto Serupa Tidak Ditemukan, Tips: </h2> <br/> | |
| <p> | |
| - Gunakan foto terupdate, | |
| - Jika kamu punya foto saat lari, lebih baik gunakan foto itu, | |
| - Jangan menggunakan foto blur, | |
| - Jangan upload foto yang menggunakan aksesoris yang tidak kamu gunakan saat lari (semisal kacamata) | |
| </p> | |
| ''' | |
| # result = f"{len(found_ids)} foto kamu berhasil ditemukan!" | |
| link_format = [f'<a href="{link}" target="_blank"> <img src="{link}" height="150"></img></a>' for i, link in enumerate(found_ids)] | |
| return htmlPref + f''' | |
| <h2> {len(found_ids)} Foto Ditemukan! </h2> | |
| <p> Klik foto untuk memperbesar atau download </p> | |
| <div class="gallery"> | |
| {"".join(link_format)} | |
| </div></body></html> | |
| ''' | |
| except Exception as e: | |
| result = f"Gagal: {e}" | |
| return result | |
| gr_inputs = [ | |
| gr.Image(type='pil') | |
| ] | |
| gallery = gr.Gallery( | |
| label="Generated images", show_label=False, elem_id="gallery" | |
| ).style(columns=[2], rows=[2], object_fit="contain", height="auto") | |
| gr.Markdown('Upload Foto Wajah Kamu Ya (Pastikan hanya terdapat SATU wajah pada foto yang kamu upload)') | |
| iface = gr.Interface(fn=search_photo, inputs=gr_inputs, outputs="html") | |
| iface.launch() |