cetinca commited on
Commit
05b4410
1 Parent(s): 90009ee

Update test_api

Browse files
Files changed (5) hide show
  1. .gitignore +1 -0
  2. app.py +122 -73
  3. plot_calls.py +9 -0
  4. requirements.txt +1 -0
  5. test_api.py +75 -43
.gitignore CHANGED
@@ -98,3 +98,4 @@ docs/**/*.html
98
  .bash_env
99
  **/*secret*
100
  **/*private*
 
 
98
  .bash_env
99
  **/*secret*
100
  **/*private*
101
+ /call_history.csv
app.py CHANGED
@@ -138,17 +138,6 @@ def get_types(cls_set: List[Type], component: str):
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
 
@@ -157,73 +146,133 @@ def get_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
 
138
 
139
  routes.get_types = get_types
140
 
 
 
 
 
 
 
 
 
 
 
 
141
  sentiment = pipeline(task="sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
142
 
143
 
 
146
 
147
 
148
  with gr.Blocks() as html_block:
149
+ gr.Markdown("# Rori - Mathbot")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
 
151
+ with gr.Tab("Text to integer"):
152
+ inputs_text2int = [
153
+ gr.Text(placeholder="Type a number as text or a sentence", label="Text to process",
154
+ value="forty two"),
155
+ ]
156
 
157
+ outputs_text2int = gr.Textbox(label="Output integer")
 
158
 
159
+ button_text2int = gr.Button("text2int")
 
 
 
160
 
161
+ button_text2int.click(
162
+ fn=try_text2int,
163
+ inputs=inputs_text2int,
164
+ outputs=outputs_text2int,
165
+ api_name="text2int",
166
+ )
167
 
168
+ examples_text2int = [
169
+ "one thousand forty seven",
170
+ "one hundred",
171
+ ]
172
+
173
+ gr.Examples(examples=examples_text2int, inputs=inputs_text2int)
174
+
175
+ gr.Markdown(r"""
176
+
177
+ ## API
178
+ ```python
179
+ import requests
180
+
181
+ requests.post(
182
+ url="https://tangibleai-mathtext.hf.space/run/text2int", json={"data": ["one hundred forty five"]}
183
+ ).json()
184
+ ```
185
+
186
+ Or using `curl`:
187
+
188
+ ```bash
189
+ curl -X POST https://tangibleai-mathtext.hf.space/run/text2int -H 'Content-Type: application/json' -d '{"data": ["one hundred forty five"]}'
190
+ ```
191
+ {bq_json}""" + f"{json.loads(BQ_JSON)['type']}")
192
+
193
+ with gr.Tab("Text to integer preprocessed"):
194
+ inputs_text2int_preprocessed = [
195
+ gr.Text(placeholder="Type a number as text or a sentence", label="Text to process",
196
+ value="forty two"),
197
+ ]
198
+
199
+ outputs_text2int_preprocessed = gr.Textbox(label="Output integer")
200
+
201
+ button_text2int = gr.Button("text2int preprocessed")
202
+
203
+ button_text2int.click(
204
+ fn=try_text2int_preprocessed,
205
+ inputs=inputs_text2int_preprocessed,
206
+ outputs=outputs_text2int_preprocessed,
207
+ api_name="text2int_preprocessed",
208
+ )
209
+
210
+ examples_text2int_preprocessed = [
211
+ "one thousand forty seven",
212
+ "one hundred",
213
+ ]
214
+
215
+ gr.Examples(examples=examples_text2int_preprocessed, inputs=inputs_text2int_preprocessed)
216
+
217
+ gr.Markdown(r"""
218
+
219
+ ## API
220
+ ```python
221
+ import requests
222
+
223
+ requests.post(
224
+ url="https://tangibleai-mathtext.hf.space/run/text2int_preprocessed", json={"data": ["one hundred forty five"]}
225
+ ).json()
226
+ ```
227
+
228
+ Or using `curl`:
229
+
230
+ ```bash
231
+ curl -X POST https://tangibleai-mathtext.hf.space/run/text2int_preprocessed -H 'Content-Type: application/json' -d '{"data": ["one hundred forty five"]}'
232
+ ```
233
+ {bq_json}""" + f"{json.loads(BQ_JSON)['type']}")
234
+
235
+ with gr.Tab("Sentiment Analysis"):
236
+ inputs_sentiment = [
237
+ gr.Text(placeholder="Type a number as text or a sentence", label="Text to process",
238
+ value="I really like it!"),
239
+ ]
240
+
241
+ outputs_sentiment = gr.Textbox(label="Sentiment result")
242
+
243
+ button_sentiment = gr.Button("sentiment analysis")
244
+
245
+ button_sentiment.click(
246
+ get_sentiment,
247
+ inputs=inputs_sentiment,
248
+ outputs=outputs_sentiment,
249
+ api_name="sentiment-analysis"
250
+ )
251
+
252
+ examples_sentiment = [
253
+ ["Totally agree!"],
254
+ ["Sorry, I can not accept this!"],
255
+ ]
256
+
257
+ gr.Examples(examples=examples_sentiment, inputs=inputs_sentiment)
258
+
259
+ gr.Markdown(r"""
260
+
261
+ ## API
262
+ ```python
263
+ import requests
264
+
265
+ requests.post(
266
+ url="https://tangibleai-mathtext.hf.space/run/sentiment-analysis", json={"data": ["You are right!"]}
267
+ ).json()
268
+ ```
269
+
270
+ Or using `curl`:
271
+
272
+ ```bash
273
+ curl -X POST https://tangibleai-mathtext.hf.space/run/sentiment-analysis -H 'Content-Type: application/json' -d '{"data": ["You are right!"]}'
274
+ ```
275
+ {bq_json}""" + f"{json.loads(BQ_JSON)['type']}")
276
 
277
  # interface = gr.Interface(lambda x: x, inputs=["text"], outputs=["text"])
278
  # html_block.input_components = interface.input_components
plot_calls.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import matplotlib.pyplot as plt
2
+ import pandas as pd
3
+
4
+ df = pd.read_csv('call_history.csv') # data loading
5
+ print(df)
6
+
7
+ df.plot(by='endpoint', column='delay', kind='box', showmeans=True)
8
+
9
+ plt.show()
requirements.txt CHANGED
@@ -5,3 +5,4 @@ gradio==3.14.0
5
  python-dotenv
6
  transformers
7
  torch
 
 
5
  python-dotenv
6
  transformers
7
  torch
8
+ httpx
test_api.py CHANGED
@@ -3,61 +3,93 @@
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))
 
 
 
 
3
  import asyncio
4
  import random
5
  import time
6
+ import pandas as pd
7
  import httpx
8
+ from os.path import exists
9
+
10
+ NUMBER_OF_CALLS = 10
11
 
12
  headers = {"Content-Type": "application/json; charset=utf-8"}
13
 
14
+ base_url = "https://tangibleai-mathtext.hf.space/run/{endpoint}"
15
+ # base_url = "http://localhost:7860/run/{endpoint}"
16
+
17
+ data_list_1 = {
18
+ "endpoint": "text2int",
19
+ "test_data": [
20
+ "one hundred forty five",
21
+ "twenty thousand nine hundred fifty",
22
+ "one hundred forty five",
23
+ "nine hundred eighty three",
24
+ "five million",
25
+ ]
26
+ }
27
+
28
+ data_list_2 = {
29
+ "endpoint": "text2int_preprocessed",
30
+ "test_data": [
31
+ "one hundred forty five",
32
+ "twenty thousand nine hundred fifty",
33
+ "one hundred forty five",
34
+ "nine hundred eighty three",
35
+ "five million",
36
+ ]
37
+ }
38
+ data_list_3 = {
39
+ "endpoint": "sentiment-analysis",
40
+ "test_data": [
41
+ "Totally agree",
42
+ "I like it",
43
+ "No more",
44
+ "I am not sure",
45
+ "Never",
46
+ ]
47
+ }
48
 
49
 
50
  # async call to endpoint
51
+ async def call_api(url, data, call_number, number_of_calls):
52
+ json = {"data": [data]}
53
  async with httpx.AsyncClient() as client:
54
  start = time.perf_counter() # Used perf_counter for more precise result.
55
  response = await client.post(url=url, headers=headers, json=json, timeout=30)
56
  end = time.perf_counter()
57
+ return {
58
+ "endpoint": url.split("/")[-1],
59
+ "test data": data,
60
+ "response": response.json().get("data"),
61
+ "call number": call_number,
62
+ "number of calls": number_of_calls,
63
+ "start": start.__round__(4),
64
+ "end": end.__round__(4),
65
+ "delay": (end - start).__round__(4)
66
+ }
67
+
68
+
69
+ data_lists = [data_list_1, data_list_2, data_list_3]
70
+
71
+ results = []
72
+
73
+
74
+ async def main(number_of_calls):
75
+ for data_list in data_lists:
76
+ calls = []
77
+ for call_number in range(1, number_of_calls + 1):
78
+ url = base_url.format(endpoint=data_list["endpoint"])
79
+ data = random.choice(data_list["test_data"])
80
+ calls.append(call_api(url, data, call_number, number_of_calls))
81
+ r = await asyncio.gather(*calls)
82
+ results.extend(r)
83
 
84
 
 
 
 
 
 
 
 
 
 
85
 
86
+ start = time.perf_counter()
87
+ asyncio.run(main(NUMBER_OF_CALLS))
88
+ end = time.perf_counter()
89
+ print(end-start)
90
+ df = pd.DataFrame(results)
91
 
92
+ if exists("call_history.csv"):
93
+ df.to_csv(path_or_buf="call_history.csv", mode="a", header=False, index=False)
94
+ else:
95
+ df.to_csv(path_or_buf="call_history.csv", mode="w", header=True, index=False)