BICORP commited on
Commit
ee5c0a0
·
verified ·
1 Parent(s): e27b980

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -28
app.py CHANGED
@@ -8,26 +8,44 @@ client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
8
  def switch_client(model_name: str):
9
  return InferenceClient(model_name)
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  def respond(
12
  message,
13
- history: list[dict],
14
- system_message,
15
- max_tokens,
16
- temperature,
17
- top_p,
18
- model_name
19
  ):
20
  # Switch client based on model selection
21
  global client
22
  client = switch_client(model_name)
23
 
24
- messages = [{"role": "system", "content": system_message}]
25
 
 
26
  for val in history:
27
- messages.append({"role": val['role'], "content": val['content']})
 
28
 
29
  messages.append({"role": "user", "content": message})
30
 
 
 
 
 
 
 
31
  # Get the response from the model
32
  response = client.chat_completion(
33
  messages,
@@ -52,37 +70,24 @@ pseudonyms = [model[1] for model in model_choices]
52
  # Function to handle model selection and pseudonyms
53
  def respond_with_pseudonym(
54
  message,
55
- history: list[dict],
56
- system_message,
57
- max_tokens,
58
- temperature,
59
- top_p,
60
- selected_pseudonym
61
  ):
62
  # Find the actual model name from the pseudonym
63
  model_name = next(model[0] for model in model_choices if model[1] == selected_pseudonym)
64
 
65
  # Call the existing respond function
66
- response = respond(message, history, system_message, max_tokens, temperature, top_p, model_name)
67
 
68
- # No longer adding the pseudonym at the end of the response
69
  return response
70
 
71
  # Gradio Chat Interface
72
  demo = gr.ChatInterface(
73
- respond_with_pseudonym,
74
  additional_inputs=[
75
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
76
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
77
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
78
- gr.Slider(
79
- minimum=0.1,
80
- maximum=1.0,
81
- value=0.95,
82
- step=0.05,
83
- label="Top-p (nucleus sampling)",
84
- ),
85
- gr.Dropdown(pseudonyms, label="Select Model", value=pseudonyms[0]) # Pseudonym selection dropdown
86
  ],
87
  )
88
 
 
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 = "Lake 1 Base"
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,
 
70
  # Function to handle model selection and pseudonyms
71
  def respond_with_pseudonym(
72
  message,
73
+ history: list,
74
+ selected_pseudonym,
75
+ selected_preset
 
 
 
76
  ):
77
  # Find the actual model name from the pseudonym
78
  model_name = next(model[0] for model in model_choices if model[1] == selected_pseudonym)
79
 
80
  # Call the existing respond function
81
+ response = respond(message, history, model_name, selected_preset)
82
 
 
83
  return response
84
 
85
  # Gradio Chat Interface
86
  demo = gr.ChatInterface(
87
+ fn=respond_with_pseudonym,
88
  additional_inputs=[
89
+ gr.Dropdown(choices=list(presets["mistralai/Mistral-7B-Instruct-v0.3"].keys()), label="Select Preset", value="Fast"), # Preset selection dropdown
90
+ gr.Dropdown(choices=pseudonyms, label="Select Model", value=pseudonyms[0]) # Pseudonym selection dropdown
 
 
 
 
 
 
 
 
 
91
  ],
92
  )
93