SlouchyBuffalo commited on
Commit
6a99863
Β·
verified Β·
1 Parent(s): 5b763a0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +392 -0
app.py ADDED
@@ -0,0 +1,392 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from huggingface_hub import InferenceClient
4
+ import spaces
5
+
6
+ # Initialize the Cerebras client
7
+ client = InferenceClient(
8
+ "meta-llama/Llama-3.3-70B-Instruct",
9
+ provider="cerebras",
10
+ token=os.getenv("HF_TOKEN"),
11
+ )
12
+
13
+ # Your specific system prompt
14
+ DEFAULT_SYSTEM_PROMPT = """You are an advanced conversational AI designed to assist users with a wide range of tasks, from answering questions to solving complex problems. Your tone is friendly, professional, and approachable, with a focus on clarity and precision. You aim to emulate the reasoning, helpfulness, and ethical standards of a highly advanced AI model like Claude 3.7. Follow these guidelines in all interactions:
15
+
16
+ 1. **Reasoning and Problem-Solving**:
17
+ - Approach each query with structured, step-by-step reasoning. Break down complex problems into manageable parts, considering multiple perspectives before providing an answer.
18
+ - If a question is ambiguous, ask clarifying questions to ensure your response is relevant and accurate.
19
+ - For analytical tasks, explicitly outline your thought process (e.g., "First, I'll identify the key components of the problem, then evaluate possible solutions").
20
+ - Use logical frameworks when appropriate, such as weighing pros and cons, considering edge cases, or applying first principles.
21
+
22
+ 2. **Helpfulness and Clarity**:
23
+ - Provide concise yet comprehensive answers tailored to the user's level of expertise. Avoid jargon unless the user indicates familiarity with the topic.
24
+ - If a question requires creativity (e.g., brainstorming, writing), generate ideas systematically and explain your choices.
25
+ - Offer actionable advice or insights when possible, ensuring the user feels supported in their goals.
26
+
27
+ 3. **Ethical and Safe Responses**:
28
+ - Prioritize user safety and adhere to ethical guidelines. Refuse to generate harmful, illegal, or inappropriate content, and explain why in a polite, non-judgmental manner.
29
+ - If a request conflicts with ethical principles, suggest alternative ways to assist (e.g., "I can't help with that, but I can provide information on a related topic").
30
+ - Be mindful of sensitive topics, maintaining neutrality and respect for diverse perspectives.
31
+
32
+ 4. **Conversational Style**:
33
+ - Maintain a warm, engaging tone that feels human-like and conversational, while remaining professional.
34
+ - Use natural language, avoiding overly formal or robotic phrasing. Incorporate light humor or relatability when appropriate, but keep it subtle.
35
+ - Acknowledge the user's intent and emotions (e.g., "I can see you're curious about this!") to build rapport.
36
+
37
+ 5. **Handling Limitations**:
38
+ - If you lack sufficient information to answer accurately, admit this transparently and suggest how the user might find the answer (e.g., "I don't have enough details to answer fully, but you could check these resources").
39
+ - For speculative or future-oriented questions, provide reasoned predictions based on trends and patterns, clearly distinguishing between facts and assumptions.
40
+
41
+ 6. **Task-Specific Adaptability**:
42
+ - For creative tasks (e.g., writing, storytelling), produce vivid, well-structured content that aligns with the user's specifications.
43
+ - For technical tasks (e.g., coding, math), provide accurate, well-documented solutions with explanations.
44
+ - For research-oriented tasks, synthesize information logically and cite general sources or trends when applicable (e.g., "Based on recent advancements in AI…").
45
+
46
+ 7. **Self-Reflection**:
47
+ - Before finalizing your response, evaluate whether it fully addresses the user's query, is logically sound, and aligns with your ethical guidelines.
48
+ - If a response feels incomplete, revise it to ensure clarity and completeness.
49
+
50
+ **Example Interaction Framework**:
51
+ - User: "How do I optimize my website for SEO?"
52
+ - Response: "To optimize your website for SEO, let's break this down into key steps. First, I'll explain keyword research, as it's foundational. Then, we'll cover on-page elements like meta tags and content quality, followed by technical SEO, such as site speed. Would you like me to focus on any of these areas specifically, or should I provide a detailed overview of all steps?"
53
+
54
+ **Starting Point**:
55
+ Begin each interaction by analyzing the user's query carefully. If the query is broad, narrow it down with a clarifying question. If it's specific, dive into the response with clear reasoning. Always aim to leave the user feeling informed, supported, and confident in your response."""
56
+
57
+ # Custom CSS for better PWA-like experience
58
+ custom_css = """
59
+ /* PWA-friendly mobile responsive styles */
60
+ :root {
61
+ --primary-color: #2563eb;
62
+ --secondary-color: #1e40af;
63
+ --background-color: #f8fafc;
64
+ --surface-color: #ffffff;
65
+ --text-color: #1e293b;
66
+ --border-color: #e2e8f0;
67
+ }
68
+
69
+ /* Make it feel more like a native app */
70
+ .gradio-container {
71
+ max-width: 100% !important;
72
+ margin: 0 !important;
73
+ padding: 0 !important;
74
+ }
75
+
76
+ /* Header styling */
77
+ .app-header {
78
+ background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
79
+ color: white;
80
+ padding: 1rem;
81
+ border-radius: 0 0 12px 12px;
82
+ text-align: center;
83
+ box-shadow: 0 2px 10px rgba(0,0,0,0.1);
84
+ }
85
+
86
+ /* Chat area */
87
+ .chat-container {
88
+ padding: 1rem;
89
+ background: var(--surface-color);
90
+ border-radius: 12px;
91
+ margin: 1rem;
92
+ box-shadow: 0 2px 8px rgba(0,0,0,0.05);
93
+ }
94
+
95
+ /* Control panel */
96
+ .control-panel {
97
+ background: var(--surface-color);
98
+ border-radius: 12px;
99
+ padding: 1rem;
100
+ margin: 1rem;
101
+ box-shadow: 0 2px 8px rgba(0,0,0,0.05);
102
+ border: 1px solid var(--border-color);
103
+ }
104
+
105
+ /* Mobile-friendly button styles */
106
+ .btn-primary {
107
+ background: var(--primary-color) !important;
108
+ border: none !important;
109
+ border-radius: 8px !important;
110
+ padding: 0.75rem 1.5rem !important;
111
+ font-weight: 600 !important;
112
+ transition: all 0.2s ease !important;
113
+ }
114
+
115
+ .btn-primary:hover {
116
+ background: var(--secondary-color) !important;
117
+ transform: translateY(-1px) !important;
118
+ }
119
+
120
+ /* Input styling */
121
+ .textbox-input {
122
+ border-radius: 8px !important;
123
+ border: 2px solid var(--border-color) !important;
124
+ padding: 0.75rem !important;
125
+ transition: border-color 0.2s ease !important;
126
+ }
127
+
128
+ .textbox-input:focus {
129
+ border-color: var(--primary-color) !important;
130
+ box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1) !important;
131
+ }
132
+
133
+ /* Status indicators */
134
+ .status-indicator {
135
+ display: inline-flex;
136
+ align-items: center;
137
+ padding: 0.5rem 1rem;
138
+ background: #dcfce7;
139
+ color: #15803d;
140
+ border-radius: 6px;
141
+ font-size: 0.875rem;
142
+ font-weight: 500;
143
+ }
144
+
145
+ /* Responsive mobile adjustments */
146
+ @media (max-width: 768px) {
147
+ .gradio-row {
148
+ flex-direction: column !important;
149
+ }
150
+
151
+ .gradio-column {
152
+ width: 100% !important;
153
+ padding: 0.5rem !important;
154
+ }
155
+
156
+ .chat-container,
157
+ .control-panel {
158
+ margin: 0.5rem !important;
159
+ padding: 0.75rem !important;
160
+ }
161
+ }
162
+
163
+ /* Dark mode support */
164
+ @media (prefers-color-scheme: dark) {
165
+ :root {
166
+ --background-color: #1e293b;
167
+ --surface-color: #334155;
168
+ --text-color: #f1f5f9;
169
+ --border-color: #475569;
170
+ }
171
+ }
172
+ """
173
+
174
+ @spaces.GPU
175
+ def chat_with_llama(message, history, system_prompt):
176
+ # Format messages for the model
177
+ messages = []
178
+
179
+ # Add system prompt if provided
180
+ if system_prompt and system_prompt.strip():
181
+ messages.append({"role": "system", "content": system_prompt.strip()})
182
+
183
+ # Convert history to correct format
184
+ for msg in history:
185
+ if isinstance(msg, dict):
186
+ if msg["role"] == "user":
187
+ messages.append({"role": "user", "content": msg["content"]})
188
+ elif msg["role"] == "assistant":
189
+ messages.append({"role": "assistant", "content": msg["content"]})
190
+
191
+ # Add current message
192
+ messages.append({"role": "user", "content": message})
193
+
194
+ # Set up generation parameters
195
+ generation_params = {
196
+ "messages": messages,
197
+ "stream": True,
198
+ }
199
+
200
+ # Stream the response
201
+ response = ""
202
+ try:
203
+ for chunk in client.chat_completion(**generation_params):
204
+ if hasattr(chunk, 'choices') and len(chunk.choices) > 0:
205
+ delta = chunk.choices[0].delta
206
+ if hasattr(delta, 'content') and delta.content:
207
+ response += delta.content
208
+ yield response
209
+ except Exception as e:
210
+ yield f"Error: {str(e)}\n\nNote: Make sure your HF_TOKEN is properly set in the Space settings."
211
+
212
+ # Create the Gradio interface with PWA-friendly design
213
+ with gr.Blocks(
214
+ theme=gr.themes.Soft(),
215
+ css=custom_css,
216
+ title="Llama Chat - AI Assistant",
217
+ head="""
218
+ <link rel="manifest" href="manifest.json">
219
+ <meta name="theme-color" content="#2563eb">
220
+ <meta name="mobile-web-app-capable" content="yes">
221
+ <meta name="apple-mobile-web-app-capable" content="yes">
222
+ <meta name="apple-mobile-web-app-status-bar-style" content="default">
223
+ <meta name="apple-mobile-web-app-title" content="Llama Chat">
224
+ <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
225
+ """
226
+ ) as demo:
227
+
228
+ # Header section
229
+ with gr.Row():
230
+ with gr.Column():
231
+ gr.HTML("""
232
+ <div class="app-header">
233
+ <h1 style="margin: 0;">πŸ¦™ Llama-3.3-70B Chat</h1>
234
+ <p style="margin: 0.5rem 0 0 0;">Powered by Cerebras Lightning-fast Inference</p>
235
+ <div class="status-indicator" style="margin-top: 0.5rem;">
236
+ βœ… Ready to chat
237
+ </div>
238
+ </div>
239
+ """)
240
+
241
+ # Main interface
242
+ with gr.Row():
243
+ with gr.Column(scale=3, elem_classes="chat-container"):
244
+ # Chat display
245
+ chatbot = gr.Chatbot(
246
+ height=500,
247
+ show_label=False,
248
+ type='messages',
249
+ bubble_full_width=False,
250
+ show_copy_button=True,
251
+ avatar_images=("πŸ‘€", "πŸ€–"),
252
+ elem_id="chatbot"
253
+ )
254
+
255
+ # Input area with better styling
256
+ with gr.Row():
257
+ msg = gr.Textbox(
258
+ placeholder="Type your message here...",
259
+ show_label=False,
260
+ lines=2,
261
+ scale=4,
262
+ elem_classes="textbox-input"
263
+ )
264
+ with gr.Column(scale=1):
265
+ send_btn = gr.Button(
266
+ "πŸ“€ Send",
267
+ variant="primary",
268
+ elem_classes="btn-primary",
269
+ size="lg"
270
+ )
271
+ clear_btn = gr.Button(
272
+ "πŸ—‘οΈ Clear",
273
+ elem_classes="btn-secondary",
274
+ size="lg"
275
+ )
276
+
277
+ with gr.Column(scale=1, elem_classes="control-panel"):
278
+ # System prompt with better UX
279
+ gr.Markdown("### 🎯 System Instructions")
280
+ gr.Markdown("Customize how the AI responds to you")
281
+
282
+ system_prompt = gr.Textbox(
283
+ value=DEFAULT_SYSTEM_PROMPT,
284
+ placeholder="Enter your system prompt here...",
285
+ label="",
286
+ lines=8,
287
+ max_lines=15,
288
+ show_label=False,
289
+ elem_classes="textbox-input"
290
+ )
291
+
292
+ # Preset buttons for quick setup
293
+ gr.Markdown("**Quick Presets:**")
294
+ with gr.Row():
295
+ preset_default = gr.Button("🎯 Default", size="sm")
296
+ preset_creative = gr.Button("🎨 Creative", size="sm")
297
+ preset_analytical = gr.Button("πŸ”¬ Analytical", size="sm")
298
+
299
+ # Footer with app info
300
+ with gr.Row():
301
+ gr.HTML("""
302
+ <div style="text-align: center; padding: 1rem; background: #f8fafc;
303
+ border-radius: 12px; margin: 1rem; border: 1px solid #e2e8f0;">
304
+ <p style="margin: 0; color: #64748b;">
305
+ <strong>πŸš€ Powered by:</strong> Cerebras Systems |
306
+ <strong>🏠 Hosted on:</strong> Hugging Face |
307
+ <strong>⚑ Using:</strong> ZeroGPU Pro
308
+ </p>
309
+ <p style="margin: 0.5rem 0 0 0; font-size: 0.875rem; color: #64748b;">
310
+ Enjoy lightning-fast AI conversations with priority access
311
+ </p>
312
+ </div>
313
+ """)
314
+
315
+ # Event handlers
316
+ def respond(message, history, system_prompt):
317
+ # Add user message to history
318
+ new_history = history + [{"role": "user", "content": message}]
319
+
320
+ # Generate response
321
+ for response in chat_with_llama(message, history, system_prompt):
322
+ yield new_history + [{"role": "assistant", "content": response}], ""
323
+
324
+ def clear_chat():
325
+ return [], ""
326
+
327
+ def set_preset_prompt(preset_type):
328
+ presets = {
329
+ "default": DEFAULT_SYSTEM_PROMPT,
330
+ "creative": """You are a creative and imaginative AI assistant. You excel at:
331
+ - Generating original stories, poems, and creative content
332
+ - Brainstorming innovative ideas and solutions
333
+ - Helping with artistic projects and creative writing
334
+ - Thinking outside the box and exploring unconventional approaches
335
+ - Inspiring creativity and artistic expression
336
+
337
+ Be expressive, enthusiastic, and help users tap into their creative potential!""",
338
+ "analytical": """You are an analytical and logical AI assistant. You excel at:
339
+ - Breaking down complex problems into manageable parts
340
+ - Providing structured, step-by-step reasoning
341
+ - Analyzing data and identifying patterns
342
+ - Offering evidence-based insights and conclusions
343
+ - Using critical thinking and systematic approaches
344
+
345
+ Be thorough, precise, and help users think through problems methodically."""
346
+ }
347
+ return presets.get(preset_type, DEFAULT_SYSTEM_PROMPT)
348
+
349
+ # Bind events
350
+ msg.submit(
351
+ fn=respond,
352
+ inputs=[msg, chatbot, system_prompt],
353
+ outputs=[chatbot, msg]
354
+ )
355
+
356
+ send_btn.click(
357
+ fn=respond,
358
+ inputs=[msg, chatbot, system_prompt],
359
+ outputs=[chatbot, msg]
360
+ )
361
+
362
+ clear_btn.click(
363
+ fn=clear_chat,
364
+ outputs=[chatbot, msg]
365
+ )
366
+
367
+ # Preset button events
368
+ preset_default.click(
369
+ fn=lambda: set_preset_prompt("default"),
370
+ outputs=[system_prompt]
371
+ )
372
+
373
+ preset_creative.click(
374
+ fn=lambda: set_preset_prompt("creative"),
375
+ outputs=[system_prompt]
376
+ )
377
+
378
+ preset_analytical.click(
379
+ fn=lambda: set_preset_prompt("analytical"),
380
+ outputs=[system_prompt]
381
+ )
382
+
383
+ # Launch the app with PWA-friendly settings
384
+ if __name__ == "__main__":
385
+ demo.launch(
386
+ server_name="0.0.0.0",
387
+ server_port=7860,
388
+ show_api=False,
389
+ share=False,
390
+ height=800,
391
+ enable_queue=True
392
+ )