Threatthriver commited on
Commit
0f0407d
Β·
verified Β·
1 Parent(s): a26b3e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -13
app.py CHANGED
@@ -9,7 +9,7 @@ client = Cerebras(api_key=os.getenv("CEREBRAS_API_KEY"))
9
  def chat_with_cerebras(user_input):
10
  """
11
  Handles interaction with the Cerebras model.
12
- Sends user input and returns the model's response along with compute time.
13
  """
14
  # Start compute time measurement
15
  start_time = time.time()
@@ -18,7 +18,7 @@ def chat_with_cerebras(user_input):
18
  # Create a chat stream with Cerebras
19
  stream = client.chat.completions.create(
20
  messages=[
21
- {"role": "system", "content": "You are IntellijMind, an advanced AI designed to assist users with detailed insights and problem-solving."},
22
  {"role": "user", "content": user_input}
23
  ],
24
  model="llama-3.3-70b",
@@ -30,44 +30,49 @@ def chat_with_cerebras(user_input):
30
 
31
  # Collect response from the stream
32
  response = ""
 
33
  for chunk in stream:
34
- response += chunk.choices[0].delta.content or ""
 
 
 
35
 
36
  # End compute time measurement
37
  compute_time = time.time() - start_time
38
 
39
- return response, f"Compute Time: {compute_time:.2f} seconds"
40
 
41
  except Exception as e:
42
- return "Error: Unable to process your request.", str(e)
43
 
44
  # Gradio interface
45
  def gradio_ui():
46
  with gr.Blocks() as demo:
47
- gr.Markdown("""# πŸš€ IntellijMind: The Future of AI Chatbots\nExperience the most advanced chatbot for deep insights and chain-of-thought reasoning!""")
48
 
49
  with gr.Row():
50
- with gr.Column(scale=8):
51
  chat_history = gr.Chatbot(label="Chat History")
52
  with gr.Column(scale=2):
53
  compute_time = gr.Textbox(label="Compute Time", interactive=False)
 
54
 
55
  user_input = gr.Textbox(label="Type your message", placeholder="Ask me anything...", lines=2)
56
  send_button = gr.Button("Send", variant="primary")
57
  clear_button = gr.Button("Clear Chat")
58
 
59
  def handle_chat(chat_history, user_input):
60
- ai_response, compute_info = chat_with_cerebras(user_input)
61
  chat_history.append((user_input, ai_response))
62
- return chat_history, compute_info
63
 
64
  def clear_chat():
65
- return [], ""
66
 
67
- send_button.click(handle_chat, inputs=[chat_history, user_input], outputs=[chat_history, compute_time])
68
- clear_button.click(clear_chat, outputs=[chat_history, compute_time])
69
 
70
- gr.Markdown("""---\n### 🌟 Features:\n- **Advanced Reasoning**: Chain-of-thought explanations for complex queries.\n- **Performance Metrics**: Measure response compute time instantly.\n- **User-Friendly Design**: Intuitive chatbot interface.\n- **Powered by IntellijMind**: Redefining AI interaction standards.\n""")
71
 
72
  return demo
73
 
 
9
  def chat_with_cerebras(user_input):
10
  """
11
  Handles interaction with the Cerebras model.
12
+ Sends user input and returns the model's response along with compute time and chain-of-thought reasoning.
13
  """
14
  # Start compute time measurement
15
  start_time = time.time()
 
18
  # Create a chat stream with Cerebras
19
  stream = client.chat.completions.create(
20
  messages=[
21
+ {"role": "system", "content": "You are IntellijMind, an advanced AI designed to assist users with detailed insights, problem-solving, and chain-of-thought reasoning."},
22
  {"role": "user", "content": user_input}
23
  ],
24
  model="llama-3.3-70b",
 
30
 
31
  # Collect response from the stream
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
42
 
43
+ return response, chain_of_thought, f"Compute Time: {compute_time:.2f} seconds"
44
 
45
  except Exception as e:
46
+ return "Error: Unable to process your request.", "", str(e)
47
 
48
  # Gradio interface
49
  def gradio_ui():
50
  with gr.Blocks() as demo:
51
+ gr.Markdown("""# πŸš€ IntellijMind: The Future of AI Chatbots\nExperience the most advanced chatbot for deep insights, chain-of-thought reasoning, and unmatched clarity!""")
52
 
53
  with gr.Row():
54
+ with gr.Column(scale=6):
55
  chat_history = gr.Chatbot(label="Chat History")
56
  with gr.Column(scale=2):
57
  compute_time = gr.Textbox(label="Compute Time", interactive=False)
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
68
 
69
  def clear_chat():
70
+ return [], "", ""
71
 
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
78