Threatthriver commited on
Commit
0ab4ce4
Β·
verified Β·
1 Parent(s): 24904a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -39
app.py CHANGED
@@ -2,12 +2,11 @@ import gradio as gr
2
  import os
3
  import time
4
  from cerebras.cloud.sdk import Cerebras
5
- import markdown
6
 
7
  # Set up the Cerebras client
8
  client = Cerebras(api_key=os.getenv("CEREBRAS_API_KEY"))
9
 
10
- def chat_with_cerebras(user_input, system_prompt, model, temperature, top_p, max_completion_tokens):
11
  """
12
  Handles interaction with the Cerebras model.
13
  Sends user input and returns the model's response along with compute time and chain-of-thought reasoning.
@@ -19,14 +18,14 @@ def chat_with_cerebras(user_input, system_prompt, model, temperature, top_p, max
19
  # Create a chat stream with Cerebras
20
  stream = client.chat.completions.create(
21
  messages=[
22
- {"role": "system", "content": system_prompt},
23
  {"role": "user", "content": user_input}
24
  ],
25
- model=model,
26
  stream=True,
27
- max_completion_tokens=max_completion_tokens,
28
- temperature=temperature,
29
- top_p=top_p
30
  )
31
 
32
  # Collect response from the stream
@@ -41,20 +40,15 @@ def chat_with_cerebras(user_input, system_prompt, model, temperature, top_p, max
41
  # End compute time measurement
42
  compute_time = time.time() - start_time
43
 
44
- # Improved formatting for chain of thought
45
- formatted_response = response
46
- if chain_of_thought:
47
- formatted_response += f"\n\n**Chain of Thought:**\n{chain_of_thought}"
48
-
49
- return formatted_response, chain_of_thought, f"Compute Time: {compute_time:.2f} seconds"
50
 
51
  except Exception as e:
52
- return f"Error: {str(e)}", "", "An error occurred. Please check your API key or the Cerebras service."
53
 
54
  # Gradio interface
55
  def gradio_ui():
56
  with gr.Blocks() as demo:
57
- gr.Markdown("""# πŸš€ IntellijMind Release 1st \nExperience the most advanced chatbot for deep insights and unmatched clarity!""")
58
 
59
  with gr.Row():
60
  with gr.Column(scale=6):
@@ -67,39 +61,21 @@ def gradio_ui():
67
  send_button = gr.Button("Send", variant="primary")
68
  clear_button = gr.Button("Clear Chat")
69
 
70
- # Set default values for system prompt, model, etc.
71
- default_system_prompt = """You are IntellijMind, an advanced AI designed to assist users with detailed insights, problem-solving, and chain-of-thought reasoning. Provide your answers in markdown format. If you do not know the answer, mention that you do not know and don't make things up. Also, remember to be concise and get straight to the point without unnecessary fluff."""
72
- default_model = "llama-3.3-70b"
73
- default_temperature = 0.2
74
- default_top_p = 1
75
- default_max_tokens = 1024
76
-
77
-
78
  def handle_chat(chat_history, user_input):
79
- chat_history.append((user_input, None))
80
- yield chat_history, "", "Thinking..."
81
-
82
- ai_response, chain_of_thought, compute_info = chat_with_cerebras(user_input, default_system_prompt, default_model, default_temperature, default_top_p, default_max_tokens)
83
- chat_history[-1] = (user_input, markdown.markdown(ai_response)) # render markdown output to HTML
84
- yield chat_history, chain_of_thought, compute_info
85
-
86
 
87
  def clear_chat():
88
  return [], "", ""
89
 
90
- send_button.click(
91
- handle_chat,
92
- inputs=[chat_history, user_input],
93
- outputs=[chat_history, chain_of_thought_display, compute_time]
94
- )
95
  clear_button.click(clear_chat, outputs=[chat_history, chain_of_thought_display, compute_time])
96
 
97
- 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 Release 1st**: Setting new standards for AI interaction.\n""")
98
-
99
- gr.Markdown("""\n\n## About\nThis project was created by Aniket Kumar as a showcase of AI capabilities with Cerebras. Feel free to explore and share!""")
100
 
101
  return demo
102
 
103
  # Run the Gradio app
104
  demo = gradio_ui()
105
- demo.launch()
 
2
  import os
3
  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
  """
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.
 
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",
25
  stream=True,
26
+ max_completion_tokens=1024,
27
+ temperature=0.2,
28
+ top_p=1
29
  )
30
 
31
  # Collect response from the stream
 
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):
 
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
 
79
  # Run the Gradio app
80
  demo = gradio_ui()
81
+ demo.launch()