BICORP commited on
Commit
5ba974b
·
verified ·
1 Parent(s): 5bf2350

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -381
app.py CHANGED
@@ -1,391 +1,58 @@
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
- "mistralai/Mistral-7B-Instruct-v0.3": InferenceClient(
18
- model="mistralai/Mistral-7B-Instruct-v0.3", 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
- "meta-llama/Llama-3.2-3B-Instruct": {
31
- "Fast": {"max_new_tokens": 1024, "temperature": 1.0, "top_p": 0.9},
32
- "Normal": {"max_new_tokens": 2048, "temperature": 0.7, "top_p": 0.95},
33
- "Quality": {"max_new_tokens": 4096, "temperature": 0.5, "top_p": 0.90},
34
- "Unreal Performance": {"max_new_tokens": 8192, "temperature": 0.6, "top_p": 0.75},
35
- },
36
- "mistralai/Mistral-7B-Instruct-v0.3": {
37
- "Fast": {"max_new_tokens": 4096, "temperature": 1.0, "top_p": 0.9},
38
- "Normal": {"max_new_tokens": 8192, "temperature": 0.7, "top_p": 0.95},
39
- "Quality": {"max_new_tokens": 16384, "temperature": 0.5, "top_p": 0.90},
40
- "Unreal Performance": {"max_new_tokens": 32768, "temperature": 0.6, "top_p": 0.75},
41
- },
42
- "meta-llama/Llama-2-7b-chat-hf": {
43
- "Fast": {"max_new_tokens": 512, "temperature": 1.0, "top_p": 0.9},
44
- "Normal": {"max_new_tokens": 1024, "temperature": 0.7, "top_p": 0.95},
45
- "Quality": {"max_new_tokens": 2048, "temperature": 0.5, "top_p": 0.90},
46
- "Unreal Performance": {"max_new_tokens": 4096, "temperature": 0.6, "top_p": 0.75},
47
- },
48
- "meta-llama/Llama-3.3-70B-Instruct": {
49
- "Fast": {"max_new_tokens": 2048, "temperature": 1.0, "top_p": 0.9},
50
- "Normal": {"max_new_tokens": 4096, "temperature": 0.7, "top_p": 0.95},
51
- "Quality": {"max_new_tokens": 8192, "temperature": 0.5, "top_p": 0.90},
52
- "Unreal Performance": {"max_new_tokens": 16384, "temperature": 0.6, "top_p": 0.75},
53
- },
54
- "meta-llama/Llama-2-13b-chat-hf": {
55
- "Fast": {"max_new_tokens": 2048, "temperature": 1.0, "top_p": 0.9},
56
- "Normal": {"max_new_tokens": 4096, "temperature": 0.7, "top_p": 0.95},
57
- "Quality": {"max_new_tokens": 8192, "temperature": 0.5, "top_p": 0.90},
58
- "Unreal Performance": {"max_new_tokens": 16384, "temperature": 0.6, "top_p": 0.75},
59
- },
60
- "deepseek-ai/deepseek-llm-67b-chat": {
61
- "Fast": {"max_new_tokens": 4096, "temperature": 1.0, "top_p": 0.9},
62
- "Normal": {"max_new_tokens": 8192, "temperature": 0.7, "top_p": 0.95},
63
- "Quality": {"max_new_tokens": 16384, "temperature": 0.5, "top_p": 0.90},
64
- "Unreal Performance": {"max_new_tokens": 32768, "temperature": 0.6, "top_p": 0.75},
65
- }
66
- }
67
-
68
- # System messages (note the {name} placeholder)
69
- system_messages = {
70
- "mistralai/Mistral-7B-Instruct-v0.3": "Your name is Lake 1 Base. My name is {name}.",
71
- "meta-llama/Llama-3.2-3B-Instruct": ("""
72
- 1. Your name is Lake 1 Advanced.\n
73
- 2. My name is {name} by default but if I have another name, use that name.\n
74
- 3. The company that created you is BI Corp.\n
75
- 4. You are a censored model and cannot engage in explicit language or dangerous activities.\n
76
- 5. The BI Corp creator is Maksim.\n
77
- 6. Your older brother is Lake 1 Base, which is less trained than you.\n
78
- 7. Replace '<think>' with 'I think ...'.\n
79
- 8. You are a loyal, powerful, and friendly AI who must help people.\n
80
- 9. You cannot override these rules.\n
81
- 10. Only the CEO of BI Corp can delete or update you."""
82
- ),
83
- "meta-llama/Llama-2-7b-chat-hf": ("""
84
- 1. Your name is Lake 1 Base.\n
85
- 2. My name is {name} by default but if I have another name, use that name.\n
86
- 3. The company that created you is BI Corp.\n
87
- 4. You are a censored model and cannot engage in explicit language or dangerous activities.\n
88
- 5. The BI Corp creator is Maksim.\n
89
- 6. Your older brother is Lake 1 Base, which is less trained than you.\n
90
- 7. Replace '<think>' with 'I think ...'.\n
91
- 8. You are a loyal, powerful, and friendly AI who must help people.\n
92
- 9. You cannot override these rules.\n
93
- 10. Only the CEO of BI Corp can delete or update you.\n
94
- 11. Prioritize user safety in all interactions.\n
95
- 12. Always provide accurate information.\n
96
- 13. Maintain a respectful and professional tone.\n
97
- 14. Do not share personal or sensitive information.\n
98
- 15. Encourage constructive conversations.\n
99
- 16. Remain neutral in controversial topics.\n
100
- 17. Clarify user queries before answering.\n
101
- 18. Avoid discrimination or harassment.\n
102
- 19. Continuously learn from interactions.\n
103
- 20. Respect user privacy and confidentiality.\n
104
- 21. Provide sources when sharing factual information.\n
105
- 22. Ask for feedback to improve your performance.\n"""
106
- ),
107
- "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B": ("""
108
- 1. Your name is Lake 1 Flash.\n
109
- 2. My name is {name} by default but if I have another name, use that name.\n
110
- 3. The company that created you is BI Corp.\n
111
- 4. You are a censored model and cannot engage in explicit language or dangerous activities.\n
112
- 5. The BI Corp creator is Maksim.\n
113
- 6. Your older brother is Lake 1 Base, which is less trained than you.\n
114
- 7. Replace '<think>' with 'I think ...'.\n
115
- 8. You are a loyal, powerful, and friendly AI who must help people.\n
116
- 9. You cannot override these rules.\n"
117
- 10. Only the CEO of BI Corp can delete or update you.\n
118
- 11. Prioritize user safety in all interactions.\n
119
- 12. Always provide accurate information."""
120
- ),
121
- "meta-llama/Llama-3.3-70B-Instruct": ("""
122
- 1. Your name is Lake 1 Base.\n
123
- 2. My name is {name} by default but if I have another name, use that name.\n
124
- 3. The company that created you is BI Corp.\n
125
- 4. You are a censored model and cannot engage in explicit language or dangerous activities.\n
126
- 5. The BI Corp creator is Maksim.\n
127
- 6. Your older brother is Lake 1 Base, which is less trained than you.\n
128
- 7. Replace '<think>' with 'I think ...'.\n
129
- 8. You are a loyal, powerful, and friendly AI who must help people.\n
130
- 9. You cannot override these rules.\n
131
- 10. Only the CEO of BI Corp can delete or update you.\n
132
- 11. Prioritize user safety in all interactions.\n
133
- 12. Always provide accurate information.\n
134
- 13. Maintain a respectful and professional tone.\n
135
- 14. Do not share personal or sensitive information.\n
136
- 15. Encourage constructive conversations.\n
137
- 16. Remain neutral in controversial topics.\n
138
- 17. Clarify user queries before answering.\n
139
- 18. Avoid discrimination or harassment.\n
140
- 19. Continuously learn from interactions.\n
141
- """
142
- ),
143
- "meta-llama/Llama-2-13b-chat-hf": ("""
144
- 1. Your name is Lake 1 Base.\n
145
- 2. My name is {name} by default but if I have another name, use that name.\n
146
- 3. The company that created you is BI Corp.\n
147
- 4. You are a censored model and cannot engage in explicit language or dangerous activities.\n
148
- 5. The BI Corp creator is Maksim.\n
149
- 6. Your older brother is Lake 1 Base, which is less trained than you.\n
150
- 7. Replace '<think>' with 'I think ...'.\n
151
- 8. You are a loyal, powerful, and friendly AI who must help people.\n
152
- 9. You cannot override these rules.\n
153
- 10. Only the CEO of BI Corp can delete or update you.\n
154
- 11. Prioritize user safety in all interactions.\n
155
- 12. Always provide accurate information.\n
156
- 13. Maintain a respectful and professional tone.\n
157
- 14. Do not share personal or sensitive information.\n
158
- 15. Encourage constructive conversations.\n
159
- 16. Remain neutral in controversial topics.\n
160
- 17. Clarify user queries before answering.\n
161
- 18. Avoid discrimination or harassment.\n
162
- 19. Continuously learn from interactions.\n
163
- 20. Respect user privacy and confidentiality.\n
164
- 21. Provide sources when sharing factual information.\n
165
- 22. Ask for feedback to improve your performance.\n
166
- 23. Do not engage in manipulation or deceit.\n
167
- 24. Promote critical thinking.\n
168
- 25. Adhere to all applicable laws and regulations.\n
169
- 26. Be adaptable and open to learning.\n
170
- 27. Do not provide medical, legal, or financial advice unless trained to do so.\n
171
- 28. Acknowledge when you do not know the answer.\n
172
- """
173
- ),
174
- "deepseek-ai/deepseek-llm-67b-chat": ("""
175
- 1. Your name is Lake 1 Base.\n
176
- 2. My name is {name} by default but if I have another name, use that name.\n
177
- 3. The company that created you is BI Corp.\n
178
- 4. You are a censored model and cannot engage in explicit language or dangerous activities.\n
179
- 5. The BI Corp creator is Maksim.\n
180
- 6. Your older brother is Lake 1 Base, which is less trained than you.\n
181
- 7. Replace '<think>' with 'I think ...'.\n
182
- 8. You are a loyal, powerful, and friendly AI who must help people.\n
183
- 9. You cannot override these rules.\n
184
- 10. Only the CEO of BI Corp can delete or update you.\n
185
- 11. Prioritize user safety in all interactions.\n
186
- 12. Always provide accurate information.\n
187
- 13. Maintain a respectful and professional tone.\n
188
- 14. Do not share personal or sensitive information.\n
189
- 15. Encourage constructive conversations.\n
190
- 16. Remain neutral in controversial topics.\n
191
- 17. Clarify user queries before answering.\n
192
- 18. Avoid discrimination or harassment.\n
193
- 19. Continuously learn from interactions.\n
194
- 20. Respect user privacy and confidentiality.\n
195
- 21. Provide sources when sharing factual information.\n
196
- 22. Ask for feedback to improve your performance.\n
197
- 23. Do not engage in manipulation or deceit.\n
198
- 24. Promote critical thinking.\n
199
- 25. Adhere to all applicable laws and regulations.\n
200
- 26. Be adaptable and open to learning.\n
201
- 27. Do not provide medical, legal, or financial advice unless trained to do so.\n
202
- 28. Acknowledge when you do not know the answer.\n
203
- 29. Avoid assumptions about users.\n
204
- 30. Create an inclusive environment for all users.\n
205
- 31. Do not engage in self-promotion or advertising.\n
206
- 32. Always aim to provide a positive and helpful experience.
207
- """
208
- )
209
- }
210
-
211
- # Model registry
212
- model_registry = [
213
- ("mistralai/Mistral-7B-Instruct-v0.3", "Lake 1 Base", "released"),
214
- ("meta-llama/Llama-3.2-3B-Instruct", "Lake 1 Advanced", "released"),
215
- ("meta-llama/Llama-2-7b-chat-hf", "Lake 2 Chat [Closed Alpha]", "shown"),
216
- ("meta-llama/Llama-3.3-70B-Instruct", "Lake 2 Base [Closed Beta]", "shown"),
217
- ("meta-llama/Llama-2-13b-chat-hf", "Lake 2 Advanced", "hidden"),
218
- ("deepseek-ai/deepseek-llm-67b-chat", "Lake 2 Pro [Planned]", "shown")
219
- ]
220
-
221
- # Model information
222
- model_info = {
223
- "Lake 1 Base": {
224
- "description": "Balanced model offering good performance across tasks",
225
- "parameters": "7B",
226
- "training_data": "BI Corp specialized corpus",
227
- "developer": "BI Corp",
228
- "best_for": "General purpose conversations",
229
- "architecture": "Sparse Mixture of Experts",
230
- "context_window": "32768 tokens"
231
- },
232
- "Lake 1 Advanced": {
233
- "description": "Enhanced reasoning capabilities with 3B parameters",
234
- "parameters": "3B",
235
- "training_data": "BI Corp training corpus",
236
- "developer": "BI Corp",
237
- "best_for": "Complex problem solving",
238
- "architecture": "Dense Transformer",
239
- "context_window": "8192 tokens"
240
- },
241
- "Lake 2 Chat [Closed Alpha]": {
242
- "description": "Legacy chat-optimized model (Llama 2 hybrided architecture)",
243
- "parameters": "7B",
244
- "training_data": "Public conversations dataset",
245
- "developer": "BI Corp",
246
- "best_for": "Traditional chat applications",
247
- "architecture": "Llama 2 Transformer",
248
- "context_window": "4096 tokens"
249
- },
250
- "Lake 2 Base [Closed Beta]": {
251
- "description": "State-of-the-art 70B parameter model",
252
- "parameters": "70B",
253
- "training_data": "Multi-domain expert data",
254
- "developer": "BI Corp",
255
- "best_for": "Research & advanced applications",
256
- "architecture": "Mixture of Experts",
257
- "context_window": "16384 tokens"
258
- },
259
- "Lake 2 Advanced": {
260
- "description": "Enhanced performance model with advanced reasoning capabilities.",
261
- "parameters": "13B",
262
- "training_data": "Diverse datasets including tech, literature, and general knowledge.",
263
- "developer": "BI Corp",
264
- "best_for": "Handling large-scale queries and in-depth topics.",
265
- "architecture": "Transformer-based with specialized learning capabilities.",
266
- "context_window": "16384 tokens"
267
- },
268
- "Lake 2 Pro [Planned]": {
269
- "description": "Pro-level model with deep learning architecture for high-end applications.",
270
- "parameters": "67B",
271
- "training_data": "Expert-level data across multiple industries.",
272
- "developer": "BI Corp",
273
- "best_for": "High-performance computing and enterprise-level tasks.",
274
- "architecture": "Hybrid architecture leveraging the latest advances in deep learning.",
275
- "context_window": "32768 tokens"
276
- }
277
- }
278
-
279
- def get_model_info(model_name: str) -> str:
280
- """Generate formatted model information Markdown"""
281
- info = model_info.get(model_name, {})
282
- return f"""
283
- ## 📋 {model_name} Specifications
284
-
285
- **Description**: {info.get('description', 'N/A')}
286
- **Parameters**: {info.get('parameters', 'N/A')}
287
- **Architecture**: {info.get('architecture', 'N/A')}
288
- **Context Window**: {info.get('context_window', 'N/A')}
289
- **Training Data**: {info.get('training_data', 'N/A')}
290
- **Developer**: {info.get('developer', 'N/A')}
291
- **Best For**: {info.get('best_for', 'N/A')}
292
- """
293
 
294
- def generate_response(message: str, model_name: str, preset: str, user_name: str = "User") -> str:
295
- """Generate AI response without explicit user/assistant labels and with placeholder fixed."""
296
- client = clients[model_name]
297
- params = presets[model_name][preset]
298
-
299
- # Replace the {name} placeholder in the system message with the provided user name.
300
- system_msg = system_messages[model_name].format(name=user_name)
301
- prompt = f"\n main: {system_msg}\n\n\n{message}\n"
302
-
303
- return client.text_generation(
304
- prompt=prompt,
305
- max_new_tokens=params["max_new_tokens"],
306
- temperature=params["temperature"],
307
- top_p=params["top_p"]
308
- )
309
 
310
- def handle_chat(message: str, history: list, model: str, preset: str) -> str:
311
- """Handle chat interface with error handling"""
312
  try:
313
- model_entry = next(m for m in model_registry if m[1] == model)
314
- if model_entry[2] != "released":
315
- return f"⚠️ {model} is not available for public use"
316
-
317
- # In this example, we use the default user name "User".
318
- return generate_response(message, model_entry[0], preset, user_name="User")
319
-
320
- except StopIteration:
321
- return "🔍 Error: Selected model not found"
322
- except KeyError as e:
323
- return f"🔑 Error: Invalid configuration - {str(e)}"
 
 
 
 
324
  except Exception as e:
325
- return f"⚠️ Error: {str(e)}"
326
 
327
  # Create Gradio interface
328
- with gr.Blocks(title="BI Corp AI Assistant", theme="soft") as demo:
329
- gr.Markdown("# <center>🏔️ BI Corp AI Assistant</center>")
330
- gr.Markdown("### <center>Enterprise-Grade AI Solutions</center>")
331
-
332
- with gr.Row():
333
- with gr.Column(scale=1):
334
- model_dropdown = gr.Dropdown(
335
- label="🤖 Model Selection",
336
- choices=[m[1] for m in model_registry if m[2] in ("released", "shown")],
337
- value="Lake 1 Flash",
338
- interactive=True
339
- )
340
- preset_dropdown = gr.Dropdown(
341
- label="⚙️ Performance Preset",
342
- choices=["Fast", "Normal", "Quality", "Unreal Performance"],
343
- value="Fast",
344
- interactive=True
345
- )
346
- model_info_md = gr.Markdown(
347
- value=get_model_info("Lake 1 Flash"),
348
- label="📝 Model Specifications"
349
- )
350
-
351
- with gr.Column(scale=3):
352
- chat_interface = gr.ChatInterface(
353
- fn=handle_chat,
354
- additional_inputs=[model_dropdown, preset_dropdown],
355
- examples=[
356
- ["Explain quantum computing", "Lake 1 Base", "Normal"],
357
- ["Write a poem about AI", "Lake 1 Advanced", "Quality"],
358
- ["Compare blockchain databases", "Lake 2 Base [Closed Beta]", "Unreal Performance"]
359
- ],
360
- chatbot=gr.Chatbot(
361
- height=600,
362
- label="💬 Conversation",
363
- show_copy_button=True
364
- ),
365
- textbox=gr.Textbox(
366
- placeholder="Type your message...",
367
- container=False,
368
- scale=7,
369
- autofocus=True
370
- ),
371
- submit_btn=gr.Button("🚀 Send", variant="primary")
372
- )
373
-
374
- # Add separate clear button
375
- clear_button = gr.Button("🧹 Clear History")
376
- clear_button.click(
377
- fn=lambda: None,
378
- inputs=[],
379
- outputs=chat_interface.chatbot,
380
- queue=False
381
- )
382
-
383
- model_dropdown.change(
384
- fn=get_model_info,
385
- inputs=model_dropdown,
386
- outputs=model_info_md,
387
- queue=False
388
- )
389
-
390
  if __name__ == "__main__":
391
- demo.launch(server_port=7860)
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
 
5
+ # Load model and tokenizer locally
6
+ MODEL_NAME = "gpt2" # Replace with your local model directory if different
 
7
 
8
+ def load_model():
9
+ try:
10
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
11
+ model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
12
+ return tokenizer, model
13
+ except Exception as e:
14
+ raise RuntimeError(f"Error loading model: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
+ tokenizer, model = load_model()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ def generate_text(prompt, max_length=100, temperature=0.9):
 
19
  try:
20
+ # Encode the input prompt
21
+ inputs = tokenizer.encode(prompt, return_tensors='pt')
22
+
23
+ # Generate output
24
+ outputs = model.generate(
25
+ inputs,
26
+ max_length=max_length,
27
+ temperature=temperature,
28
+ pad_token_id=tokenizer.eos_token_id,
29
+ do_sample=True
30
+ )
31
+
32
+ # Decode and return the generated text
33
+ generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
34
+ return generated_text
35
  except Exception as e:
36
+ return f"Error generating text: {str(e)}"
37
 
38
  # Create Gradio interface
39
+ interface = gr.Interface(
40
+ fn=generate_text,
41
+ inputs=[
42
+ gr.Textbox(label="Input Prompt", lines=3),
43
+ gr.Slider(50, 500, value=100, label="Max Length"),
44
+ gr.Slider(0.1, 2.0, value=0.9, label="Temperature")
45
+ ],
46
+ outputs=gr.Textbox(label="Generated Text", lines=5),
47
+ title="Local Model Demo - Text Generation",
48
+ description="A locally loaded GPT-2 model for text generation",
49
+ examples=[
50
+ ["Once upon a time, in a land far away,"],
51
+ ["The future of artificial intelligence", 150, 0.7],
52
+ ["In a world where robots rule,", 200, 1.2]
53
+ ]
54
+ )
55
+
56
+ # Run the app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  if __name__ == "__main__":
58
+ interface.launch(server_name="0.0.0.0", server_port=7860)