Spaces:
Runtime error
Runtime error
Create new file
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from docarray import Document
|
3 |
+
from jina import Flow
|
4 |
+
import clip_server
|
5 |
+
from clip_client import Client
|
6 |
+
import asyncio
|
7 |
+
|
8 |
+
def setupServer():
|
9 |
+
cas_path = clip_server.__path__[0]
|
10 |
+
flow_yaml = f'''
|
11 |
+
jtype: Flow
|
12 |
+
with:
|
13 |
+
protocol: grpc
|
14 |
+
port: 51000
|
15 |
+
executors:
|
16 |
+
- name: clip_t
|
17 |
+
uses:
|
18 |
+
jtype: CLIPEncoder
|
19 |
+
metas:
|
20 |
+
py_modules:
|
21 |
+
- {cas_path}/executors/clip_torch.py
|
22 |
+
'''
|
23 |
+
|
24 |
+
f = Flow.load_config(flow_yaml)
|
25 |
+
f.start()
|
26 |
+
|
27 |
+
def setupClient():
|
28 |
+
c = Client('https://demo-cas.jina.ai:8443')
|
29 |
+
return c
|
30 |
+
|
31 |
+
# setupServer()
|
32 |
+
c = setupClient()
|
33 |
+
|
34 |
+
def rank(uri, options):
|
35 |
+
r = c.rank([
|
36 |
+
Document(
|
37 |
+
uri=uri,
|
38 |
+
matches=[
|
39 |
+
Document(text=f'a photo of a {p}')
|
40 |
+
for p in options.split(',')
|
41 |
+
],
|
42 |
+
)
|
43 |
+
])
|
44 |
+
return r['@m', ['text', 'scores__clip_score__value']]
|
45 |
+
|
46 |
+
examples = [["https://picsum.photos/id/102/300/300", "three berry, four berries, ten berries"]]
|
47 |
+
iface = gr.Interface(fn=rank, inputs=["text", "text"], outputs="text", examples=examples)
|
48 |
+
iface.launch()
|