BICORP commited on
Commit
2d6bec8
·
verified ·
1 Parent(s): 2229b59

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -108
app.py CHANGED
@@ -1,108 +1,124 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- # Default clients for each model
5
- clients = {
6
- "mistralai/Mistral-7B-Instruct-v0.3": InferenceClient("mistralai/Mistral-7B-Instruct-v0.3"),
7
- "BICORP/Lake-1-Advanced": InferenceClient("BICORP/Lake-1-Advanced")
8
- }
9
-
10
- # Define presets for each model
11
- presets = {
12
- "mistralai/Mistral-7B-Instruct-v0.3": {
13
- "Fast": {"max_tokens": 256, "temperature": 1.0, "top_p": 0.8},
14
- "Normal": {"max_tokens": 512, "temperature": 0.6, "top_p": 0.75},
15
- "Quality": {"max_tokens": 1024, "temperature": 0.45, "top_p": 0.60},
16
- "Unreal Performance": {"max_tokens": 1048, "temperature": 0.5, "top_p": 0.7},
17
- },
18
- "BICORP/Lake-1-Advanced": {
19
- "Fast": {"max_tokens": 800, "temperature": 1.0, "top_p": 0.9},
20
- "Normal": {"max_tokens": 4000, "temperature": 0.7, "top_p": 0.95},
21
- "Quality": {"max_tokens": 32000, "temperature": 0.5, "top_p": 0.90},
22
- "Unreal Performance": {"max_tokens": 128000, "temperature": 0.6, "top_p": 0.75},
23
- }
24
- }
25
-
26
- # System messages for each model
27
- system_messages = {
28
- "mistralai/Mistral-7B-Instruct-v0.3": "Your name is Lake 1 Base but mine is User",
29
- "BICORP/Lake-1-Advanced": "Your name is Lake 1 Advanced [Alpha] but mine is User or what i will type as my name"
30
- }
31
-
32
- # Model names and their pseudonyms
33
- model_choices = [
34
- ("mistralai/Mistral-7B-Instruct-v0.3", "Lake 1 Base"),
35
- ("BICORP/Lake-1-Advanced", "Lake 1 Advanced [Alpha]")
36
- ]
37
-
38
- # Convert pseudonyms to model names for the dropdown
39
- pseudonyms = [model[1] for model in model_choices]
40
-
41
- def respond(
42
- message,
43
- history: list,
44
- model_name,
45
- preset_name
46
- ):
47
- # Get the correct client
48
- client = clients[model_name]
49
-
50
- # Get the system message for the model
51
- system_message = system_messages[model_name]
52
-
53
- messages = [{"role": "system", "content": system_message}]
54
-
55
- # Ensure history is a list of dictionaries
56
- for val in history:
57
- if isinstance(val, dict) and 'role' in val and 'content' in val:
58
- messages.append({"role": val['role'], "content": val['content']})
59
-
60
- messages.append({"role": "user", "content": message})
61
-
62
- # Get the preset settings
63
- preset = presets[model_name][preset_name]
64
- max_tokens = preset["max_tokens"]
65
- temperature = preset["temperature"]
66
- top_p = preset["top_p"]
67
-
68
- # Get the response from the model
69
- response = client.chat_completion(
70
- messages,
71
- max_tokens=max_tokens,
72
- temperature=temperature,
73
- top_p=top_p,
74
- )
75
-
76
- # Extract the content from the response
77
- final_response = response.choices[0].message['content']
78
-
79
- return final_response
80
-
81
- def respond_with_pseudonym(
82
- message,
83
- history: list,
84
- selected_model,
85
- selected_preset
86
- ):
87
- # Find the actual model name from the pseudonym
88
- try:
89
- model_name = next(model[0] for model in model_choices if model[1] == selected_model)
90
- except StopIteration:
91
- return f"Error: The selected model '{selected_model}' is not valid. Please select a valid model."
92
-
93
- # Call the existing respond function
94
- response = respond(message, history, model_name, selected_preset)
95
-
96
- return response
97
-
98
- # Gradio Chat Interface
99
- demo = gr.ChatInterface(
100
- fn=respond_with_pseudonym,
101
- additional_inputs=[
102
- gr.Dropdown(choices=pseudonyms, label="Select Model", value=pseudonyms[0]),
103
- gr.Dropdown(choices=list(presets[model_choices[0][0]].keys()), label="Select Preset", value="Fast")
104
- ],
105
- )
106
-
107
- if __name__ == "__main__":
108
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
+
5
+ # Local model loading
6
+ models = {
7
+ "mistralai/Mistral-7B-Instruct-v0.3": AutoModelForCausalLM.from_pretrained(
8
+ "mistralai/Mistral-7B-Instruct-v0.3",
9
+ device_map="auto",
10
+ torch_dtype=torch.bfloat16
11
+ ),
12
+ "BICORP/Lake-1-Advanced": AutoModelForCausalLM.from_pretrained(
13
+ "BICORP/Lake-1-Advanced",
14
+ device_map="auto",
15
+ torch_dtype=torch.bfloat16
16
+ )
17
+ }
18
+
19
+ tokenizers = {
20
+ "mistralai/Mistral-7B-Instruct-v0.3": AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3"),
21
+ "BICORP/Lake-1-Advanced": AutoTokenizer.from_pretrained("BICORP/Lake-1-Advanced")
22
+ }
23
+
24
+ # Define presets (updated parameter names for local inference)
25
+ presets = {
26
+ "mistralai/Mistral-7B-Instruct-v0.3": {
27
+ "Fast": {"max_new_tokens": 256, "temperature": 1.0, "top_p": 0.8},
28
+ "Normal": {"max_new_tokens": 512, "temperature": 0.6, "top_p": 0.75},
29
+ "Quality": {"max_new_tokens": 1024, "temperature": 0.45, "top_p": 0.60},
30
+ "Unreal Performance": {"max_new_tokens": 1048, "temperature": 0.5, "top_p": 0.7},
31
+ },
32
+ "BICORP/Lake-1-Advanced": {
33
+ "Fast": {"max_new_tokens": 800, "temperature": 1.0, "top_p": 0.9},
34
+ "Normal": {"max_new_tokens": 4000, "temperature": 0.7, "top_p": 0.95},
35
+ "Quality": {"max_new_tokens": 32000, "temperature": 0.5, "top_p": 0.90},
36
+ "Unreal Performance": {"max_new_tokens": 128000, "temperature": 0.6, "top_p": 0.75},
37
+ }
38
+ }
39
+
40
+ # System messages and model choices remain the same
41
+ system_messages = {
42
+ "mistralai/Mistral-7B-Instruct-v0.3": "Your name is Lake 1 Base but mine is User",
43
+ "BICORP/Lake-1-Advanced": "Your name is Lake 1 Advanced [Alpha] but mine is User or what I will type as my name"
44
+ }
45
+
46
+ model_choices = [
47
+ ("mistralai/Mistral-7B-Instruct-v0.3", "Lake 1 Base"),
48
+ ("BICORP/Lake-1-Advanced", "Lake 1 Advanced [Alpha]")
49
+ ]
50
+
51
+ pseudonyms = [model[1] for model in model_choices]
52
+
53
+ def respond(
54
+ message,
55
+ history: list,
56
+ model_name,
57
+ preset_name
58
+ ):
59
+ # Get the correct model and tokenizer
60
+ model = models[model_name]
61
+ tokenizer = tokenizers[model_name]
62
+
63
+ # Get the system message for the model
64
+ system_message = system_messages[model_name]
65
+
66
+ messages = [{"role": "system", "content": system_message}]
67
+
68
+ # Ensure history is a list of dictionaries
69
+ for val in history:
70
+ if isinstance(val, dict) and 'role' in val and 'content' in val:
71
+ messages.append({"role": val['role'], "content": val['content']})
72
+
73
+ messages.append({"role": "user", "content": message})
74
+
75
+ # Get the preset settings
76
+ preset = presets[model_name][preset_name]
77
+ max_new_tokens = preset["max_new_tokens"]
78
+ temperature = preset["temperature"]
79
+ top_p = preset["top_p"]
80
+
81
+ # Prepare input for the model
82
+ input_ids = tokenizer.encode(tokenizer.chat_template(messages), return_tensors="pt").to(model.device)
83
+
84
+ # Get the response from the model
85
+ outputs = model.generate(
86
+ input_ids,
87
+ max_new_tokens=max_new_tokens,
88
+ temperature=temperature,
89
+ top_p=top_p,
90
+ )
91
+
92
+ # Extract the content from the response
93
+ final_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
94
+
95
+ return final_response
96
+
97
+ def respond_with_pseudonym(
98
+ message,
99
+ history: list,
100
+ selected_model,
101
+ selected_preset
102
+ ):
103
+ # Find the actual model name from the pseudonym
104
+ try:
105
+ model_name = next(model[0] for model in model_choices if model[1] == selected_model)
106
+ except StopIteration:
107
+ return f"Error: The selected model '{selected_model}' is not valid. Please select a valid model."
108
+
109
+ # Call the existing respond function
110
+ response = respond(message, history, model_name, selected_preset)
111
+
112
+ return response
113
+
114
+ # Gradio Chat Interface
115
+ demo = gr.ChatInterface(
116
+ fn=respond_with_pseudonym,
117
+ additional_inputs=[
118
+ gr.Dropdown(choices=pseudonyms, label="Select Model", value=pseudonyms[0]),
119
+ gr.Dropdown(choices=list(presets[model_choices[0][0]].keys()), label="Select Preset", value="Fast")
120
+ ],
121
+ )
122
+
123
+ if __name__ == "__main__":
124
+ demo.launch()