BICORP commited on
Commit
766342a
Β·
verified Β·
1 Parent(s): 52cd08b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +287 -79
app.py CHANGED
@@ -1,102 +1,310 @@
1
  import gradio as gr
 
2
  from huggingface_hub import InferenceClient
3
 
4
- # Default client with the first model
5
- client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
 
6
 
7
- # Function to switch between models based on selection
8
- def switch_client(model_name: str):
9
- return InferenceClient(model_name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- # Define presets for each model
12
  presets = {
13
  "mistralai/Mistral-7B-Instruct-v0.3": {
14
- "Fast": {"max_tokens": 256, "temperature": 1.0, "top_p": 0.9},
15
- "Normal": {"max_tokens": 512, "temperature": 0.7, "top_p": 0.95},
16
- "Quality": {"max_tokens": 1024, "temperature": 0.5, "top_p": 0.90},
17
- "Unreal Performance": {"max_tokens": 2048, "temperature": 0.6, "top_p": 0.75},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  }
19
  }
20
 
21
- # Fixed system message
22
- SYSTEM_MESSAGE = "Your name is Lake 1 Base but my is User"
23
-
24
- def respond(
25
- message,
26
- history: list,
27
- model_name,
28
- preset_name
29
- ):
30
- # Switch client based on model selection
31
- global client
32
- client = switch_client(model_name)
33
-
34
- messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
35
-
36
- # Ensure history is a list of dictionaries
37
- for val in history:
38
- if isinstance(val, dict) and 'role' in val and 'content' in val:
39
- messages.append({"role": val['role'], "content": val['content']})
40
-
41
- messages.append({"role": "user", "content": message})
42
-
43
- # Get the preset settings
44
- preset = presets[model_name][preset_name]
45
- max_tokens = preset["max_tokens"]
46
- temperature = preset["temperature"]
47
- top_p = preset["top_p"]
48
-
49
- # Get the response from the model
50
- response = client.chat_completion(
51
- messages,
52
- max_tokens=max_tokens,
53
- temperature=temperature,
54
- top_p=top_p,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  )
 
56
 
57
- # Extract the content from the response
58
- final_response = response.choices[0].message['content']
59
-
60
- return final_response
61
-
62
- # Model names and their pseudonyms
63
- model_choices = [
64
- ("mistralai/Mistral-7B-Instruct-v0.3", "Lake 1 Base")
65
  ]
66
 
67
- # Convert pseudonyms to model names for the dropdown
68
- pseudonyms = [model[1] for model in model_choices]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
- # Function to handle model selection and pseudonyms
71
- def respond_with_pseudonym(
72
- message,
73
- history: list,
74
- selected_model, # Change this to selected_model
75
- selected_preset
76
- ):
77
- print(f"Selected Model: {selected_model}") # Debugging line
78
- print(f"Available Models: {pseudonyms}") # Debugging line
 
 
 
 
 
 
79
 
80
- # Find the actual model name from the pseudonym
 
81
  try:
82
- model_name = next(model[0] for model in model_choices if model[1] == selected_model)
 
 
 
 
 
 
83
  except StopIteration:
84
- return f"Error: The selected model '{selected_model}' is not valid. Please select a valid model."
 
 
 
 
85
 
86
- # Call the existing respond function
87
- response = respond(message, history, model_name, selected_preset)
 
 
88
 
89
- return response
90
-
91
- # Gradio Chat Interface
92
- demo = gr.ChatInterface(
93
- fn=respond_with_pseudonym,
94
- additional_inputs=[
95
- gr.Dropdown(choices=pseudonyms, label="Select Model", value=pseudonyms[0]), # Pseudonym selection dropdown
96
- gr.Dropdown(choices=list(presets["mistralai/Mistral-7B-Instruct-v0.3"].keys()), label="Select Preset", value="Fast") # Preset selection dropdown
 
 
 
 
 
 
 
 
 
 
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)