BICORP commited on
Commit
2229b59
·
verified ·
1 Parent(s): 94c6e6c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()