Threatthriver commited on
Commit
01990d8
·
verified ·
1 Parent(s): 0ab4ce4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -7
app.py CHANGED
@@ -4,7 +4,11 @@ import time
4
  from cerebras.cloud.sdk import Cerebras
5
 
6
  # Set up the Cerebras client
7
- client = Cerebras(api_key=os.getenv("CEREBRAS_API_KEY"))
 
 
 
 
8
 
9
  def chat_with_cerebras(user_input):
10
  """
@@ -32,10 +36,11 @@ def chat_with_cerebras(user_input):
32
  response = ""
33
  chain_of_thought = ""
34
  for chunk in stream:
35
- if chunk.choices[0].delta.content:
36
- response += chunk.choices[0].delta.content
37
- if "Chain of Thought:" in chunk.choices[0].delta.content:
38
- chain_of_thought += chunk.choices[0].delta.content.split("Chain of Thought:", 1)[-1]
 
39
 
40
  # End compute time measurement
41
  compute_time = time.time() - start_time
@@ -58,10 +63,14 @@ def gradio_ui():
58
  chain_of_thought_display = gr.Textbox(label="Chain of Thought", interactive=False, lines=10)
59
 
60
  user_input = gr.Textbox(label="Type your message", placeholder="Ask me anything...", lines=2)
61
- send_button = gr.Button("Send", variant="primary")
62
- clear_button = gr.Button("Clear Chat")
 
 
63
 
64
  def handle_chat(chat_history, user_input):
 
 
65
  ai_response, chain_of_thought, compute_info = chat_with_cerebras(user_input)
66
  chat_history.append((user_input, ai_response))
67
  return chat_history, chain_of_thought, compute_info
@@ -72,6 +81,8 @@ def gradio_ui():
72
  send_button.click(handle_chat, inputs=[chat_history, user_input], outputs=[chat_history, chain_of_thought_display, compute_time])
73
  clear_button.click(clear_chat, outputs=[chat_history, chain_of_thought_display, compute_time])
74
 
 
 
75
  gr.Markdown("""---\n### 🌟 Features:\n- **Advanced Reasoning**: Chain-of-thought explanations for complex queries.\n- **Real-Time Performance Metrics**: Measure response compute time instantly.\n- **Insightful Chain of Thought**: See the reasoning process behind AI decisions.\n- **User-Friendly Design**: Intuitive chatbot interface with powerful features.\n- **Powered by IntellijMind**: Setting new standards for AI interaction.\n""")
76
 
77
  return demo
 
4
  from cerebras.cloud.sdk import Cerebras
5
 
6
  # Set up the Cerebras client
7
+ api_key = os.getenv("CEREBRAS_API_KEY")
8
+ if not api_key:
9
+ raise ValueError("CEREBRAS_API_KEY environment variable is not set.")
10
+
11
+ client = Cerebras(api_key=api_key)
12
 
13
  def chat_with_cerebras(user_input):
14
  """
 
36
  response = ""
37
  chain_of_thought = ""
38
  for chunk in stream:
39
+ if chunk.choices[0].delta and chunk.choices[0].delta.content:
40
+ content = chunk.choices[0].delta.content
41
+ response += content
42
+ if "Chain of Thought:" in content:
43
+ chain_of_thought += content.split("Chain of Thought:", 1)[-1]
44
 
45
  # End compute time measurement
46
  compute_time = time.time() - start_time
 
63
  chain_of_thought_display = gr.Textbox(label="Chain of Thought", interactive=False, lines=10)
64
 
65
  user_input = gr.Textbox(label="Type your message", placeholder="Ask me anything...", lines=2)
66
+
67
+ with gr.Row():
68
+ send_button = gr.Button("Send", variant="primary")
69
+ clear_button = gr.Button("Clear Chat")
70
 
71
  def handle_chat(chat_history, user_input):
72
+ if not user_input.strip():
73
+ return chat_history, "", "Please enter a valid message."
74
  ai_response, chain_of_thought, compute_info = chat_with_cerebras(user_input)
75
  chat_history.append((user_input, ai_response))
76
  return chat_history, chain_of_thought, compute_info
 
81
  send_button.click(handle_chat, inputs=[chat_history, user_input], outputs=[chat_history, chain_of_thought_display, compute_time])
82
  clear_button.click(clear_chat, outputs=[chat_history, chain_of_thought_display, compute_time])
83
 
84
+ user_input.submit(handle_chat, inputs=[chat_history, user_input], outputs=[chat_history, chain_of_thought_display, compute_time])
85
+
86
  gr.Markdown("""---\n### 🌟 Features:\n- **Advanced Reasoning**: Chain-of-thought explanations for complex queries.\n- **Real-Time Performance Metrics**: Measure response compute time instantly.\n- **Insightful Chain of Thought**: See the reasoning process behind AI decisions.\n- **User-Friendly Design**: Intuitive chatbot interface with powerful features.\n- **Powered by IntellijMind**: Setting new standards for AI interaction.\n""")
87
 
88
  return demo