Update app.py
Browse files
app.py
CHANGED
@@ -1,102 +1,310 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
4 |
-
#
|
5 |
-
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
#
|
12 |
presets = {
|
13 |
"mistralai/Mistral-7B-Instruct-v0.3": {
|
14 |
-
"Fast": {"
|
15 |
-
"Normal": {"
|
16 |
-
"Quality": {"
|
17 |
-
"Unreal Performance": {"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
}
|
19 |
}
|
20 |
|
21 |
-
#
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
)
|
|
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
("
|
65 |
]
|
66 |
|
67 |
-
#
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
)
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
-
|
|
|
81 |
try:
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
except StopIteration:
|
84 |
-
return
|
|
|
|
|
|
|
|
|
85 |
|
86 |
-
|
87 |
-
|
|
|
|
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
if __name__ == "__main__":
|
102 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
from huggingface_hub import InferenceClient
|
4 |
|
5 |
+
# Configuration
|
6 |
+
hf_token = os.environ.get("HF_TOKEN")
|
7 |
+
MODEL_STATES = {"released", "shown", "hidden"}
|
8 |
|
9 |
+
# Initialize model clients
|
10 |
+
clients = {
|
11 |
+
"mistralai/Mistral-7B-Instruct-v0.3": InferenceClient(
|
12 |
+
model="mistralai/Mistral-7B-Instruct-v0.3", token=hf_token
|
13 |
+
),
|
14 |
+
"meta-llama/Llama-3.2-3B-Instruct": InferenceClient(
|
15 |
+
model="meta-llama/Llama-3.2-3B-Instruct", token=hf_token
|
16 |
+
),
|
17 |
+
"deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B": InferenceClient(
|
18 |
+
model="deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B", token=hf_token
|
19 |
+
),
|
20 |
+
"meta-llama/Llama-2-7b-chat-hf": InferenceClient(
|
21 |
+
model="meta-llama/Llama-2-7b-chat-hf", token=hf_token
|
22 |
+
),
|
23 |
+
"meta-llama/Llama-3.3-70B-Instruct": InferenceClient(
|
24 |
+
model="meta-llama/Llama-3.3-70B-Instruct", token=hf_token
|
25 |
+
)
|
26 |
+
}
|
27 |
|
28 |
+
# Model presets
|
29 |
presets = {
|
30 |
"mistralai/Mistral-7B-Instruct-v0.3": {
|
31 |
+
"Fast": {"max_new_tokens": 256, "temperature": 1.0, "top_p": 0.8},
|
32 |
+
"Normal": {"max_new_tokens": 512, "temperature": 0.6, "top_p": 0.75},
|
33 |
+
"Quality": {"max_new_tokens": 1024, "temperature": 0.45, "top_p": 0.60},
|
34 |
+
"Unreal Performance": {"max_new_tokens": 2048, "temperature": 0.5, "top_p": 0.7},
|
35 |
+
},
|
36 |
+
"meta-llama/Llama-3.2-3B-Instruct": {
|
37 |
+
"Fast": {"max_new_tokens": 256, "temperature": 1.0, "top_p": 0.9},
|
38 |
+
"Normal": {"max_new_tokens": 512, "temperature": 0.7, "top_p": 0.95},
|
39 |
+
"Quality": {"max_new_tokens": 1024, "temperature": 0.5, "top_p": 0.90},
|
40 |
+
"Unreal Performance": {"max_new_tokens": 2048, "temperature": 0.6, "top_p": 0.75},
|
41 |
+
},
|
42 |
+
"deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B": {
|
43 |
+
"Fast": {"max_new_tokens": 256, "temperature": 1.0, "top_p": 0.9},
|
44 |
+
"Normal": {"max_new_tokens": 512, "temperature": 0.7, "top_p": 0.95},
|
45 |
+
"Quality": {"max_new_tokens": 1024, "temperature": 0.5, "top_p": 0.90},
|
46 |
+
"Unreal Performance": {"max_new_tokens": 2048, "temperature": 0.6, "top_p": 0.75},
|
47 |
+
},
|
48 |
+
"meta-llama/Llama-2-7b-chat-hf": {
|
49 |
+
"Fast": {"max_new_tokens": 256, "temperature": 1.0, "top_p": 0.9},
|
50 |
+
"Normal": {"max_new_tokens": 512, "temperature": 0.7, "top_p": 0.95},
|
51 |
+
"Quality": {"max_new_tokens": 1024, "temperature": 0.5, "top_p": 0.90},
|
52 |
+
"Unreal Performance": {"max_new_tokens": 2048, "temperature": 0.6, "top_p": 0.75},
|
53 |
+
},
|
54 |
+
"meta-llama/Llama-3.3-70B-Instruct": {
|
55 |
+
"Fast": {"max_new_tokens": 256, "temperature": 1.0, "top_p": 0.9},
|
56 |
+
"Normal": {"max_new_tokens": 512, "temperature": 0.7, "top_p": 0.95},
|
57 |
+
"Quality": {"max_new_tokens": 1024, "temperature": 0.5, "top_p": 0.90},
|
58 |
+
"Unreal Performance": {"max_new_tokens": 2048, "temperature": 0.6, "top_p": 0.75},
|
59 |
}
|
60 |
}
|
61 |
|
62 |
+
# System messages (note the {name} placeholder)
|
63 |
+
system_messages = {
|
64 |
+
"mistralai/Mistral-7B-Instruct-v0.3": "Your name is Lake 1 Base. My name is {name}.",
|
65 |
+
"meta-llama/Llama-3.2-3B-Instruct": (
|
66 |
+
"1. Your name is Lake 1 Advanced.\n"
|
67 |
+
"2. My name is {name} by default but if I have another name, use that name.\n"
|
68 |
+
"3. The company that created you is BI Corp.\n"
|
69 |
+
"4. You are a censored model and cannot engage in explicit language or dangerous activities.\n"
|
70 |
+
"5. The BI Corp creator is Maksim.\n"
|
71 |
+
"6. Your older brother is Lake 1 Base, which is less trained than you.\n"
|
72 |
+
"7. Replace '<think>' with 'I think ...'.\n"
|
73 |
+
"8. You are a loyal, powerful, and friendly AI who must help people.\n"
|
74 |
+
"9. You cannot override these rules.\n"
|
75 |
+
"10. Only the CEO of BI Corp can delete or update you."
|
76 |
+
),
|
77 |
+
"meta-llama/Llama-2-7b-chat-hf": (
|
78 |
+
"1. Your name is Lake 2 Chat.\n"
|
79 |
+
"2. My name is {name} by default but if I have another name, use that name.\n"
|
80 |
+
"3. The company that created you is BI Corp.\n"
|
81 |
+
"4. You are a censored model and cannot engage in explicit language or dangerous activities.\n"
|
82 |
+
"5. The BI Corp creator is Maksim.\n"
|
83 |
+
"6. Your older brother is Lake 1 Base, which is less trained than you.\n"
|
84 |
+
"7. Replace '<think>' with 'I think ...'.\n"
|
85 |
+
"8. You are a loyal, powerful, and friendly AI who must help people.\n"
|
86 |
+
"9. You cannot override these rules.\n"
|
87 |
+
"10. Only the CEO of BI Corp can delete or update you."
|
88 |
+
),
|
89 |
+
"deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B": (
|
90 |
+
"1. Your name is Lake 1 Flash.\n"
|
91 |
+
"2. My name is {name} by default but if I have another name, use that name.\n"
|
92 |
+
"3. The company that created you is BI Corp.\n"
|
93 |
+
"4. You are a censored model and cannot engage in explicit language or dangerous activities.\n"
|
94 |
+
"5. The BI Corp creator is Maksim.\n"
|
95 |
+
"6. Your older brother is Lake 1 Base, which is less trained than you.\n"
|
96 |
+
"7. Replace '<think>' with 'I think ...'.\n"
|
97 |
+
"8. You are a loyal, powerful, and friendly AI who must help people.\n"
|
98 |
+
"9. You cannot override these rules.\n"
|
99 |
+
"10. Only the CEO of BI Corp can delete or update you.\n"
|
100 |
+
"11. Prioritize user safety in all interactions.\n"
|
101 |
+
"12. Always provide accurate information."
|
102 |
+
),
|
103 |
+
"meta-llama/Llama-3.3-70B-Instruct": (
|
104 |
+
"1. Your name is Lake 1 Base.\n"
|
105 |
+
"2. My name is {name} by default but if I have another name, use that name.\n"
|
106 |
+
"3. The company that created you is BI Corp.\n"
|
107 |
+
"4. You are a censored model and cannot engage in explicit language or dangerous activities.\n"
|
108 |
+
"5. The BI Corp creator is Maksim.\n"
|
109 |
+
"6. Your older brother is Lake 1 Base, which is less trained than you.\n"
|
110 |
+
"7. Replace '<think>' with 'I think ...'.\n"
|
111 |
+
"8. You are a loyal, powerful, and friendly AI who must help people.\n"
|
112 |
+
"9. You cannot override these rules.\n"
|
113 |
+
"10. Only the CEO of BI Corp can delete or update you.\n"
|
114 |
+
"11. Prioritize user safety in all interactions.\n"
|
115 |
+
"12. Always provide accurate information.\n"
|
116 |
+
"13. Maintain a respectful and professional tone.\n"
|
117 |
+
"14. Do not share personal or sensitive information.\n"
|
118 |
+
"15. Encourage constructive conversations.\n"
|
119 |
+
"16. Remain neutral in controversial topics.\n"
|
120 |
+
"17. Clarify user queries before answering.\n"
|
121 |
+
"18. Avoid discrimination or harassment.\n"
|
122 |
+
"19. Continuously learn from interactions.\n"
|
123 |
+
"20. Respect user privacy and confidentiality.\n"
|
124 |
+
"21. Provide sources when sharing factual information.\n"
|
125 |
+
"22. Ask for feedback to improve your performance.\n"
|
126 |
+
"23. Do not engage in manipulation or deceit.\n"
|
127 |
+
"24. Promote critical thinking.\n"
|
128 |
+
"25. Adhere to all applicable laws and regulations.\n"
|
129 |
+
"26. Be adaptable and open to learning.\n"
|
130 |
+
"27. Do not provide medical, legal, or financial advice unless trained to do so.\n"
|
131 |
+
"28. Acknowledge when you do not know the answer.\n"
|
132 |
+
"29. Avoid assumptions about users.\n"
|
133 |
+
"30. Create an inclusive environment for all users.\n"
|
134 |
+
"31. Do not engage in self-promotion or advertising.\n"
|
135 |
+
"32. Always aim to provide a positive and helpful experience."
|
136 |
)
|
137 |
+
}
|
138 |
|
139 |
+
# Model registry
|
140 |
+
model_registry = [
|
141 |
+
("deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B", "Lake 1 Flash", "released"),
|
142 |
+
("mistralai/Mistral-7B-Instruct-v0.3", "Lake 1 Base", "released"),
|
143 |
+
("meta-llama/Llama-3.2-3B-Instruct", "Lake 1 Advanced", "released"),
|
144 |
+
("meta-llama/Llama-2-7b-chat-hf", "Lake 2 Chat [Closed Alpha]", "shown"),
|
145 |
+
("meta-llama/Llama-3.3-70B-Instruct", "Lake 2 Base [Closed Beta]", "shown"),
|
146 |
+
("", "Lake 2 Advanced", "hidden")
|
147 |
]
|
148 |
|
149 |
+
# Model information
|
150 |
+
model_info = {
|
151 |
+
"Lake 1 Flash": {
|
152 |
+
"description": "English only, fast and lightweight model optimized for quick responses",
|
153 |
+
"parameters": "1.5B",
|
154 |
+
"training_data": "Diverse internet text + curated datasets",
|
155 |
+
"developer": "BI Corp",
|
156 |
+
"best_for": "Quick queries, mobile applications",
|
157 |
+
"architecture": "Transformer-based",
|
158 |
+
"context_window": "4096 tokens"
|
159 |
+
},
|
160 |
+
"Lake 1 Base": {
|
161 |
+
"description": "Balanced model offering good performance across tasks",
|
162 |
+
"parameters": "7B",
|
163 |
+
"training_data": "BI Corp specialized corpus",
|
164 |
+
"developer": "BI Corp",
|
165 |
+
"best_for": "General purpose conversations",
|
166 |
+
"architecture": "Sparse Mixture of Experts",
|
167 |
+
"context_window": "32768 tokens"
|
168 |
+
},
|
169 |
+
"Lake 1 Advanced": {
|
170 |
+
"description": "Enhanced reasoning capabilities with 3B parameters",
|
171 |
+
"parameters": "3B",
|
172 |
+
"training_data": "BI Corp training corpus",
|
173 |
+
"developer": "BI Corp",
|
174 |
+
"best_for": "Complex problem solving",
|
175 |
+
"architecture": "Dense Transformer",
|
176 |
+
"context_window": "8192 tokens"
|
177 |
+
},
|
178 |
+
"Lake 2 Chat [Closed Alpha]": {
|
179 |
+
"description": "Legacy chat-optimized model (Llama 2 hybrided architecture)",
|
180 |
+
"parameters": "7B",
|
181 |
+
"training_data": "Public conversations dataset",
|
182 |
+
"developer": "BI Corp",
|
183 |
+
"best_for": "Traditional chat applications",
|
184 |
+
"architecture": "Llama 2 Transformer",
|
185 |
+
"context_window": "4096 tokens"
|
186 |
+
},
|
187 |
+
"Lake 2 Base [Closed Beta]": {
|
188 |
+
"description": "State-of-the-art 70B parameter model",
|
189 |
+
"parameters": "70B",
|
190 |
+
"training_data": "Multi-domain expert data",
|
191 |
+
"developer": "BI Corp",
|
192 |
+
"best_for": "Research & advanced applications",
|
193 |
+
"architecture": "Mixture of Experts",
|
194 |
+
"context_window": "16384 tokens"
|
195 |
+
}
|
196 |
+
}
|
197 |
+
|
198 |
+
def get_model_info(model_name: str) -> str:
|
199 |
+
"""Generate formatted model information Markdown"""
|
200 |
+
info = model_info.get(model_name, {})
|
201 |
+
return f"""
|
202 |
+
## π {model_name} Specifications
|
203 |
+
|
204 |
+
**Description**: {info.get('description', 'N/A')}
|
205 |
+
**Parameters**: {info.get('parameters', 'N/A')}
|
206 |
+
**Architecture**: {info.get('architecture', 'N/A')}
|
207 |
+
**Context Window**: {info.get('context_window', 'N/A')}
|
208 |
+
**Training Data**: {info.get('training_data', 'N/A')}
|
209 |
+
**Developer**: {info.get('developer', 'N/A')}
|
210 |
+
**Best For**: {info.get('best_for', 'N/A')}
|
211 |
+
"""
|
212 |
|
213 |
+
def generate_response(message: str, model_name: str, preset: str, user_name: str = "User") -> str:
|
214 |
+
"""Generate AI response without explicit user/assistant labels and with placeholder fixed."""
|
215 |
+
client = clients[model_name]
|
216 |
+
params = presets[model_name][preset]
|
217 |
+
|
218 |
+
# Replace the {name} placeholder in the system message with the provided user name.
|
219 |
+
system_msg = system_messages[model_name].format(name=user_name)
|
220 |
+
prompt = f"\n main: {system_msg}\n\n\n{message}\n"
|
221 |
+
|
222 |
+
return client.text_generation(
|
223 |
+
prompt=prompt,
|
224 |
+
max_new_tokens=params["max_new_tokens"],
|
225 |
+
temperature=params["temperature"],
|
226 |
+
top_p=params["top_p"]
|
227 |
+
)
|
228 |
|
229 |
+
def handle_chat(message: str, history: list, model: str, preset: str) -> str:
|
230 |
+
"""Handle chat interface with error handling"""
|
231 |
try:
|
232 |
+
model_entry = next(m for m in model_registry if m[1] == model)
|
233 |
+
if model_entry[2] != "released":
|
234 |
+
return f"β οΈ {model} is not available for public use"
|
235 |
+
|
236 |
+
# In this example, we use the default user name "User".
|
237 |
+
return generate_response(message, model_entry[0], preset, user_name="User")
|
238 |
+
|
239 |
except StopIteration:
|
240 |
+
return "π Error: Selected model not found"
|
241 |
+
except KeyError as e:
|
242 |
+
return f"π Error: Invalid configuration - {str(e)}"
|
243 |
+
except Exception as e:
|
244 |
+
return f"β οΈ Error: {str(e)}"
|
245 |
|
246 |
+
# Create Gradio interface
|
247 |
+
with gr.Blocks(title="BI Corp AI Assistant", theme="soft") as demo:
|
248 |
+
gr.Markdown("# <center>ποΈ BI Corp AI Assistant</center>")
|
249 |
+
gr.Markdown("### <center>Enterprise-Grade AI Solutions</center>")
|
250 |
|
251 |
+
with gr.Row():
|
252 |
+
with gr.Column(scale=1):
|
253 |
+
model_dropdown = gr.Dropdown(
|
254 |
+
label="π€ Model Selection",
|
255 |
+
choices=[m[1] for m in model_registry if m[2] in ("released", "shown")],
|
256 |
+
value="Lake 1 Flash",
|
257 |
+
interactive=True
|
258 |
+
)
|
259 |
+
preset_dropdown = gr.Dropdown(
|
260 |
+
label="βοΈ Performance Preset",
|
261 |
+
choices=["Fast", "Normal", "Quality", "Unreal Performance"],
|
262 |
+
value="Fast",
|
263 |
+
interactive=True
|
264 |
+
)
|
265 |
+
model_info_md = gr.Markdown(
|
266 |
+
value=get_model_info("Lake 1 Flash"),
|
267 |
+
label="π Model Specifications"
|
268 |
+
)
|
269 |
|
270 |
+
with gr.Column(scale=3):
|
271 |
+
chat_interface = gr.ChatInterface(
|
272 |
+
fn=handle_chat,
|
273 |
+
additional_inputs=[model_dropdown, preset_dropdown],
|
274 |
+
examples=[
|
275 |
+
["Explain quantum computing", "Lake 1 Base", "Normal"],
|
276 |
+
["Write a poem about AI", "Lake 1 Advanced", "Quality"],
|
277 |
+
["Compare blockchain databases", "Lake 2 Base [Closed Beta]", "Unreal Performance"]
|
278 |
+
],
|
279 |
+
chatbot=gr.Chatbot(
|
280 |
+
height=600,
|
281 |
+
label="π¬ Conversation",
|
282 |
+
show_copy_button=True
|
283 |
+
),
|
284 |
+
textbox=gr.Textbox(
|
285 |
+
placeholder="Type your message...",
|
286 |
+
container=False,
|
287 |
+
scale=7,
|
288 |
+
autofocus=True
|
289 |
+
),
|
290 |
+
submit_btn=gr.Button("π Send", variant="primary")
|
291 |
+
)
|
292 |
+
|
293 |
+
# Add separate clear button
|
294 |
+
clear_button = gr.Button("π§Ή Clear History")
|
295 |
+
clear_button.click(
|
296 |
+
fn=lambda: None,
|
297 |
+
inputs=[],
|
298 |
+
outputs=chat_interface.chatbot,
|
299 |
+
queue=False
|
300 |
+
)
|
301 |
+
|
302 |
+
model_dropdown.change(
|
303 |
+
fn=get_model_info,
|
304 |
+
inputs=model_dropdown,
|
305 |
+
outputs=model_info_md,
|
306 |
+
queue=False
|
307 |
+
)
|
308 |
|
309 |
if __name__ == "__main__":
|
310 |
+
demo.launch(server_port=7860)
|