File size: 1,149 Bytes
2069ee0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b83f839
2069ee0
 
 
b83f839
 
 
 
 
 
2069ee0
 
b83f839
 
 
 
 
 
 
 
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
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(conn, 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)

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")
with gr.Blocks():
    p = gr.Textbox()
    f = gr.Textbox()
    n = gr.Textbox()
    btn = gr.Button(value="insert")
    btn.click(greet, inputs=[p, f, 0], outputs=[n])
    btn = gr.Button(value="query")
    btn.click(greet, inputs=[p, f, 1], outputs=[n])
# demo.launch()
demo.launch(auth=("username", "password"))