saneowl commited on
Commit
616220d
·
verified ·
1 Parent(s): 63c9177

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -17
app.py CHANGED
@@ -8,13 +8,10 @@ API_TOKEN = os.getenv("API_TOKEN")
8
 
9
  def get_ai_response(message, history):
10
  """Fetch AI response from the API using the modern messages format."""
11
- messages = [
12
- {"role": "system", "content": "You are a helpful assistant."}
13
- ]
14
  # Build the API history using all prior complete pairs
15
  for user_msg, ai_msg in history:
16
- # Only include completed exchanges (skip "pending")
17
- if ai_msg != "pending":
18
  clean_ai_msg = re.sub(r'<details>.*?</details>', '', ai_msg, flags=re.DOTALL)
19
  clean_ai_msg = re.sub(r'<[^>]*>', '', clean_ai_msg)
20
  messages.append({"role": "user", "content": user_msg})
@@ -54,22 +51,32 @@ def convert_reasoning_to_collapsible(text):
54
  return html_response
55
 
56
  def add_user_message(message, history):
57
- """Immediately add the user's message with a 'pending' assistant reply."""
58
  if history is None:
59
  history = []
60
- history.append((message, "pending"))
61
- return history
 
62
 
63
  def generate_response_from_history(history):
64
  """Generate the assistant's reply and update the last pending message."""
65
  if not history:
66
- return history
67
- # Get the last user message from history (which is paired with "pending")
68
  last_user_message = history[-1][0]
69
- # Get the full conversation (ignoring the pending reply)
70
- ai_response = get_ai_response(last_user_message, history)
 
 
 
 
 
 
 
 
 
71
  history[-1] = (last_user_message, ai_response)
72
- return history
73
 
74
  # Modern CSS for a clean UI
75
  custom_css = """
@@ -93,7 +100,8 @@ with gr.Blocks(css=custom_css, title="Reka Flash 3") as demo:
93
  submit_btn = gr.Button("Send", size="lg")
94
  clear_chat_btn = gr.Button("Clear Chat")
95
 
96
- chat_state = gr.State([]) # Current chat history
 
97
 
98
  js = """
99
  function() {
@@ -118,7 +126,7 @@ with gr.Blocks(css=custom_css, title="Reka Flash 3") as demo:
118
  }
119
  """
120
 
121
- # First, add the user message immediately (with pending), then update it with the generated response.
122
  submit_btn.click(
123
  add_user_message,
124
  [message, chat_state],
@@ -128,12 +136,12 @@ with gr.Blocks(css=custom_css, title="Reka Flash 3") as demo:
128
  chat_state,
129
  [chat_state, chatbot]
130
  ).then(
131
- lambda: "", # Clear the input box
132
  None,
133
  message
134
  )
135
 
136
- # Allow pressing Enter to submit
137
  message.submit(
138
  add_user_message,
139
  [message, chat_state],
@@ -154,6 +162,7 @@ with gr.Blocks(css=custom_css, title="Reka Flash 3") as demo:
154
  [chat_state, chatbot]
155
  )
156
 
 
157
  demo.load(
158
  fn=lambda: None,
159
  inputs=None,
 
8
 
9
  def get_ai_response(message, history):
10
  """Fetch AI response from the API using the modern messages format."""
11
+ messages = [{"role": "system", "content": "You are a helpful assistant."}]
 
 
12
  # Build the API history using all prior complete pairs
13
  for user_msg, ai_msg in history:
14
+ if ai_msg != "⏳ Thinking...":
 
15
  clean_ai_msg = re.sub(r'<details>.*?</details>', '', ai_msg, flags=re.DOTALL)
16
  clean_ai_msg = re.sub(r'<[^>]*>', '', clean_ai_msg)
17
  messages.append({"role": "user", "content": user_msg})
 
51
  return html_response
52
 
53
  def add_user_message(message, history):
54
+ """Immediately add the user's message with a '⏳ Thinking...' assistant reply."""
55
  if history is None:
56
  history = []
57
+ history.append((message, "⏳ Thinking..."))
58
+ # Return both updated state and chatbot messages
59
+ return history, history
60
 
61
  def generate_response_from_history(history):
62
  """Generate the assistant's reply and update the last pending message."""
63
  if not history:
64
+ return history, history
65
+ # Get the last user message (which is paired with "⏳ Thinking...")
66
  last_user_message = history[-1][0]
67
+ # Build API history excluding pending messages
68
+ api_history = []
69
+ for user_msg, ai_msg in history:
70
+ if ai_msg != "⏳ Thinking...":
71
+ clean_ai_msg = re.sub(r'<details>.*?</details>', '', ai_msg, flags=re.DOTALL)
72
+ clean_ai_msg = re.sub(r'<[^>]*>', '', clean_ai_msg)
73
+ api_history.append({"role": "user", "content": user_msg})
74
+ api_history.append({"role": "assistant", "content": clean_ai_msg})
75
+ # Append the last user message to fetch the assistant's reply
76
+ api_history.append({"role": "user", "content": last_user_message})
77
+ ai_response = get_ai_response(last_user_message, api_history)
78
  history[-1] = (last_user_message, ai_response)
79
+ return history, history
80
 
81
  # Modern CSS for a clean UI
82
  custom_css = """
 
100
  submit_btn = gr.Button("Send", size="lg")
101
  clear_chat_btn = gr.Button("Clear Chat")
102
 
103
+ # State management for chat history
104
+ chat_state = gr.State([])
105
 
106
  js = """
107
  function() {
 
126
  }
127
  """
128
 
129
+ # First, add the user message with a pending reply, then update it with the actual response.
130
  submit_btn.click(
131
  add_user_message,
132
  [message, chat_state],
 
136
  chat_state,
137
  [chat_state, chatbot]
138
  ).then(
139
+ lambda: "", # Clear the input box after processing
140
  None,
141
  message
142
  )
143
 
144
+ # Enable pressing Enter to submit
145
  message.submit(
146
  add_user_message,
147
  [message, chat_state],
 
162
  [chat_state, chatbot]
163
  )
164
 
165
+ # Load JavaScript to enable HTML rendering in chatbot messages
166
  demo.load(
167
  fn=lambda: None,
168
  inputs=None,