Update app.py
Browse files
app.py
CHANGED
|
@@ -1,108 +1,124 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
"
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
"
|
| 13 |
-
"
|
| 14 |
-
"
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 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 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
)
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|