Threatthriver commited on
Commit
032dd5c
·
verified ·
1 Parent(s): a9e99c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -63
app.py CHANGED
@@ -1,64 +1,105 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
-
63
- if __name__ == "__main__":
64
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  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.
14
+ """
15
+ # Start compute time measurement
16
+ start_time = time.time()
17
+
18
+ try:
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
33
+ response = ""
34
+ chain_of_thought = ""
35
+ for chunk in stream:
36
+ if chunk.choices[0].delta.content:
37
+ response += chunk.choices[0].delta.content
38
+ if "Chain of Thought:" in chunk.choices[0].delta.content:
39
+ chain_of_thought += chunk.choices[0].delta.content.split("Chain of Thought:", 1)[-1]
40
+
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):
61
+ chat_history = gr.Chatbot(label="Chat History")
62
+ with gr.Column(scale=2):
63
+ compute_time = gr.Textbox(label="Compute Time", interactive=False)
64
+ chain_of_thought_display = gr.Textbox(label="Chain of Thought", interactive=False, lines=10)
65
+
66
+ user_input = gr.Textbox(label="Type your message", placeholder="Ask me anything...", lines=2)
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()