A-baoYang commited on
Commit
c0d38ef
·
1 Parent(s): f79ddf1

Add requests stream call & yield function

Browse files
Files changed (2) hide show
  1. api_calls.py +15 -4
  2. app.py +14 -3
api_calls.py CHANGED
@@ -3,10 +3,21 @@ import requests
3
  API_ENDPOINT = "http://35.229.175.237:8889"
4
 
5
  # function to call api
 
 
 
 
 
 
 
 
 
6
  def call_api(api_path, api_params):
 
7
  url = f"{API_ENDPOINT}/{api_path}"
8
- response = requests.post(
9
- url, json=api_params, headers={"Content-Type": "application/json"})
 
10
  return response.json()
11
 
12
  def api_qa_normal(query, filtered_data, prompt_template):
@@ -16,7 +27,7 @@ def api_qa_normal(query, filtered_data, prompt_template):
16
  "filtered_data": filtered_data,
17
  "prompt_template": prompt_template
18
  }
19
- return call_api(api_path, api_params)
20
 
21
  def api_qa_waterfee(query, filtered_data, prompt_template):
22
  api_path = "qa/waterfee"
@@ -25,7 +36,7 @@ def api_qa_waterfee(query, filtered_data, prompt_template):
25
  "filtered_data": filtered_data,
26
  "prompt_template": prompt_template
27
  }
28
- return call_api(api_path, api_params)
29
 
30
  def api_ocr(image_filepath, model_provider):
31
  api_path = "ocr"
 
3
  API_ENDPOINT = "http://35.229.175.237:8889"
4
 
5
  # function to call api
6
+ def call_api_stream(api_path, api_params):
7
+ session = requests.Session()
8
+ url = f"{API_ENDPOINT}/{api_path}"
9
+ response = session.post(
10
+ url, json=api_params, headers={"Content-Type": "application/json"},
11
+ stream=True
12
+ )
13
+ return response
14
+
15
  def call_api(api_path, api_params):
16
+ session = requests.Session()
17
  url = f"{API_ENDPOINT}/{api_path}"
18
+ response = session.post(
19
+ url, json=api_params, headers={"Content-Type": "application/json"}
20
+ )
21
  return response.json()
22
 
23
  def api_qa_normal(query, filtered_data, prompt_template):
 
27
  "filtered_data": filtered_data,
28
  "prompt_template": prompt_template
29
  }
30
+ return call_api_stream(api_path, api_params)
31
 
32
  def api_qa_waterfee(query, filtered_data, prompt_template):
33
  api_path = "qa/waterfee"
 
36
  "filtered_data": filtered_data,
37
  "prompt_template": prompt_template
38
  }
39
+ return call_api_stream(api_path, api_params)
40
 
41
  def api_ocr(image_filepath, model_provider):
42
  api_path = "ocr"
app.py CHANGED
@@ -55,11 +55,22 @@ def bot(query, history, data_array, file_paths, qa_prompt_tmpl, checkbox_replace
55
  func = api_qa_normal
56
 
57
  response = func(**params)
 
58
 
59
  full_anwser = ""
60
- for character in response:
61
- full_anwser += character
62
- yield full_anwser
 
 
 
 
 
 
 
 
 
 
63
 
64
  def draw_cat_pain_assessment_result(user_input_image):
65
  if user_input_image:
 
55
  func = api_qa_normal
56
 
57
  response = func(**params)
58
+
59
 
60
  full_anwser = ""
61
+ for chunk in response.iter_content(chunk_size=32):
62
+ if chunk:
63
+ try:
64
+ _c = chunk.decode('utf-8')
65
+ except UnicodeDecodeError:
66
+ _c = " "
67
+ full_anwser += _c
68
+ yield full_anwser
69
+ # print(_c, flush=True, end="")
70
+
71
+ # for character in response:
72
+ # full_anwser += character
73
+ # yield full_anwser
74
 
75
  def draw_cat_pain_assessment_result(user_input_image):
76
  if user_input_image: