st192011 commited on
Commit
a6168f7
·
verified ·
1 Parent(s): a0e60b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -13
app.py CHANGED
@@ -32,20 +32,19 @@ def socratic_streaming_chat(user_query):
32
  "-p", formatted_chat_prompt,
33
  "-n", "120",
34
  "-temp", "0.4",
35
- "-t", "2" # Optimized for Hugging Face free-tier dual-core CPUs
36
  ]
37
 
38
- # Spawn the process with stdout piping enabled for live stream reading
39
  process = subprocess.Popen(
40
  cmd,
41
  stdout=subprocess.PIPE,
42
- stderr=subprocess.DEVNULL,
43
  text=True,
44
  bufsize=1
45
  )
46
 
47
  accumulator = ""
48
- # Lookahead buffer size in characters (~45 chars safely covers variance of stop loops)
49
  LOOKAHEAD_SIZE = 45
50
 
51
  stop_markers = [
@@ -55,19 +54,22 @@ def socratic_streaming_chat(user_query):
55
  "Assistant:"
56
  ]
57
 
58
- # Read the terminal execution stream live character-by-character
59
  while True:
60
  char = process.stdout.read(1)
61
  if not char:
62
- break # End of stream reached
63
 
64
  accumulator += char
65
 
66
- # Discard the echoed prompt wrapper if it slips into the stdout read window
67
- if accumulator.startswith(formatted_chat_prompt):
68
- accumulator = accumulator.replace(formatted_chat_prompt, "").strip()
 
 
 
 
69
 
70
- # Scan the live accumulator for structural collapse boundaries
71
  stop_triggered = False
72
  for marker in stop_markers:
73
  if marker in accumulator:
@@ -76,15 +78,14 @@ def socratic_streaming_chat(user_query):
76
  break
77
 
78
  if stop_triggered:
79
- process.terminate() # Hard-kill the engine to stop burning CPU cycles
80
  break
81
 
82
- # UX Lookahead Protection: Only stream text that sits safely outside the trailing boundary window
83
  if len(accumulator) > LOOKAHEAD_SIZE:
84
  safe_to_display = accumulator[:len(accumulator) - LOOKAHEAD_SIZE]
85
  yield safe_to_display.strip()
86
 
87
- # Yield the completely un-buffered, finalized clean string
88
  yield accumulator.strip()
89
 
90
  # ==============================================================================
 
32
  "-p", formatted_chat_prompt,
33
  "-n", "120",
34
  "-temp", "0.4",
35
+ "-t", "2"
36
  ]
37
 
 
38
  process = subprocess.Popen(
39
  cmd,
40
  stdout=subprocess.PIPE,
41
+ stderr=subprocess.DEVNULL, # Keeps system logs hidden
42
  text=True,
43
  bufsize=1
44
  )
45
 
46
  accumulator = ""
47
+ prompt_cleared = False
48
  LOOKAHEAD_SIZE = 45
49
 
50
  stop_markers = [
 
54
  "Assistant:"
55
  ]
56
 
 
57
  while True:
58
  char = process.stdout.read(1)
59
  if not char:
60
+ break
61
 
62
  accumulator += char
63
 
64
+ # --- THE FIX: SWALLOW THE ECHOED PROMPT ---
65
+ if not prompt_cleared:
66
+ if "Assistant:" in accumulator:
67
+ prompt_cleared = True
68
+ # Delete the prompt and keep only what comes after "Assistant:"
69
+ accumulator = accumulator.split("Assistant:")[-1].lstrip()
70
+ continue # Do not yield text until the prompt is fully swallowed
71
 
72
+ # Scan for structural collapse boundaries
73
  stop_triggered = False
74
  for marker in stop_markers:
75
  if marker in accumulator:
 
78
  break
79
 
80
  if stop_triggered:
81
+ process.terminate()
82
  break
83
 
84
+ # Stream text safely outside the trailing boundary window
85
  if len(accumulator) > LOOKAHEAD_SIZE:
86
  safe_to_display = accumulator[:len(accumulator) - LOOKAHEAD_SIZE]
87
  yield safe_to_display.strip()
88
 
 
89
  yield accumulator.strip()
90
 
91
  # ==============================================================================