Tri4 commited on
Commit
c8a54e1
·
verified ·
1 Parent(s): 341df5e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +16 -4
main.py CHANGED
@@ -1,5 +1,7 @@
 
1
  from flask import Flask, request, jsonify, Response, stream_with_context
2
  from huggingface_hub import InferenceClient
 
3
 
4
  # Initialize Flask app
5
  app = Flask(__name__)
@@ -18,7 +20,7 @@ def format_prompt(message, history):
18
  return prompt
19
 
20
  def generate(prompt, history, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0):
21
- print(f"\nUser: {prompt}\n")
22
 
23
  temperature = float(temperature)
24
  if temperature < 1e-2:
@@ -47,12 +49,22 @@ def generate(prompt, history, temperature=0.9, max_new_tokens=256, top_p=0.95, r
47
  )
48
 
49
  output = ""
 
 
 
50
  for token in response:
51
- output += token.token.text
52
- yield token.token.text # Yield each token for streaming
 
 
 
 
 
 
 
53
 
54
  # Print AI response
55
- print(f"\nSema AI: {output}\n")
56
  except Exception as e:
57
  print(f"Exception during generation: {str(e)}")
58
  yield "Error occurred"
 
1
+
2
  from flask import Flask, request, jsonify, Response, stream_with_context
3
  from huggingface_hub import InferenceClient
4
+ import time
5
 
6
  # Initialize Flask app
7
  app = Flask(__name__)
 
20
  return prompt
21
 
22
  def generate(prompt, history, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0):
23
+ print(f"\nUser: {prompt}\n", flush=True)
24
 
25
  temperature = float(temperature)
26
  if temperature < 1e-2:
 
49
  )
50
 
51
  output = ""
52
+ buffer = []
53
+ buffer_size = 5 # Adjust the buffer size as needed
54
+
55
  for token in response:
56
+ buffer.append(token.token.text)
57
+ if len(buffer) >= buffer_size:
58
+ chunk = ''.join(buffer)
59
+ yield chunk
60
+ buffer.clear()
61
+ time.sleep(0.1) # Introduce a delay to manage the flow of data
62
+
63
+ if buffer:
64
+ yield ''.join(buffer)
65
 
66
  # Print AI response
67
+ print(f"\nSema AI: {output}\n, flush=True")
68
  except Exception as e:
69
  print(f"Exception during generation: {str(e)}")
70
  yield "Error occurred"