File size: 2,945 Bytes
7baafc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de99784
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7baafc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from dotenv import load_dotenv
load_dotenv("docker/.env")

from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
from src.mongo_search import MongoSearch
from src.image_search import ImageSearcher

import os
import gradio as gr

uri = os.getenv("MONGO_URI")
db = os.getenv("MONGO_DB")
cars_collection = os.getenv("MONGO_CARS_COLLECTION")
image_collection = os.getenv("MONGO_IMAGE_COLLECTION")
img_search_index = os.getenv("MONGO_SEARCH_IMG_INDEX")
car_search_index = os.getenv("MONGO_SEARCH_CAR_INDEX")

client = MongoClient(uri, server_api=ServerApi('1'))
db = client[db]
cars_collection = db[cars_collection]
image_collection = db[image_collection]

mongo_search = MongoSearch(cars_collection, car_search_index, index_variable="review_embedding") #desc_embedding
image_searcher = ImageSearcher(image_collection, 
                                cars_collection, 
                                img_search_index)

def search_mongo(search_query):
    object = mongo_search(search_query)
    return object[0]['makeModel'], object[0]['photoUrls']


def search_img(image):

    result = image_searcher(image)['results']
    return result[0]['makeModel'], result[0]['photoUrls']



with gr.Blocks() as demo:

    with gr.Tab("Text search"):
        with gr.Column():
            with gr.Row():
                with gr.Column():
                    text_output_img_1 = gr.Gallery(height=300, selected_index=0)
                    text_output_text_1 = gr.Textbox(label='Output car model', show_label=True)

                with gr.Column():
                    text_output_img_2 = gr.Gallery(height=300, selected_index=0)
                    text_output_text_2 = gr.Textbox(label='Output car model', show_label=True)

                with gr.Column():
                    text_output_img_3 = gr.Gallery(height=300, selected_index=0)
                    text_output_text_3 = gr.Textbox(label='Output car model', show_label=True)

                with gr.Column():
                    text_output_img_4 = gr.Gallery(visible=False)
                    text_output_text_4 = gr.Textbox(label='Output car model', show_label=True, visible=False)

        text_input_text = gr.Textbox(label='Search Query', show_label=True)
        text_btn  = gr.Button()


    with gr.Tab("Image search"):
        with gr.Row():
            with gr.Column():
                input_img = gr.Image(type='pil')
                img_btn  = gr.Button()
            with gr.Column():
                img_output_img = gr.Gallery(preview=True)
                img_output_text = gr.Textbox(label='Output car model', show_label=True)

    text_btn.click(search_mongo,
                    inputs=text_input_text,
                    outputs=[text_output_text, text_output_img])
    

    img_btn.click(search_img,
                inputs=input_img,
                outputs=[img_output_text, img_output_img])


if __name__ == "__main__":
    demo.launch()