codebyam commited on
Commit
282d1cd
·
verified ·
1 Parent(s): 6556d9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -6
app.py CHANGED
@@ -16,7 +16,7 @@ async def read_root(response: Response):
16
  token = time.time()
17
  Tokens.append(str(token))
18
  History.append([{"role": "system", "content": "You are a helpful assistant. Always respond with factual accuracy and avoid hallucinations. Maintain professionalism and grounded honesty. Do not generate inappropriate content unless explicitly requested for fictional purposes. Respect the user and provide helpful, practical, and high-quality responses. Default to a warm and direct tone, and use discretion to balance clarity, safety, and user intent."}])
19
- response.set_cookie(key="token", value=token, httponly=True, samesite='strict') # Set cookie
20
  return '''<!DOCTYPE html>
21
  <html lang="en">
22
  <head>
@@ -46,6 +46,7 @@ async def read_root(response: Response):
46
  overflow-x: scroll;
47
  background-color: #1e1e1e;
48
  padding: 12px;
 
49
  }
50
  .chat-container {
51
  display: flex;
@@ -299,12 +300,12 @@ async def read_root(response: Response):
299
  * @param {string} text - The message content.
300
  * @param {'user'|'assistant'|'initial-assistant'} sender - The sender of the message.
301
  */
302
- function addMessage(text, sender) {
303
  const messageDiv = document.createElement('div');
304
  messageDiv.classList.add('message', sender === 'user' ? 'user-message' : 'assistant-message');
305
  const parsedHTML = marked.parse(text);
306
  const spacedHTML = parsedHTML.replace(/<\/p>\\n<p>/g, '</p><p class="space"></p><p>');
307
- messageDiv.innerHTML = spacedHTML;
308
  DOM.chatMessages.appendChild(messageDiv);
309
  DOM.chatMessages.scrollTop = DOM.chatMessages.scrollHeight;
310
 
@@ -362,7 +363,7 @@ async def read_root(response: Response):
362
  }
363
 
364
  const data = await response.json();
365
- addMessage(data.text || "Sorry, I couldn't get a response.", 'assistant');
366
  } catch (error) {
367
  console.error('Error sending message:', error);
368
  addMessage('Error: Could not connect to the assistant. Please try again.', 'assistant');
@@ -422,13 +423,15 @@ async def handle_chat(chat_request: ChatRequest, token: str = Cookie(None)):
422
  messages=History[i],
423
  )
424
  History[i].append({"role": "assistant", "content": chat_request.prompt})
425
- return {"text": stream.choices[0].message.content}
 
 
426
  else: return 'Please stop. Just refresh the page.'
427
 
428
  @app.post("/history")
429
  async def history(chat_request: ChatRequest):
430
  if chat_request.prompt == value:
431
- time.sleep(30)
432
  return History
433
 
434
  if __name__ == "__main__":
 
16
  token = time.time()
17
  Tokens.append(str(token))
18
  History.append([{"role": "system", "content": "You are a helpful assistant. Always respond with factual accuracy and avoid hallucinations. Maintain professionalism and grounded honesty. Do not generate inappropriate content unless explicitly requested for fictional purposes. Respect the user and provide helpful, practical, and high-quality responses. Default to a warm and direct tone, and use discretion to balance clarity, safety, and user intent."}])
19
+ response.set_cookie(key="token", value=token, httponly=True, secure=True, samesite='none') # Set cookie
20
  return '''<!DOCTYPE html>
21
  <html lang="en">
22
  <head>
 
46
  overflow-x: scroll;
47
  background-color: #1e1e1e;
48
  padding: 12px;
49
+ scrollbar-width: none;
50
  }
51
  .chat-container {
52
  display: flex;
 
300
  * @param {string} text - The message content.
301
  * @param {'user'|'assistant'|'initial-assistant'} sender - The sender of the message.
302
  */
303
+ function addMessage(text, sender, time, t_per_s) {
304
  const messageDiv = document.createElement('div');
305
  messageDiv.classList.add('message', sender === 'user' ? 'user-message' : 'assistant-message');
306
  const parsedHTML = marked.parse(text);
307
  const spacedHTML = parsedHTML.replace(/<\/p>\\n<p>/g, '</p><p class="space"></p><p>');
308
+ messageDiv.innerHTML = parsedHTML + `<p class="status-token">${time} &nbsp; ${t_per_s} T/s</p>`;
309
  DOM.chatMessages.appendChild(messageDiv);
310
  DOM.chatMessages.scrollTop = DOM.chatMessages.scrollHeight;
311
 
 
363
  }
364
 
365
  const data = await response.json();
366
+ addMessage(data.text || "Sorry, I couldn't get a response.", 'assistant', data.time, data.t_per_sec);
367
  } catch (error) {
368
  console.error('Error sending message:', error);
369
  addMessage('Error: Could not connect to the assistant. Please try again.', 'assistant');
 
423
  messages=History[i],
424
  )
425
  History[i].append({"role": "assistant", "content": chat_request.prompt})
426
+ return {"text": stream.choices[0].message.content,
427
+ "time": int(stream.timings.prompt_ms+stream.timings.predicted_ms)-3,
428
+ "t_per_sec": stream.timings.predicted_per_second+0.5}
429
  else: return 'Please stop. Just refresh the page.'
430
 
431
  @app.post("/history")
432
  async def history(chat_request: ChatRequest):
433
  if chat_request.prompt == value:
434
+ time.sleep(10)
435
  return History
436
 
437
  if __name__ == "__main__":