複数の検索結果表示に対応
Browse files
app.py
CHANGED
@@ -2,6 +2,8 @@ import gradio as gr
|
|
2 |
import json
|
3 |
from pathlib import Path
|
4 |
import re
|
|
|
|
|
5 |
|
6 |
fileid2json = json.loads(Path("fileid2json.json").read_text())
|
7 |
fileid2image = json.loads(Path("fileid2image.json").read_text())
|
@@ -9,33 +11,61 @@ fileid2image = json.loads(Path("fileid2image.json").read_text())
|
|
9 |
autov22fileid = json.loads(Path("autov22fileid.json").read_text())
|
10 |
filename2fileid = json.loads(Path("filename2fileid.json").read_text())
|
11 |
name2fileid = json.loads(Path("name2fileid.json").read_text())
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
def greet(query):
|
15 |
-
|
16 |
fileid = query
|
17 |
hit = None
|
18 |
|
19 |
if query.upper() in autov22fileid:
|
20 |
fileid = str(autov22fileid[query.upper()])
|
|
|
21 |
hit = True
|
22 |
if re.sub(r'\..*$', "", query) in filename2fileid:
|
23 |
fileid = str(filename2fileid[re.sub(r'\..*$', "", query)])
|
|
|
24 |
hit = True
|
25 |
if query in name2fileid:
|
26 |
fileid = str(name2fileid[query])
|
|
|
27 |
hit = True
|
28 |
if hit is not True:
|
29 |
for k, v in [(k.lower(), v) for k, v in name2fileid.items()]:
|
30 |
if re.search(re.compile(query), k):
|
31 |
fileid = str(v)
|
|
|
|
|
|
|
32 |
|
33 |
-
|
34 |
-
result = json.dumps(fileid2json[fileid.strip()])
|
35 |
-
src = "https://huggingface.co/front/assets/huggingface_logo-noborder.svg"
|
36 |
-
if fileid in fileid2image:
|
37 |
-
src = fileid2image[fileid.strip()]
|
38 |
-
return result, '<img src="{}" style="max-height: 240px">'.format(src)
|
39 |
-
|
40 |
-
iface = gr.Interface(fn=greet, inputs="text", outputs=["text", "html"])
|
41 |
iface.launch(server_name="0.0.0.0")
|
|
|
2 |
import json
|
3 |
from pathlib import Path
|
4 |
import re
|
5 |
+
from bs4 import BeautifulSoup
|
6 |
+
import base64
|
7 |
|
8 |
fileid2json = json.loads(Path("fileid2json.json").read_text())
|
9 |
fileid2image = json.loads(Path("fileid2image.json").read_text())
|
|
|
11 |
autov22fileid = json.loads(Path("autov22fileid.json").read_text())
|
12 |
filename2fileid = json.loads(Path("filename2fileid.json").read_text())
|
13 |
name2fileid = json.loads(Path("name2fileid.json").read_text())
|
14 |
+
|
15 |
+
def fileids2html(fileids, query):
|
16 |
+
html = BeautifulSoup("<div></div>", "html.parser")
|
17 |
+
for fileid in sorted(list(fileids), reverse=True):
|
18 |
+
src = "https://huggingface.co/front/assets/huggingface_logo-noborder.svg"
|
19 |
+
if fileid in fileid2image:
|
20 |
+
src = fileid2image[fileid.strip()]
|
21 |
+
div = html.new_tag("div", style="height:16rem;")
|
22 |
+
h1 = html.new_tag("h1")
|
23 |
+
a = html.new_tag("a", target="_blank")
|
24 |
+
a.append(str(fileid))
|
25 |
+
h1.append(a)
|
26 |
+
textarea = html.new_tag("textarea", dir="ltr", style='color:var(--body-text-color);display:inline-block;overflow:auto;width:calc(100% - 256px);height:100%;background:var(--block-background-fill);')
|
27 |
+
html.append(h1)
|
28 |
+
html.append(div)
|
29 |
+
div.append(textarea)
|
30 |
+
div.append(html.new_tag("img", src=src, style='display:inline-block;max-width:256px;height:100%;vertical-align:initial;'))
|
31 |
+
if fileid in fileid2json:
|
32 |
+
j = fileid2json[fileid.strip()]
|
33 |
+
a["href"] = j["notes"]
|
34 |
+
textarea.append(json.dumps(j, indent=2, ensure_ascii=False))
|
35 |
+
j = base64.b64encode(json.dumps(j, indent=2, ensure_ascii=False).encode()).decode()
|
36 |
+
a2 = html.new_tag("a", download="{}.json".format(query), href="data:application/json;base64,{}".format(j), style="margin-left:3rem;")
|
37 |
+
a2.append("Save JSON")
|
38 |
+
h1.append(a2)
|
39 |
+
html.append(html.new_tag("hr", style="margin-top:1rem;margin-bottom:1rem;"))
|
40 |
+
return str(html)
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
|
45 |
def greet(query):
|
46 |
+
fileids = set()
|
47 |
fileid = query
|
48 |
hit = None
|
49 |
|
50 |
if query.upper() in autov22fileid:
|
51 |
fileid = str(autov22fileid[query.upper()])
|
52 |
+
fileids.add(fileid)
|
53 |
hit = True
|
54 |
if re.sub(r'\..*$', "", query) in filename2fileid:
|
55 |
fileid = str(filename2fileid[re.sub(r'\..*$', "", query)])
|
56 |
+
fileids.add(fileid)
|
57 |
hit = True
|
58 |
if query in name2fileid:
|
59 |
fileid = str(name2fileid[query])
|
60 |
+
fileids.add(fileid)
|
61 |
hit = True
|
62 |
if hit is not True:
|
63 |
for k, v in [(k.lower(), v) for k, v in name2fileid.items()]:
|
64 |
if re.search(re.compile(query), k):
|
65 |
fileid = str(v)
|
66 |
+
fileids.add(fileid)
|
67 |
+
|
68 |
+
return fileids2html(fileids, query)
|
69 |
|
70 |
+
iface = gr.Interface(fn=greet, inputs="text", outputs="html", allow_flagging='never', css='#component-4 { max-width: 16rem; }')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
iface.launch(server_name="0.0.0.0")
|