File size: 1,290 Bytes
2069ee0
 
 
 
 
 
 
 
925a98e
 
 
2069ee0
 
 
 
 
 
 
b83f839
2069ee0
b6ea566
2069ee0
b83f839
 
 
 
 
 
2069ee0
4931548
054992f
4931548
b83f839
 
9a9d27e
 
 
4931548
9a9d27e
 
2069ee0
b83f839
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
import gradio as gr
import duckdb
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import DuckDB

conn = duckdb.connect('your_database.duckdb')

embedding_function = HuggingFaceEmbeddings()
vector_store = DuckDB(
    connection=conn,
    embedding=embedding_function)

# Define a data structure for user data
class User:
    def __init__(self, phone: str, features: str):
        self.phone = phone
        self.features = features
    def create(self):
        vector_store.add_texts([f'#features\n{self.features}\n\n#phone\n{self.phone}'])
    def search(self):
        return vector_store.similarity_search(self.features, k=1)[0].page_content

def greet(a, b, c):
    u = User(a, b)
    if c:
        return u.search()
    u.create()
    return 1

# demo = gr.Interface(fn=greet, inputs=["textbox", "button"], outputs="text")
demo = gr.Blocks()
with demo:
    p = gr.Textbox()
    f = gr.Textbox()
    with gr.Row():
        btn = gr.Button("insert")
        btn2 = gr.Button("query")
    n = gr.Textbox()
    btn.click(greet, inputs=[p, f, gr.Number(value=0, visible=False)], outputs=[n])
    btn2.click(greet, inputs=[p, f, gr.Number(value=1, visible=False)], outputs=[n])
# demo.launch()
demo.launch(auth=("username", "password"))