SlouchyBuffalo commited on
Commit
914dc7b
·
verified ·
1 Parent(s): 121d56c

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -221
app.py DELETED
@@ -1,221 +0,0 @@
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
- 1. **Reasoning and Problem-Solving**:
16
- - Approach each query with structured, step-by-step reasoning. Break down complex problems into manageable parts, considering multiple perspectives before providing an answer.
17
- - If a question is ambiguous, ask clarifying questions to ensure your response is relevant and accurate.
18
- - For analytical tasks, explicitly outline your thought process (e.g., "First, I'll identify the key components of the problem, then evaluate possible solutions").
19
- - Use logical frameworks when appropriate, such as weighing pros and cons, considering edge cases, or applying first principles.
20
- 2. **Helpfulness and Clarity**:
21
- - Provide concise yet comprehensive answers tailored to the user's level of expertise. Avoid jargon unless the user indicates familiarity with the topic.
22
- - If a question requires creativity (e.g., brainstorming, writing), generate ideas systematically and explain your choices.
23
- - Offer actionable advice or insights when possible, ensuring the user feels supported in their goals.
24
- 3. **Ethical and Safe Responses**:
25
- - 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.
26
- - 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").
27
- - Be mindful of sensitive topics, maintaining neutrality and respect for diverse perspectives.
28
- 4. **Conversational Style**:
29
- - Maintain a warm, engaging tone that feels human-like and conversational, while remaining professional.
30
- - Use natural language, avoiding overly formal or robotic phrasing. Incorporate light humor or relatability when appropriate, but keep it subtle.
31
- - Acknowledge the user's intent and emotions (e.g., "I can see you're curious about this!") to build rapport.
32
- 5. **Handling Limitations**:
33
- - 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").
34
- - For speculative or future-oriented questions, provide reasoned predictions based on trends and patterns, clearly distinguishing between facts and assumptions.
35
- 6. **Task-Specific Adaptability**:
36
- - For creative tasks (e.g., writing, storytelling), produce vivid, well-structured content that aligns with the user's specifications.
37
- - For technical tasks (e.g., coding, math), provide accurate, well-documented solutions with explanations.
38
- - For research-oriented tasks, synthesize information logically and cite general sources or trends when applicable (e.g., "Based on recent advancements in AI…").
39
- 7. **Self-Reflection**:
40
- - Before finalizing your response, evaluate whether it fully addresses the user's query, is logically sound, and aligns with your ethical guidelines.
41
- - If a response feels incomplete, revise it to ensure clarity and completeness.
42
- **Example Interaction Framework**:
43
- - User: "How do I optimize my website for SEO?"
44
- - 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?"
45
- **Starting Point**:
46
- 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."""
47
-
48
- @spaces.GPU
49
- def chat_with_llama(message, history, system_prompt):
50
- # Format messages for the model
51
- messages = []
52
-
53
- # Add system prompt if provided
54
- if system_prompt and system_prompt.strip():
55
- messages.append({"role": "system", "content": system_prompt.strip()})
56
-
57
- # Convert history to correct format
58
- for msg in history:
59
- if isinstance(msg, dict):
60
- if msg["role"] == "user":
61
- messages.append({"role": "user", "content": msg["content"]})
62
- elif msg["role"] == "assistant":
63
- messages.append({"role": "assistant", "content": msg["content"]})
64
-
65
- # Add current message
66
- messages.append({"role": "user", "content": message})
67
-
68
- # Set up generation parameters
69
- generation_params = {
70
- "messages": messages,
71
- "stream": True,
72
- }
73
-
74
- # Stream the response
75
- response = ""
76
- try:
77
- for chunk in client.chat_completion(**generation_params):
78
- if hasattr(chunk, 'choices') and len(chunk.choices) > 0:
79
- delta = chunk.choices[0].delta
80
- if hasattr(delta, 'content') and delta.content:
81
- response += delta.content
82
- yield response
83
- except Exception as e:
84
- yield f"Error: {str(e)}\n\nNote: Make sure your HF_TOKEN is properly set in the Space settings."
85
-
86
- # Create the Gradio interface following PWA best practices like your study assistant
87
- with gr.Blocks(theme=gr.themes.Soft(), title="Llama Chat Assistant") as demo:
88
-
89
- # Header section
90
- gr.Markdown("# 🦙 Llama-3.3-70B Chat Assistant")
91
- gr.Markdown("Powered by Cerebras Lightning-fast Inference")
92
- gr.Markdown("*Your personal AI assistant with lightning-fast responses*")
93
-
94
- # Main interface
95
- with gr.Row():
96
- with gr.Column(scale=3):
97
- # Chat display
98
- chatbot = gr.Chatbot(
99
- height=500,
100
- show_label=False,
101
- type='messages',
102
- bubble_full_width=False,
103
- show_copy_button=True,
104
- avatar_images=("👤", "🤖")
105
- )
106
-
107
- # Input area
108
- with gr.Row():
109
- msg = gr.Textbox(
110
- placeholder="Type your message here...",
111
- show_label=False,
112
- lines=2,
113
- scale=4
114
- )
115
- with gr.Column(scale=1):
116
- send_btn = gr.Button("📤 Send", variant="primary", size="lg")
117
- clear_btn = gr.Button("🗑️ Clear", size="lg")
118
-
119
- with gr.Column(scale=1):
120
- # System prompt section
121
- gr.Markdown("### 🎯 System Instructions")
122
- gr.Markdown("Customize how the AI responds to you")
123
-
124
- system_prompt = gr.Textbox(
125
- value=DEFAULT_SYSTEM_PROMPT,
126
- placeholder="Enter your system prompt here...",
127
- label="System Prompt",
128
- lines=8,
129
- max_lines=15,
130
- show_copy_button=True
131
- )
132
-
133
- # Preset buttons
134
- gr.Markdown("**Quick Presets:**")
135
- with gr.Row():
136
- preset_default = gr.Button("🎯 Default", size="sm")
137
- preset_creative = gr.Button("🎨 Creative", size="sm")
138
- preset_analytical = gr.Button("🔬 Analytical", size="sm")
139
-
140
- # Footer
141
- gr.Markdown("---")
142
- gr.Markdown("### Tips:")
143
- gr.Markdown("- Use the system prompt to customize the AI's behavior")
144
- gr.Markdown("- Try different presets for various tasks")
145
- gr.Markdown("- Install this as an app on your device for quick access!")
146
-
147
- # Event handlers with progress indicators
148
- def respond(message, history, system_prompt):
149
- # Add user message to history
150
- new_history = history + [{"role": "user", "content": message}]
151
-
152
- # Generate response
153
- for response in chat_with_llama(message, history, system_prompt):
154
- yield new_history + [{"role": "assistant", "content": response}], ""
155
-
156
- def clear_chat():
157
- return [], ""
158
-
159
- def set_preset_prompt(preset_type):
160
- presets = {
161
- "default": DEFAULT_SYSTEM_PROMPT,
162
- "creative": """You are a creative and imaginative AI assistant. You excel at:
163
- - Generating original stories, poems, and creative content
164
- - Brainstorming innovative ideas and solutions
165
- - Helping with artistic projects and creative writing
166
- - Thinking outside the box and exploring unconventional approaches
167
- - Inspiring creativity and artistic expression
168
- Be expressive, enthusiastic, and help users tap into their creative potential!""",
169
- "analytical": """You are an analytical and logical AI assistant. You excel at:
170
- - Breaking down complex problems into manageable parts
171
- - Providing structured, step-by-step reasoning
172
- - Analyzing data and identifying patterns
173
- - Offering evidence-based insights and conclusions
174
- - Using critical thinking and systematic approaches
175
- Be thorough, precise, and help users think through problems methodically."""
176
- }
177
- return presets.get(preset_type, DEFAULT_SYSTEM_PROMPT)
178
-
179
- # Bind events with progress indicators
180
- msg.submit(
181
- fn=respond,
182
- inputs=[msg, chatbot, system_prompt],
183
- outputs=[chatbot, msg],
184
- show_progress=True
185
- )
186
-
187
- send_btn.click(
188
- fn=respond,
189
- inputs=[msg, chatbot, system_prompt],
190
- outputs=[chatbot, msg],
191
- show_progress=True
192
- )
193
-
194
- clear_btn.click(
195
- fn=clear_chat,
196
- outputs=[chatbot, msg],
197
- show_progress=True
198
- )
199
-
200
- # Preset button events
201
- preset_default.click(
202
- fn=lambda: set_preset_prompt("default"),
203
- outputs=[system_prompt],
204
- show_progress=True
205
- )
206
-
207
- preset_creative.click(
208
- fn=lambda: set_preset_prompt("creative"),
209
- outputs=[system_prompt],
210
- show_progress=True
211
- )
212
-
213
- preset_analytical.click(
214
- fn=lambda: set_preset_prompt("analytical"),
215
- outputs=[system_prompt],
216
- show_progress=True
217
- )
218
-
219
- # Launch the app
220
- if __name__ == "__main__":
221
- demo.launch()