shukdevdatta123 commited on
Commit
136df13
·
verified ·
1 Parent(s): de6ef81

Create v2.txt

Browse files
Files changed (1) hide show
  1. v2.txt +441 -0
v2.txt ADDED
@@ -0,0 +1,441 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import re
4
+ from groq import Groq
5
+ import pandas as pd
6
+ import matplotlib.pyplot as plt
7
+ import io
8
+ import base64
9
+ from datetime import datetime, timedelta
10
+ import json
11
+
12
+ def validate_api_key(api_key):
13
+ """Validate if the API key has the correct format."""
14
+ # Basic format check for Groq API keys (they typically start with 'gsk_')
15
+ if not api_key.strip():
16
+ return False, "API key cannot be empty"
17
+
18
+ if not api_key.startswith("gsk_"):
19
+ return False, "Invalid API key format. Groq API keys typically start with 'gsk_'"
20
+
21
+ return True, "API key looks valid"
22
+
23
+ def test_api_connection(api_key):
24
+ """Test the API connection with a minimal request."""
25
+ try:
26
+ client = Groq(api_key=api_key)
27
+ # Making a minimal API call to test the connection
28
+ client.chat.completions.create(
29
+ model="llama3-70b-8192",
30
+ messages=[{"role": "user", "content": "test"}],
31
+ max_tokens=5
32
+ )
33
+ return True, "API connection successful"
34
+ except Exception as e:
35
+ # Handle all exceptions since Groq might not expose specific error types
36
+ if "authentication" in str(e).lower() or "api key" in str(e).lower():
37
+ return False, "Authentication failed: Invalid API key"
38
+ else:
39
+ return False, f"Error connecting to Groq API: {str(e)}"
40
+
41
+ # Ensure analytics directory exists
42
+ os.makedirs("analytics", exist_ok=True)
43
+
44
+ def log_chat_interaction(model, tokens_used, response_time, user_message_length):
45
+ """Log chat interactions for analytics"""
46
+ timestamp = datetime.now().isoformat()
47
+
48
+ log_file = "analytics/chat_log.json"
49
+
50
+ log_entry = {
51
+ "timestamp": timestamp,
52
+ "model": model,
53
+ "tokens_used": tokens_used,
54
+ "response_time_sec": response_time,
55
+ "user_message_length": user_message_length
56
+ }
57
+
58
+ # Append to existing log or create new file
59
+ if os.path.exists(log_file):
60
+ try:
61
+ with open(log_file, "r") as f:
62
+ logs = json.load(f)
63
+ except:
64
+ logs = []
65
+ else:
66
+ logs = []
67
+
68
+ logs.append(log_entry)
69
+
70
+ with open(log_file, "w") as f:
71
+ json.dump(logs, f, indent=2)
72
+
73
+ def get_template_prompt(template_name):
74
+ """Get system prompt for a given template name"""
75
+ templates = {
76
+ "General Assistant": "You are a helpful, harmless, and honest AI assistant.",
77
+ "Code Helper": "You are a programming assistant. Provide detailed code explanations and examples.",
78
+ "Creative Writer": "You are a creative writing assistant. Generate engaging and imaginative content.",
79
+ "Technical Expert": "You are a technical expert. Provide accurate, detailed technical information.",
80
+ "Data Analyst": "You are a data analysis assistant. Help interpret and analyze data effectively."
81
+ }
82
+
83
+ return templates.get(template_name, "")
84
+
85
+ def enhanced_chat_with_groq(api_key, model, user_message, temperature, max_tokens, top_p, chat_history, template_name=""):
86
+ """Enhanced chat function with analytics logging"""
87
+ start_time = datetime.now()
88
+
89
+ # Get system prompt if template is provided
90
+ system_prompt = get_template_prompt(template_name) if template_name else ""
91
+
92
+ # Validate and process as before
93
+ is_valid, message = validate_api_key(api_key)
94
+ if not is_valid:
95
+ return chat_history + [[user_message, f"Error: {message}"]]
96
+
97
+ connection_valid, connection_message = test_api_connection(api_key)
98
+ if not connection_valid:
99
+ return chat_history + [[user_message, f"Error: {connection_message}"]]
100
+
101
+ try:
102
+ # Format history
103
+ messages = []
104
+
105
+ if system_prompt:
106
+ messages.append({"role": "system", "content": system_prompt})
107
+
108
+ for human, assistant in chat_history:
109
+ messages.append({"role": "user", "content": human})
110
+ messages.append({"role": "assistant", "content": assistant})
111
+
112
+ messages.append({"role": "user", "content": user_message})
113
+
114
+ # Make API call
115
+ client = Groq(api_key=api_key)
116
+ response = client.chat.completions.create(
117
+ model=model,
118
+ messages=messages,
119
+ temperature=temperature,
120
+ max_tokens=max_tokens,
121
+ top_p=top_p
122
+ )
123
+
124
+ # Calculate metrics
125
+ end_time = datetime.now()
126
+ response_time = (end_time - start_time).total_seconds()
127
+ tokens_used = response.usage.total_tokens
128
+
129
+ # Log the interaction
130
+ log_chat_interaction(
131
+ model=model,
132
+ tokens_used=tokens_used,
133
+ response_time=response_time,
134
+ user_message_length=len(user_message)
135
+ )
136
+
137
+ # Extract response
138
+ assistant_response = response.choices[0].message.content
139
+
140
+ return chat_history + [[user_message, assistant_response]]
141
+
142
+ except Exception as e:
143
+ error_message = f"Error: {str(e)}"
144
+ return chat_history + [[user_message, error_message]]
145
+
146
+ def clear_conversation():
147
+ """Clear the conversation history."""
148
+ return []
149
+
150
+ def plt_to_html(fig):
151
+ """Convert matplotlib figure to HTML img tag"""
152
+ buf = io.BytesIO()
153
+ fig.savefig(buf, format="png", bbox_inches="tight")
154
+ buf.seek(0)
155
+ img_str = base64.b64encode(buf.read()).decode("utf-8")
156
+ plt.close(fig)
157
+ return f'<img src="data:image/png;base64,{img_str}" alt="Chart">'
158
+
159
+ def generate_analytics():
160
+ """Generate analytics from the chat log"""
161
+ log_file = "analytics/chat_log.json"
162
+
163
+ if not os.path.exists(log_file):
164
+ return "No analytics data available yet.", None, None, None, []
165
+
166
+ try:
167
+ with open(log_file, "r") as f:
168
+ logs = json.load(f)
169
+
170
+ if not logs:
171
+ return "No analytics data available yet.", None, None, None, []
172
+
173
+ # Convert to DataFrame
174
+ df = pd.DataFrame(logs)
175
+ df["timestamp"] = pd.to_datetime(df["timestamp"])
176
+
177
+ # Generate usage by model chart
178
+ model_usage = df.groupby("model").agg({
179
+ "tokens_used": "sum",
180
+ "timestamp": "count"
181
+ }).reset_index()
182
+ model_usage.columns = ["model", "total_tokens", "request_count"]
183
+
184
+ fig1 = plt.figure(figsize=(10, 6))
185
+ plt.bar(model_usage["model"], model_usage["total_tokens"])
186
+ plt.title("Token Usage by Model")
187
+ plt.xlabel("Model")
188
+ plt.ylabel("Total Tokens Used")
189
+ plt.xticks(rotation=45)
190
+ plt.tight_layout()
191
+ model_usage_img = plt_to_html(fig1)
192
+
193
+ # Generate usage over time chart
194
+ df["date"] = df["timestamp"].dt.date
195
+ daily_usage = df.groupby("date").agg({
196
+ "tokens_used": "sum"
197
+ }).reset_index()
198
+
199
+ fig2 = plt.figure(figsize=(10, 6))
200
+ plt.plot(daily_usage["date"], daily_usage["tokens_used"], marker="o")
201
+ plt.title("Daily Token Usage")
202
+ plt.xlabel("Date")
203
+ plt.ylabel("Tokens Used")
204
+ plt.grid(True)
205
+ plt.tight_layout()
206
+ daily_usage_img = plt_to_html(fig2)
207
+
208
+ # Generate response time chart
209
+ model_response_time = df.groupby("model").agg({
210
+ "response_time_sec": "mean"
211
+ }).reset_index()
212
+
213
+ fig3 = plt.figure(figsize=(10, 6))
214
+ plt.bar(model_response_time["model"], model_response_time["response_time_sec"])
215
+ plt.title("Average Response Time by Model")
216
+ plt.xlabel("Model")
217
+ plt.ylabel("Response Time (seconds)")
218
+ plt.xticks(rotation=45)
219
+ plt.tight_layout()
220
+ response_time_img = plt_to_html(fig3)
221
+
222
+ # Summary statistics
223
+ total_tokens = df["tokens_used"].sum()
224
+ total_requests = len(df)
225
+ avg_response_time = df["response_time_sec"].mean()
226
+
227
+ # Handling the case where there might not be enough data
228
+ if not model_usage.empty:
229
+ most_used_model = model_usage.iloc[model_usage["request_count"].argmax()]["model"]
230
+ else:
231
+ most_used_model = "N/A"
232
+
233
+ summary = f"""
234
+ ## Analytics Summary
235
+
236
+ - **Total API Requests**: {total_requests}
237
+ - **Total Tokens Used**: {total_tokens:,}
238
+ - **Average Response Time**: {avg_response_time:.2f} seconds
239
+ - **Most Used Model**: {most_used_model}
240
+ - **Date Range**: {df["timestamp"].min().date()} to {df["timestamp"].max().date()}
241
+ """
242
+
243
+ return summary, model_usage_img, daily_usage_img, response_time_img, df.to_dict("records")
244
+
245
+ except Exception as e:
246
+ error_message = f"Error generating analytics: {str(e)}"
247
+ return error_message, None, None, None, []
248
+
249
+ # Define available models
250
+ models = [
251
+ "llama3-70b-8192",
252
+ "llama3-8b-8192",
253
+ "mistral-saba-24b",
254
+ "gemma2-9b-it",
255
+ "allam-2-7b"
256
+ ]
257
+
258
+ # Define templates
259
+ templates = ["General Assistant", "Code Helper", "Creative Writer", "Technical Expert", "Data Analyst"]
260
+
261
+ # Create the Gradio interface
262
+ with gr.Blocks(title="Groq AI Chat Playground") as app:
263
+ gr.Markdown("# Groq AI Chat Playground")
264
+
265
+ # Create tabs for Chat and Analytics
266
+ with gr.Tabs():
267
+ with gr.Tab("Chat"):
268
+ # New model information accordion
269
+ with gr.Accordion("ℹ️ Model Information - Learn about available models", open=False):
270
+ gr.Markdown("""
271
+ ### Available Models and Use Cases
272
+
273
+ **llama3-70b-8192**
274
+ - Meta's most powerful language model
275
+ - 70 billion parameters with 8192 token context window
276
+ - Best for: Complex reasoning, sophisticated content generation, creative writing, and detailed analysis
277
+ - Optimal for users needing the highest quality AI responses
278
+
279
+ **llama3-8b-8192**
280
+ - Lighter version of Llama 3
281
+ - 8 billion parameters with 8192 token context window
282
+ - Best for: Faster responses, everyday tasks, simpler queries
283
+ - Good balance between performance and speed
284
+
285
+ **mistral-saba-24b**
286
+ - Mistral AI's advanced model
287
+ - 24 billion parameters
288
+ - Best for: High-quality reasoning, code generation, and structured outputs
289
+ - Excellent for technical and professional use cases
290
+
291
+ **gemma2-9b-it**
292
+ - Google's instruction-tuned model
293
+ - 9 billion parameters
294
+ - Best for: Following specific instructions, educational content, and general knowledge queries
295
+ - Well-rounded performance for various tasks
296
+
297
+ **allam-2-7b**
298
+ - Specialized model from Aleph Alpha
299
+ - 7 billion parameters
300
+ - Best for: Multilingual support, concise responses, and straightforward Q&A
301
+ - Good for international users and simpler applications
302
+
303
+ *Note: Larger models generally provide higher quality responses but may take slightly longer to generate.*
304
+ """)
305
+
306
+ gr.Markdown("Enter your Groq API key to start chatting with AI models.")
307
+
308
+ with gr.Row():
309
+ with gr.Column(scale=2):
310
+ api_key_input = gr.Textbox(
311
+ label="Groq API Key",
312
+ placeholder="Enter your Groq API key (starts with gsk_)",
313
+ type="password"
314
+ )
315
+
316
+ with gr.Column(scale=1):
317
+ test_button = gr.Button("Test API Connection")
318
+ api_status = gr.Textbox(label="API Status", interactive=False)
319
+
320
+ with gr.Row():
321
+ with gr.Column(scale=2):
322
+ model_dropdown = gr.Dropdown(
323
+ choices=models,
324
+ label="Select Model",
325
+ value="llama3-70b-8192"
326
+ )
327
+ with gr.Column(scale=1):
328
+ template_dropdown = gr.Dropdown(
329
+ choices=templates,
330
+ label="Select Template",
331
+ value="General Assistant"
332
+ )
333
+
334
+ with gr.Row():
335
+ with gr.Column():
336
+ with gr.Accordion("Advanced Settings", open=False):
337
+ temperature_slider = gr.Slider(
338
+ minimum=0.0, maximum=1.0, value=0.7, step=0.01,
339
+ label="Temperature (higher = more creative, lower = more focused)"
340
+ )
341
+ max_tokens_slider = gr.Slider(
342
+ minimum=256, maximum=8192, value=4096, step=256,
343
+ label="Max Tokens (maximum length of response)"
344
+ )
345
+ top_p_slider = gr.Slider(
346
+ minimum=0.0, maximum=1.0, value=0.95, step=0.01,
347
+ label="Top P (nucleus sampling probability threshold)"
348
+ )
349
+
350
+ chatbot = gr.Chatbot(label="Conversation", height=500)
351
+
352
+ with gr.Row():
353
+ message_input = gr.Textbox(
354
+ label="Your Message",
355
+ placeholder="Type your message here...",
356
+ lines=3
357
+ )
358
+
359
+ with gr.Row():
360
+ submit_button = gr.Button("Send", variant="primary")
361
+ clear_button = gr.Button("Clear Conversation")
362
+
363
+ # Analytics Dashboard Tab
364
+ with gr.Tab("Analytics Dashboard"):
365
+ with gr.Column():
366
+ gr.Markdown("# Usage Analytics Dashboard")
367
+ refresh_analytics_button = gr.Button("Refresh Analytics")
368
+
369
+ analytics_summary = gr.Markdown()
370
+
371
+ with gr.Row():
372
+ with gr.Column():
373
+ model_usage_chart = gr.HTML(label="Token Usage by Model")
374
+ with gr.Column():
375
+ daily_usage_chart = gr.HTML(label="Daily Token Usage")
376
+
377
+ response_time_chart = gr.HTML(label="Response Time by Model")
378
+
379
+ with gr.Accordion("Raw Data", open=False):
380
+ analytics_table = gr.DataFrame(label="Raw Analytics Data")
381
+
382
+ # Connect components with functions
383
+ submit_button.click(
384
+ fn=enhanced_chat_with_groq,
385
+ inputs=[
386
+ api_key_input,
387
+ model_dropdown,
388
+ message_input,
389
+ temperature_slider,
390
+ max_tokens_slider,
391
+ top_p_slider,
392
+ chatbot,
393
+ template_dropdown
394
+ ],
395
+ outputs=chatbot
396
+ ).then(
397
+ fn=lambda: "",
398
+ inputs=None,
399
+ outputs=message_input
400
+ )
401
+
402
+ message_input.submit(
403
+ fn=enhanced_chat_with_groq,
404
+ inputs=[
405
+ api_key_input,
406
+ model_dropdown,
407
+ message_input,
408
+ temperature_slider,
409
+ max_tokens_slider,
410
+ top_p_slider,
411
+ chatbot,
412
+ template_dropdown
413
+ ],
414
+ outputs=chatbot
415
+ ).then(
416
+ fn=lambda: "",
417
+ inputs=None,
418
+ outputs=message_input
419
+ )
420
+
421
+ clear_button.click(
422
+ fn=clear_conversation,
423
+ inputs=None,
424
+ outputs=chatbot
425
+ )
426
+
427
+ test_button.click(
428
+ fn=test_api_connection,
429
+ inputs=[api_key_input],
430
+ outputs=[api_status]
431
+ )
432
+
433
+ refresh_analytics_button.click(
434
+ fn=generate_analytics,
435
+ inputs=[],
436
+ outputs=[analytics_summary, model_usage_chart, daily_usage_chart, response_time_chart, analytics_table]
437
+ )
438
+
439
+ # Launch the app
440
+ if __name__ == "__main__":
441
+ app.launch(share=False)