File size: 1,668 Bytes
06c6776
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import fn
import gradio as gr
from refresh import create_refresh_button

def fn_search(dir, query):
    args = {
        'dir': dir,
        'query': query,
        'k': 1,
    }
    vector_store = fn.vector_load(args)
    result, detail = fn.search(vector_store, args)

    return result

with gr.Blocks() as demo:
    title = gr.Markdown('# FAISS(Naive RAG)')
    info = gr.Markdown()
    upload_dir = gr.Dropdown(
        choices=fn.load_dirs(),
        label='name',
        show_label=True,
        interactive=True,
        allow_custom_value=True,
    )

    with gr.Tab('Upload'):
        chunk_size = gr.Textbox(
            value=0,
            label='(optional) chunk_size if split',
            show_label=True,
            interactive=True,
        )
        rag_zip = gr.UploadButton(
            label='Zip Upload and save rag_dir',
            interactive=True,
        )
        create_refresh_button(gr, upload_dir, lambda: None, lambda: {'choices': fn.load_dirs()}, 'refresh-button', interactive=True)

    with gr.Tab('Search'):
        query = gr.Textbox(
            lines=1,
            label='query',
            show_label=True,
            interactive=True,
            show_copy_button=True,
            )
        search_button = gr.Button(value='search')
        result = gr.Textbox(label='result', show_label=True, show_copy_button=True)

    search_button.click(
        fn=fn_search,
        inputs=[upload_dir, query],
        outputs=[result],
        )
        
    rag_zip.upload(
        fn=fn.upload,
        inputs=[upload_dir, chunk_size, rag_zip],
        outputs=[info],
        )

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