ysharma HF staff commited on
Commit
09e4eaf
1 Parent(s): 72f2f88

added streaming

Browse files
Files changed (1) hide show
  1. app.py +27 -4
app.py CHANGED
@@ -31,11 +31,34 @@ def predict(message, chatbot):
31
  "parameters": {"max_new_tokens":256}
32
  }
33
 
34
- response = requests.post(api_url, headers=headers, data=json.dumps(data), auth=('hf', hf_token))
35
 
36
- print(f'Logging: API response is - {response.text}')
37
- response_json_object = json.loads(response.text)
38
- return response_json_object[0]['generated_text']
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  gr.ChatInterface(predict, title=title, description=description, css=css).queue(concurrency_count=40).launch()
 
31
  "parameters": {"max_new_tokens":256}
32
  }
33
 
34
+ #response = requests.post(api_url, headers=headers, data=json.dumps(data), auth=('hf', hf_token))
35
 
36
+ #print(f'Logging: API response is - {response.text}')
37
+ #response_json_object = json.loads(response.text)
38
+ #return response_json_object[0]['generated_text']
39
 
40
+ response = requests.post(url, headers=headers, data=json.dumps(data), auth=('hf', hf_token), stream=True)
41
+
42
+ partial_message = ""
43
+ for line in response.iter_lines():
44
+ if line: # filter out keep-alive new lines
45
+ # Decode from bytes to string
46
+ decoded_line = line.decode('utf-8')
47
+
48
+ # Remove 'data:' prefix
49
+ if decoded_line.startswith('data:'):
50
+ json_line = decoded_line[5:] # Exclude the first 5 characters ('data:')
51
+ else:
52
+ print("This line does not start with 'data:':", decoded_line)
53
+ continue
54
+
55
+ # Load as JSON
56
+ try:
57
+ #print(json.loads(json_line)['token']['text'])
58
+ partial_message = partial_message + json.loads(json_line)['token']['text']
59
+ yield partial_message
60
+ except json.JSONDecodeError:
61
+ gr.Warning("This line is not valid JSON: ", json_line)
62
+ continue
63
 
64
  gr.ChatInterface(predict, title=title, description=description, css=css).queue(concurrency_count=40).launch()