Spaces:
Sleeping
Sleeping
Commit
·
33532b1
1
Parent(s):
9d6536b
ner
Browse files- Dockerfile +6 -2
- app.py +157 -5
- requirements.txt +7 -2
Dockerfile
CHANGED
@@ -1,13 +1,17 @@
|
|
1 |
FROM python:3.11
|
2 |
-
|
3 |
RUN useradd -m -u 1000 user
|
4 |
USER user
|
5 |
-
ENV
|
|
|
6 |
|
|
|
7 |
WORKDIR /app
|
8 |
|
9 |
COPY --chown=user ./requirements.txt requirements.txt
|
10 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
11 |
|
12 |
COPY --chown=user . /app
|
|
|
|
|
|
|
13 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
1 |
FROM python:3.11
|
|
|
2 |
RUN useradd -m -u 1000 user
|
3 |
USER user
|
4 |
+
ENV HOME=/home/user \
|
5 |
+
PATH=/home/user/.local/bin:$PATH
|
6 |
|
7 |
+
ENV GRADIO_ALLOW_FLAGGING="never"
|
8 |
WORKDIR /app
|
9 |
|
10 |
COPY --chown=user ./requirements.txt requirements.txt
|
11 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
12 |
|
13 |
COPY --chown=user . /app
|
14 |
+
|
15 |
+
RUN python -m spacy download en_core_web_sm
|
16 |
+
|
17 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
CHANGED
@@ -1,7 +1,159 @@
|
|
1 |
-
from
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from functools import lru_cache
|
2 |
+
import spacy
|
3 |
+
import gradio as gr
|
4 |
+
spacy.prefer_gpu()
|
5 |
|
6 |
+
import os
|
7 |
+
DEFAULT_MODEL_NAME = os.getenv("MODEL_NAME","en_core_web_sm")
|
8 |
+
|
9 |
+
# 2024-Aug
|
10 |
+
ALL_MODELS = """\
|
11 |
+
ca_core_news_sm
|
12 |
+
ca_core_news_md
|
13 |
+
ca_core_news_lg
|
14 |
+
ca_core_news_trf
|
15 |
+
zh_core_web_sm
|
16 |
+
zh_core_web_md
|
17 |
+
zh_core_web_lg
|
18 |
+
zh_core_web_trf
|
19 |
+
hr_core_news_sm
|
20 |
+
hr_core_news_md
|
21 |
+
hr_core_news_lg
|
22 |
+
da_core_news_sm
|
23 |
+
da_core_news_md
|
24 |
+
da_core_news_lg
|
25 |
+
da_core_news_trf
|
26 |
+
nl_core_news_sm
|
27 |
+
nl_core_news_md
|
28 |
+
nl_core_news_lg
|
29 |
+
en_core_web_sm
|
30 |
+
en_core_web_md
|
31 |
+
en_core_web_lg
|
32 |
+
en_core_web_trf
|
33 |
+
fi_core_news_sm
|
34 |
+
fi_core_news_md
|
35 |
+
fi_core_news_lg
|
36 |
+
fr_core_news_sm
|
37 |
+
fr_core_news_md
|
38 |
+
fr_core_news_lg
|
39 |
+
fr_dep_news_trf
|
40 |
+
de_core_news_sm
|
41 |
+
de_core_news_md
|
42 |
+
de_core_news_lg
|
43 |
+
de_dep_news_trf
|
44 |
+
el_core_news_sm
|
45 |
+
el_core_news_md
|
46 |
+
el_core_news_lg
|
47 |
+
it_core_news_sm
|
48 |
+
it_core_news_md
|
49 |
+
it_core_news_lg
|
50 |
+
ja_core_news_sm
|
51 |
+
ja_core_news_md
|
52 |
+
ja_core_news_lg
|
53 |
+
ja_core_news_trf
|
54 |
+
ko_core_news_sm
|
55 |
+
ko_core_news_md
|
56 |
+
ko_core_news_lg
|
57 |
+
lt_core_news_sm
|
58 |
+
lt_core_news_md
|
59 |
+
lt_core_news_lg
|
60 |
+
mk_core_news_sm
|
61 |
+
mk_core_news_md
|
62 |
+
mk_core_news_lg
|
63 |
+
xx_ent_wiki_sm
|
64 |
+
xx_sent_ud_sm
|
65 |
+
nb_core_news_sm
|
66 |
+
nb_core_news_md
|
67 |
+
nb_core_news_lg
|
68 |
+
pl_core_news_sm
|
69 |
+
pl_core_news_md
|
70 |
+
pl_core_news_lg
|
71 |
+
pt_core_news_sm
|
72 |
+
pt_core_news_md
|
73 |
+
pt_core_news_lg
|
74 |
+
ro_core_news_sm
|
75 |
+
ro_core_news_md
|
76 |
+
ro_core_news_lg
|
77 |
+
ru_core_news_sm
|
78 |
+
ru_core_news_md
|
79 |
+
ru_core_news_lg
|
80 |
+
sl_core_news_sm
|
81 |
+
sl_core_news_md
|
82 |
+
sl_core_news_lg
|
83 |
+
sl_core_news_trf
|
84 |
+
es_core_news_sm
|
85 |
+
es_core_news_md
|
86 |
+
es_core_news_lg
|
87 |
+
es_dep_news_trf
|
88 |
+
sv_core_news_sm
|
89 |
+
sv_core_news_md
|
90 |
+
sv_core_news_lg
|
91 |
+
uk_core_news_sm
|
92 |
+
uk_core_news_md
|
93 |
+
uk_core_news_lg
|
94 |
+
uk_core_news_trf"""
|
95 |
+
ALL_MODELS_LIST = [
|
96 |
+
line.strip()
|
97 |
+
for line in ALL_MODELS.split("\n")
|
98 |
+
if line.strip()
|
99 |
+
]
|
100 |
+
|
101 |
+
@lru_cache(maxsize=1)
|
102 |
+
def load_model(model_name=DEFAULT_MODEL_NAME):
|
103 |
+
try:
|
104 |
+
nlp = spacy.load(model_name)
|
105 |
+
except:
|
106 |
+
os.system(f"python -m spacy download {model_name}")
|
107 |
+
nlp = spacy.load(model_name)
|
108 |
+
return nlp
|
109 |
+
load_model()
|
110 |
+
|
111 |
+
def ner(sentence, model_name):
|
112 |
+
nlp = load_model(model_name)
|
113 |
+
result = nlp(sentence)
|
114 |
+
stand_char = 0
|
115 |
+
ret = []
|
116 |
+
entities = result.ents
|
117 |
+
for e in entities:
|
118 |
+
if e.start_char > stand_char:
|
119 |
+
ret.append((
|
120 |
+
sentence[stand_char:e.start_char],
|
121 |
+
None
|
122 |
+
))
|
123 |
+
stand_char = e.start_char
|
124 |
|
125 |
+
ret.append((
|
126 |
+
sentence[e.start_char: e.end_char],
|
127 |
+
e.label_,
|
128 |
+
))
|
129 |
+
stand_char = e.end_char
|
130 |
+
return ret
|
131 |
+
|
132 |
+
|
133 |
+
demo = gr.Interface(
|
134 |
+
fn=ner,
|
135 |
+
inputs=[
|
136 |
+
gr.Text(),
|
137 |
+
gr.Dropdown(ALL_MODELS_LIST, value=DEFAULT_MODEL_NAME)
|
138 |
+
],
|
139 |
+
outputs=[
|
140 |
+
gr.HighlightedText(
|
141 |
+
show_legend=True,
|
142 |
+
)
|
143 |
+
],
|
144 |
+
examples=[
|
145 |
+
[
|
146 |
+
"This is 24/08/2024.\nAngela Zhang is one of the most important artist in Coca-Cola arena Dubai.",
|
147 |
+
"en_core_web_sm"
|
148 |
+
]
|
149 |
+
],
|
150 |
+
# cache_examples=True,
|
151 |
+
)
|
152 |
+
|
153 |
+
from fastapi import FastAPI
|
154 |
+
from gradio2api.gr_fastapi import LocalGradioAppRouter
|
155 |
+
router = LocalGradioAppRouter(demo)
|
156 |
+
app = FastAPI()
|
157 |
+
app.include_router(router=router, prefix="/api")
|
158 |
+
app = gr.mount_gradio_app(app,demo,path="/gui",)
|
159 |
+
app = gr.mount_gradio_app(app,demo,path="/",)
|
requirements.txt
CHANGED
@@ -1,2 +1,7 @@
|
|
1 |
-
fastapi
|
2 |
-
uvicorn[standard]
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi[standard]
|
2 |
+
uvicorn[standard]
|
3 |
+
gradio
|
4 |
+
|
5 |
+
spacy
|
6 |
+
|
7 |
+
gradio2api
|