Spaces:
Runtime error
Runtime error
Added sentiment
Browse files- app.py +75 -22
- requirements.txt +3 -1
- test_api.py +39 -22
app.py
CHANGED
@@ -2,11 +2,13 @@ import inspect
|
|
2 |
import json
|
3 |
import logging
|
4 |
import os
|
|
|
|
|
5 |
import gradio as gr
|
6 |
-
from gradio import routes
|
7 |
import spacy # noqa
|
8 |
-
from typing import List, Type
|
9 |
from dotenv import load_dotenv
|
|
|
|
|
10 |
|
11 |
load_dotenv()
|
12 |
|
@@ -50,7 +52,6 @@ def replace_chars(text, char_mapping=CHAR_MAPPING):
|
|
50 |
|
51 |
def tokens2int(tokens, numwords={}):
|
52 |
""" Convert an English str containing number words into an int
|
53 |
-
|
54 |
>>> text2int("nine")
|
55 |
9
|
56 |
>>> text2int("forty two")
|
@@ -137,46 +138,98 @@ def get_types(cls_set: List[Type], component: str):
|
|
137 |
|
138 |
routes.get_types = get_types
|
139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
with gr.Blocks() as html_block:
|
141 |
gr.Markdown("# Gradio Blocks (3.0) with REST API")
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
146 |
button_text2int = gr.Button("text2int")
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
|
|
|
|
151 |
)
|
152 |
-
|
153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
gr.Markdown(r"""
|
155 |
|
156 |
## API
|
157 |
|
158 |
-
You can select which function to run using the `fn_index` argument:
|
159 |
-
|
160 |
```python
|
161 |
import requests
|
162 |
|
163 |
requests.post(
|
164 |
-
url="https://
|
165 |
).json()
|
166 |
```
|
167 |
|
168 |
Or using `curl`:
|
169 |
|
170 |
```bash
|
171 |
-
curl -X POST https://
|
172 |
```
|
173 |
""" + f"{json.loads(BQ_JSON)['type']}")
|
174 |
|
175 |
-
interface = gr.Interface(lambda:
|
|
|
|
|
|
|
176 |
|
177 |
-
html_block.input_components = interface.input_components
|
178 |
-
html_block.output_components = interface.output_components
|
179 |
-
html_block.examples = None
|
180 |
html_block.predict_durations = []
|
181 |
|
182 |
-
|
|
|
2 |
import json
|
3 |
import logging
|
4 |
import os
|
5 |
+
from typing import List, Type
|
6 |
+
|
7 |
import gradio as gr
|
|
|
8 |
import spacy # noqa
|
|
|
9 |
from dotenv import load_dotenv
|
10 |
+
from gradio import routes
|
11 |
+
from transformers import pipeline
|
12 |
|
13 |
load_dotenv()
|
14 |
|
|
|
52 |
|
53 |
def tokens2int(tokens, numwords={}):
|
54 |
""" Convert an English str containing number words into an int
|
|
|
55 |
>>> text2int("nine")
|
56 |
9
|
57 |
>>> text2int("forty two")
|
|
|
138 |
|
139 |
routes.get_types = get_types
|
140 |
|
141 |
+
functions = {
|
142 |
+
"text2int": text2int,
|
143 |
+
"text2int_preprocessed": try_text2int_preprocessed,
|
144 |
+
}
|
145 |
+
|
146 |
+
|
147 |
+
def text2int_selector(text, func):
|
148 |
+
f = functions[func]
|
149 |
+
return f(text)
|
150 |
+
|
151 |
+
|
152 |
+
sentiment = pipeline(task="sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
153 |
+
|
154 |
+
|
155 |
+
def get_sentiment(text):
|
156 |
+
return sentiment(text)
|
157 |
+
|
158 |
+
|
159 |
with gr.Blocks() as html_block:
|
160 |
gr.Markdown("# Gradio Blocks (3.0) with REST API")
|
161 |
+
|
162 |
+
inputs_text2int = [
|
163 |
+
gr.Text(placeholder="Type a number as text or a sentence", label="Text to process",
|
164 |
+
value="forty two"),
|
165 |
+
gr.Radio(["text2int", "text2int_preprocessed"], label="Function Selection", value="text2int")
|
166 |
+
]
|
167 |
+
|
168 |
+
outputs_text2int = gr.Textbox(label="Output integer")
|
169 |
+
|
170 |
button_text2int = gr.Button("text2int")
|
171 |
+
|
172 |
+
button_text2int.click(
|
173 |
+
fn=text2int_selector,
|
174 |
+
inputs=inputs_text2int,
|
175 |
+
outputs=outputs_text2int,
|
176 |
+
api_name="text2int",
|
177 |
)
|
178 |
+
|
179 |
+
examples_text2int = [
|
180 |
+
["one thousand forty seven", "text2int"],
|
181 |
+
["one hundred", "text2int_preprocessed"],
|
182 |
+
]
|
183 |
+
|
184 |
+
gr.Examples(examples=examples_text2int, inputs=inputs_text2int)
|
185 |
+
|
186 |
+
inputs_sentiment = [
|
187 |
+
gr.Text(placeholder="Type a number as text or a sentence", label="Text to process",
|
188 |
+
value="I really like it!"),
|
189 |
+
]
|
190 |
+
|
191 |
+
outputs_sentiment = gr.Textbox(label="Sentiment result")
|
192 |
+
|
193 |
+
button_sentiment = gr.Button("sentiment analysis")
|
194 |
+
|
195 |
+
button_sentiment.click(
|
196 |
+
get_sentiment,
|
197 |
+
inputs=inputs_sentiment,
|
198 |
+
outputs=outputs_sentiment,
|
199 |
+
api_name="sentiment-analysis"
|
200 |
+
)
|
201 |
+
|
202 |
+
examples_sentiment = [
|
203 |
+
["Couldn't agree more!"],
|
204 |
+
["Sorry, I can not accept this!"],
|
205 |
+
]
|
206 |
+
|
207 |
+
gr.Examples(examples=examples_sentiment, inputs=inputs_sentiment)
|
208 |
+
|
209 |
gr.Markdown(r"""
|
210 |
|
211 |
## API
|
212 |
|
|
|
|
|
213 |
```python
|
214 |
import requests
|
215 |
|
216 |
requests.post(
|
217 |
+
url="https://tangibleai-mathtext.hf.space/run/text2int", json={"data": ["one hundred forty five", "text2int"]}
|
218 |
).json()
|
219 |
```
|
220 |
|
221 |
Or using `curl`:
|
222 |
|
223 |
```bash
|
224 |
+
curl -X POST https://tangibleai-mathtext.hf.space/run/text2int -H 'Content-Type: application/json' -d '{"data": ["one hundred forty five", "text2int"]}'
|
225 |
```
|
226 |
""" + f"{json.loads(BQ_JSON)['type']}")
|
227 |
|
228 |
+
# interface = gr.Interface(lambda x: x, inputs=["text"], outputs=["text"])
|
229 |
+
# html_block.input_components = interface.input_components
|
230 |
+
# html_block.output_components = interface.output_components
|
231 |
+
# html_block.examples = None
|
232 |
|
|
|
|
|
|
|
233 |
html_block.predict_durations = []
|
234 |
|
235 |
+
html_block.launch()
|
requirements.txt
CHANGED
@@ -1,5 +1,7 @@
|
|
1 |
spacy
|
2 |
pandas
|
3 |
pandas-gbq
|
4 |
-
gradio
|
5 |
python-dotenv
|
|
|
|
|
|
1 |
spacy
|
2 |
pandas
|
3 |
pandas-gbq
|
4 |
+
gradio==3.14.0
|
5 |
python-dotenv
|
6 |
+
transformers
|
7 |
+
torch
|
test_api.py
CHANGED
@@ -1,46 +1,63 @@
|
|
1 |
"""https://zetcode.com/python/concurrent-http-requests/"""
|
2 |
|
3 |
-
|
4 |
import asyncio
|
5 |
import random
|
6 |
import time
|
7 |
|
8 |
import httpx
|
9 |
|
10 |
-
local_url = "http://127.0.0.1:5000"
|
11 |
-
remote_url = "https://cetinca-mathtext-nlu.hf.space/run/text2int_preprocessed"
|
12 |
-
remote_url = "https://tangibleai-mathtext.hf.space/run/"
|
13 |
headers = {"Content-Type": "application/json; charset=utf-8"}
|
14 |
-
|
15 |
-
|
16 |
-
["
|
17 |
-
["
|
18 |
-
["one
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
]
|
20 |
|
21 |
|
22 |
# async call to endpoint
|
23 |
-
async def call_api(url,
|
24 |
-
|
25 |
-
json = {"data": data, "fn_index": 1}
|
26 |
async with httpx.AsyncClient() as client:
|
27 |
-
|
28 |
-
begin2 = time.perf_counter() # Used perf_counter for more precise result.
|
29 |
-
# print(f"Call {number} started on: {start} text: {data}")
|
30 |
response = await client.post(url=url, headers=headers, json=json, timeout=30)
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
f"input_text: {data}
|
|
|
36 |
|
37 |
|
38 |
async def main(n):
|
39 |
calls = []
|
40 |
for num in range(n):
|
41 |
-
|
|
|
|
|
|
|
42 |
r = await asyncio.gather(*calls)
|
43 |
print(*r, sep="\n")
|
44 |
|
45 |
|
46 |
-
asyncio.run(main(
|
|
|
1 |
"""https://zetcode.com/python/concurrent-http-requests/"""
|
2 |
|
|
|
3 |
import asyncio
|
4 |
import random
|
5 |
import time
|
6 |
|
7 |
import httpx
|
8 |
|
|
|
|
|
|
|
9 |
headers = {"Content-Type": "application/json; charset=utf-8"}
|
10 |
+
|
11 |
+
data_list_local = [
|
12 |
+
{"url": "http://127.0.0.1:7860/run/text2int", "data": ["one hundred forty five", "text2int"]},
|
13 |
+
{"url": "http://127.0.0.1:7860/run/text2int", "data": ["twenty thousand nine hundred fifty", "text2int_preprocessed"]},
|
14 |
+
{"url": "http://127.0.0.1:7860/run/text2int", "data": ["one hundred forty five", "text2int"]},
|
15 |
+
{"url": "http://127.0.0.1:7860/run/text2int", "data": ["nine hundred eighty three", "text2int_preprocessed"]},
|
16 |
+
{"url": "http://127.0.0.1:7860/run/text2int", "data": ["five million"]},
|
17 |
+
{"url": "http://127.0.0.1:7860/run/sentiment-analysis", "data": ["Totally agree"]},
|
18 |
+
{"url": "http://127.0.0.1:7860/run/sentiment-analysis", "data": ["I like it"]},
|
19 |
+
{"url": "http://127.0.0.1:7860/run/sentiment-analysis", "data": ["No more"]},
|
20 |
+
{"url": "http://127.0.0.1:7860/run/sentiment-analysis", "data": ["I am not sure"]},
|
21 |
+
{"url": "http://127.0.0.1:7860/run/sentiment-analysis", "data": ["Never"]},
|
22 |
+
]
|
23 |
+
|
24 |
+
data_remote = [
|
25 |
+
{"url": "https://tangibleai-mathtext.hf.space/run/text2int", "data": ["one hundred forty five", "text2int"]},
|
26 |
+
{"url": "https://tangibleai-mathtext.hf.space/run/text2int", "data": ["twenty thousand nine hundred fifty", "text2int_preprocessed"]},
|
27 |
+
{"url": "https://tangibleai-mathtext.hf.space/run/text2int", "data": ["one hundred forty five", "text2int"]},
|
28 |
+
{"url": "https://tangibleai-mathtext.hf.space/run/text2int", "data": ["nine hundred eighty three", "text2int_preprocessed"]},
|
29 |
+
{"url": "https://tangibleai-mathtext.hf.space/run/text2int", "data": ["five million"]},
|
30 |
+
{"url": "https://tangibleai-mathtext.hf.space/run/sentiment-analysis", "data": ["Totally agree"]},
|
31 |
+
{"url": "https://tangibleai-mathtext.hf.space/run/sentiment-analysis", "data": ["I like it"]},
|
32 |
+
{"url": "https://tangibleai-mathtext.hf.space/run/sentiment-analysis", "data": ["No more"]},
|
33 |
+
{"url": "https://tangibleai-mathtext.hf.space/run/sentiment-analysis", "data": ["I am not sure"]},
|
34 |
+
{"url": "https://tangibleai-mathtext.hf.space/run/sentiment-analysis", "data": ["Never"]},
|
35 |
]
|
36 |
|
37 |
|
38 |
# async call to endpoint
|
39 |
+
async def call_api(url, data, number):
|
40 |
+
json = {"data": data}
|
|
|
41 |
async with httpx.AsyncClient() as client:
|
42 |
+
start = time.perf_counter() # Used perf_counter for more precise result.
|
|
|
|
|
43 |
response = await client.post(url=url, headers=headers, json=json, timeout=30)
|
44 |
+
end = time.perf_counter()
|
45 |
+
# print(response.status_code)
|
46 |
+
return f"Call_{number}\n" \
|
47 |
+
f"start: {start:.4f} end: {end:.4f} delay: {(end - start):.4f}\n" \
|
48 |
+
f"input_text: {data}\n" \
|
49 |
+
f"result: {response.json().get('data')}"
|
50 |
|
51 |
|
52 |
async def main(n):
|
53 |
calls = []
|
54 |
for num in range(n):
|
55 |
+
item = random.choice(data_remote)
|
56 |
+
url, data = item["url"], item["data"]
|
57 |
+
# calls.append(call_api(remote_url, data_list, num))
|
58 |
+
calls.append(call_api(url, data, num))
|
59 |
r = await asyncio.gather(*calls)
|
60 |
print(*r, sep="\n")
|
61 |
|
62 |
|
63 |
+
asyncio.run(main(30))
|