Lyte commited on
Commit
c50732d
Β·
verified Β·
1 Parent(s): 587fdab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -7
app.py CHANGED
@@ -2,8 +2,23 @@ import time
2
  import gradio as gr
3
  from openai import OpenAI
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  DESCRIPTION = '''
6
- # DeepSeek-R1 Distill Qwen-1.5 Demo
 
7
  A reasoning model trained using RL (Reinforcement Learning) that demonstrates structured reasoning capabilities.
8
  '''
9
 
@@ -46,13 +61,14 @@ def user(message, history):
46
  return "", history + [[message, None]]
47
 
48
  class ParserState:
49
- __slots__ = ['answer', 'thought', 'in_think', 'start_time', 'last_pos']
50
  def __init__(self):
51
  self.answer = ""
52
  self.thought = ""
53
  self.in_think = False
54
  self.start_time = 0
55
  self.last_pos = 0
 
56
 
57
  def parse_response(text, state):
58
  buffer = text[state.last_pos:]
@@ -73,6 +89,9 @@ def parse_response(text, state):
73
  think_end = buffer.find('</think>')
74
  if think_end != -1:
75
  state.thought += buffer[:think_end]
 
 
 
76
  state.in_think = False
77
  buffer = buffer[think_end + 8:]
78
  else:
@@ -87,8 +106,15 @@ def format_response(state, elapsed):
87
  collapsible = []
88
 
89
  if state.thought or state.in_think:
90
- status = (f"πŸŒ€ Thinking for {elapsed:.0f} seconds"
91
- if state.in_think else f"βœ… Thought for {elapsed:.0f} seconds")
 
 
 
 
 
 
 
92
  collapsible.append(
93
  f"<details open><summary>{status}</summary>\n\n<div class='thinking-container'>\n{state.thought}\n</div>\n</details>"
94
  )
@@ -120,13 +146,13 @@ def generate_response(history, temperature, top_p, max_tokens, active_gen):
120
  state, elapsed = parse_response(full_response, state)
121
 
122
  collapsible, answer_part = format_response(state, elapsed)
123
- history[-1][1] = "\n\n".join(collapsible + [answer_part]) # Markdown-safe
124
  yield history
125
 
126
- # Final update
127
  state, elapsed = parse_response(full_response, state)
128
  collapsible, answer_part = format_response(state, elapsed)
129
- history[-1][1] = "\n\n".join(collapsible + [answer_part]) # Markdown-safe
130
  yield history
131
 
132
  except Exception as e:
 
2
  import gradio as gr
3
  from openai import OpenAI
4
 
5
+ def format_time(seconds_float):
6
+ total_seconds = int(round(seconds_float))
7
+ hours = total_seconds // 3600
8
+ remaining_seconds = total_seconds % 3600
9
+ minutes = remaining_seconds // 60
10
+ seconds = remaining_seconds % 60
11
+
12
+ if hours > 0:
13
+ return f"{hours}h {minutes}m {seconds}s"
14
+ elif minutes > 0:
15
+ return f"{minutes}m {seconds}s"
16
+ else:
17
+ return f"{seconds}s"
18
+
19
  DESCRIPTION = '''
20
+ # Duplicate the space for free private inference.
21
+ ## DeepSeek-R1 Distill Qwen-1.5 Demo
22
  A reasoning model trained using RL (Reinforcement Learning) that demonstrates structured reasoning capabilities.
23
  '''
24
 
 
61
  return "", history + [[message, None]]
62
 
63
  class ParserState:
64
+ __slots__ = ['answer', 'thought', 'in_think', 'start_time', 'last_pos', 'total_think_time']
65
  def __init__(self):
66
  self.answer = ""
67
  self.thought = ""
68
  self.in_think = False
69
  self.start_time = 0
70
  self.last_pos = 0
71
+ self.total_think_time = 0.0
72
 
73
  def parse_response(text, state):
74
  buffer = text[state.last_pos:]
 
89
  think_end = buffer.find('</think>')
90
  if think_end != -1:
91
  state.thought += buffer[:think_end]
92
+ # Calculate duration and accumulate
93
+ duration = time.perf_counter() - state.start_time
94
+ state.total_think_time += duration
95
  state.in_think = False
96
  buffer = buffer[think_end + 8:]
97
  else:
 
106
  collapsible = []
107
 
108
  if state.thought or state.in_think:
109
+ if state.in_think:
110
+ # Ongoing think: total time = accumulated + current elapsed
111
+ total_elapsed = state.total_think_time + elapsed
112
+ formatted_time = format_time(total_elapsed)
113
+ status = f"πŸŒ€ Thinking for {formatted_time}"
114
+ else:
115
+ # Finished: show total accumulated time
116
+ formatted_time = format_time(state.total_think_time)
117
+ status = f"βœ… Thought for {formatted_time}"
118
  collapsible.append(
119
  f"<details open><summary>{status}</summary>\n\n<div class='thinking-container'>\n{state.thought}\n</div>\n</details>"
120
  )
 
146
  state, elapsed = parse_response(full_response, state)
147
 
148
  collapsible, answer_part = format_response(state, elapsed)
149
+ history[-1][1] = "\n\n".join(collapsible + [answer_part])
150
  yield history
151
 
152
+ # Final update to ensure all content is parsed
153
  state, elapsed = parse_response(full_response, state)
154
  collapsible, answer_part = format_response(state, elapsed)
155
+ history[-1][1] = "\n\n".join(collapsible + [answer_part])
156
  yield history
157
 
158
  except Exception as e: