pond918 commited on
Commit
8c76b59
·
1 Parent(s): bbaa137

feat: vdb model

Browse files
Files changed (4) hide show
  1. .gitignore +1 -0
  2. __pycache__/faiss_vdb.cpython-310.pyc +0 -0
  3. app.py +31 -12
  4. faiss_vdb.py +28 -0
.gitignore CHANGED
@@ -1 +1,2 @@
1
  env/
 
 
1
  env/
2
+ data/
__pycache__/faiss_vdb.cpython-310.pyc ADDED
Binary file (1.08 kB). View file
 
app.py CHANGED
@@ -1,20 +1,39 @@
1
  import gradio as gr
 
 
2
 
3
- def my_inference_function(name):
4
- return "Hello " + name + "!"
 
 
 
 
 
 
 
 
5
 
6
- gradio_interface = gr.Interface(
7
- fn = my_inference_function,
8
- inputs = "text",
9
- outputs = "text"
10
- )
11
- def image_classifier(inp):
12
- return {'cat': 0.3, 'dog': 0.7}
13
 
14
- demo0 = gr.Interface(fn=image_classifier, inputs="image", outputs="label")
 
15
  # gradio_interface.launch()
16
 
17
- demo = gr.TabbedInterface([gradio_interface, demo0], ["gradio_interface", "demo"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  if __name__ == "__main__":
20
- demo.launch()
 
1
  import gradio as gr
2
+ import json
3
+ import faiss_vdb as vdb
4
 
5
+ def upsert_role(roleId, data):
6
+ """
7
+ body: { meta, content }
8
+ """
9
+ text = json.dumps(data['content'])
10
+ meta = data['meta']
11
+ meta['id'] = roleId
12
+ vdb.upsert(text, meta)
13
+
14
+ return 'true'
15
 
 
 
 
 
 
 
 
16
 
17
+ i_upsert = gr.Interface(fn=upsert_role, inputs=["text", "json"],
18
+ outputs="text")
19
  # gradio_interface.launch()
20
 
21
+
22
+ def search_roles(content, size):
23
+ if size is None: size = 4
24
+ text = json.dumps(content)
25
+ docs = vdb.search(text, size)
26
+ return docs
27
+
28
+
29
+ i_search = gr.Interface(
30
+ fn=search_roles,
31
+ inputs=["json", "number"],
32
+ outputs="json"
33
+ )
34
+
35
+ demo = gr.TabbedInterface([i_upsert, i_search], [
36
+ "upsert role", "search roles"])
37
 
38
  if __name__ == "__main__":
39
+ demo.launch()
faiss_vdb.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.vectorstores import FAISS
2
+ from langchain.embeddings import HuggingFaceEmbeddings
3
+ import os
4
+ """
5
+ for development only.
6
+ """
7
+
8
+ db_path = './data'
9
+
10
+ embeddings = HuggingFaceEmbeddings(model_name='all-mpnet-base-v2')
11
+ db = FAISS.load_local(db_path, embeddings) if os.path.exists(
12
+ db_path) else FAISS.from_texts([''], embeddings)
13
+
14
+
15
+ def upsert(text, meta):
16
+ """
17
+ Args:
18
+ text: string
19
+ meta: dict, must contain id.
20
+ """
21
+ db.aadd_texts([text], metadatas=[meta], ids=[meta['id']])
22
+ db.save_local(db_path)
23
+
24
+
25
+ def search(text, size=4):
26
+ docs = db.similarity_search(text, size)
27
+ data = [doc.metadata for doc in docs if ('id' in doc.metadata)]
28
+ return data