YiHuan commited on
Commit
93d3aa5
·
1 Parent(s): 547cfe4

Upload 11 files

Browse files
app.py CHANGED
@@ -1,7 +1,71 @@
1
- import gradio as gr
2
-
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
-
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import chromadb
2
+ from chromadb.api.types import Documents, EmbeddingFunction, Embeddings
3
+ import json
4
+ from ast import literal_eval
5
+ from chromadb.config import Settings
6
+ from paddlenlp import Taskflow
7
+ import requests
8
+ from io import BytesIO
9
+ from PIL import Image
10
+ import gradio as gr
11
+
12
+ vision_language=Taskflow("feature_extraction", model='PaddlePaddle/ernie_vil-2.0-base-zh')
13
+
14
+ def getImageTestFeture(content):
15
+ if content.startswith("http"):
16
+ response = requests.get(content)
17
+ x=BytesIO(response.content)
18
+ f_embeds = vision_language(Image.open(x))
19
+ else:
20
+ f_embeds = vision_language(content)
21
+ text_features = f_embeds["features"][0]
22
+ return text_features
23
+
24
+ class MyEmbeddingFunction(EmbeddingFunction):
25
+ def __call__(self, texts: Documents) -> Embeddings:
26
+ qr=[]
27
+ for doc in texts:
28
+ text_embeds = getImageTestFeture(doc)
29
+ #print(len(text_features))
30
+ bedx=text_embeds.tolist()
31
+ qr.append(bedx)
32
+ return qr
33
+
34
+ client = chromadb.Client(Settings(
35
+ chroma_db_impl="duckdb+parquet",
36
+ persist_directory="x/" # Optional, defaults to .chromadb/ in the current directory
37
+ ))
38
+
39
+ collection = client.get_or_create_collection(name="pics", metadata={"hnsw:space": "cosine"}, embedding_function=MyEmbeddingFunction())
40
+
41
+ def queryimgage(text):
42
+ html="<table border='1'>\
43
+ <tr>\
44
+ <th>img</th>\
45
+ <th>score</th>\
46
+ </tr>"
47
+ atext=[]
48
+ atext.append(text)
49
+ results = collection.query(
50
+ query_texts=atext,
51
+ n_results=20,
52
+ )
53
+ ids=results['ids'][0]
54
+ documents=results['documents'][0]
55
+ distances=results['distances'][0]
56
+ xcount=len(ids)
57
+ for i in range(xcount):
58
+ #print("id:%s,url:%s,score:%s"%(ids[i],documents[i],distances[i]))
59
+ html=html +"<tr>\
60
+ <td><img src='"+documents[i]+"' width=640></td>\
61
+ <td>"+ str(distances[i])+"</td>"
62
+ html=html+"</table>"
63
+ return html
64
+
65
+ demo = gr.Interface(
66
+ queryimgage,
67
+ gr.Textbox(placeholder="请输入文本"),
68
+ [ "html"]
69
+ )
70
+
71
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ paddlenlp==2.5.2
2
+ paddlepaddle==2.4.2
3
+ chromadb==0.3.25
4
+ gradio_client
5
+ gradio
x/chroma-collections.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:554ff0df5e9812d02ed6f1799cc156dbdf4583691533b8a8e47ae519e8346fe5
3
+ size 632
x/chroma-embeddings.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f201a6d14a19d8cb114b5707496fb2f9d55e49ca98737d7c7a99f1680c49d068
3
+ size 253961910
x/chroma-embeddings.parquet.tmp ADDED
File without changes
x/index/id_to_uuid_6ef81d52-9bd0-4320-8b96-96ac8a8b7377.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9e189f5cdbfd336b24e474d5f4380f1e2ef13a184e3aef0823f07568923ac730
3
+ size 1744300
x/index/index_6ef81d52-9bd0-4320-8b96-96ac8a8b7377.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9c0bd55c4333b7102a60769143f6169b0a2cbb62b118c8acfca8dc6096985c58
3
+ size 172845576
x/index/index_metadata_6ef81d52-9bd0-4320-8b96-96ac8a8b7377.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ad68245d2bea577410b12201c810098c19d15eff0d735b3fe2c105eb0b99d83a
3
+ size 105
x/index/uuid_to_id_6ef81d52-9bd0-4320-8b96-96ac8a8b7377.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:caa7c4ad9e40a9f9531d3ed82978fea866276cfa94bb30652e228fa5722e483b
3
+ size 2039606