BICORP commited on
Commit
85af627
·
verified ·
1 Parent(s): 5a665d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -35
app.py CHANGED
@@ -12,28 +12,24 @@ models = {
12
  "mistralai/Mistral-7B-Instruct-v0.3",
13
  device_map="auto",
14
  torch_dtype=torch.bfloat16, # Use bfloat16
15
- use_auth_token=True,
16
- token=hf_token # Add authentication here
17
  ),
18
  "BICORP/Lake-1-Advanced": AutoModelForCausalLM.from_pretrained(
19
  "BICORP/Lake-1-Advanced",
20
  device_map="auto",
21
  torch_dtype=torch.bfloat16, # Use bfloat16
22
- use_auth_token=True,
23
- token=hf_token # Add authentication here
24
  )
25
  }
26
 
27
  tokenizers = {
28
  "mistralai/Mistral-7B-Instruct-v0.3": AutoTokenizer.from_pretrained(
29
  "mistralai/Mistral-7B-Instruct-v0.3",
30
- use_auth_token=True,
31
- token=hf_token # Add authentication here
32
  ),
33
  "BICORP/Lake-1-Advanced": AutoTokenizer.from_pretrained(
34
  "BICORP/Lake-1-Advanced",
35
- use_auth_token=True,
36
- token=hf_token # Add authentication here
37
  )
38
  }
39
 
@@ -73,7 +69,7 @@ def respond(message, history: list, model_name, preset_name):
73
  preset = presets[model_name][preset_name]
74
 
75
  # Prepare the input for the model
76
- input_text = f"{system_messages[model_name]}\n:User {message}\nAI:"
77
  inputs = tokenizer.encode(input_text, return_tensors="pt").to(model.device)
78
 
79
  # Generate response
@@ -89,33 +85,43 @@ def respond(message, history: list, model_name, preset_name):
89
  response = tokenizer.decode(output[0], skip_special_tokens=True)
90
  return response.split("AI:")[-1].strip() # Extract the AI's response
91
 
92
- def respond_with_pseudonym(message, history, selected_model, selected_preset):
93
- try:
94
- model_id = next(model[0] for model in model_choices if model[1] == selected_model)
95
- except StopIteration:
96
- return "Model not found."
97
-
98
- response = respond(message, history, model_id, selected_preset)
99
- history.append((message, response))
100
- return history
101
-
102
- # Gradio interface setup
103
- def launch_interface():
104
- with gr.Blocks() as demo:
105
- gr.Markdown("## Chat with AI Models")
106
- with gr.Row():
107
- model_dropdown = gr.Dropdown(choices=pseudonyms, label="Select Model")
108
- preset_dropdown = gr.Dropdown(choices=["Fast", "Normal", "Quality", "Unreal Performance"], label="Select Preset")
109
- chatbot = gr.Chatbot()
110
- message_input = gr.Textbox(placeholder="Type your message here...")
111
- submit_button = gr.Button("Send")
112
 
113
- def submit_message(message, history, selected_model, selected_preset):
114
- return respond_with_pseudonym(message, history, selected_model, selected_preset)
 
115
 
116
- submit_button.click(submit_message, inputs=[message_input, chatbot, model_dropdown, preset_dropdown], outputs=chatbot)
 
 
 
 
 
 
 
117
 
118
- demo.launch(share=True)
 
 
119
 
120
- if __name__ == "__main__":
121
- launch_interface()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  "mistralai/Mistral-7B-Instruct-v0.3",
13
  device_map="auto",
14
  torch_dtype=torch.bfloat16, # Use bfloat16
15
+ token=hf_token # Use token for authentication
 
16
  ),
17
  "BICORP/Lake-1-Advanced": AutoModelForCausalLM.from_pretrained(
18
  "BICORP/Lake-1-Advanced",
19
  device_map="auto",
20
  torch_dtype=torch.bfloat16, # Use bfloat16
21
+ token=hf_token # Use token for authentication
 
22
  )
23
  }
24
 
25
  tokenizers = {
26
  "mistralai/Mistral-7B-Instruct-v0.3": AutoTokenizer.from_pretrained(
27
  "mistralai/Mistral-7B-Instruct-v0.3",
28
+ token=hf_token # Use token for authentication
 
29
  ),
30
  "BICORP/Lake-1-Advanced": AutoTokenizer.from_pretrained(
31
  "BICORP/Lake-1-Advanced",
32
+ token=hf_token # Use token for authentication
 
33
  )
34
  }
35
 
 
69
  preset = presets[model_name][preset_name]
70
 
71
  # Prepare the input for the model
72
+ input_text = f"{system_messages[model_name]}\n:User {message}\nAI:"
73
  inputs = tokenizer.encode(input_text, return_tensors="pt").to(model.device)
74
 
75
  # Generate response
 
85
  response = tokenizer.decode(output[0], skip_special_tokens=True)
86
  return response.split("AI:")[-1].strip() # Extract the AI's response
87
 
88
+ def respond_with_pseudonym(message, history: list, model_name, preset_name, pseudonym):
89
+ # Get the correct model and tokenizer
90
+ model = models[model_name]
91
+ tokenizer = tokenizers[model_name]
92
+ preset = presets[model_name][preset_name]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
+ # Prepare the input for the model with pseudonym
95
+ input_text = f"{system_messages[model_name]}\n:{pseudonym} {message}\nAI:"
96
+ inputs = tokenizer.encode(input_text, return_tensors="pt").to(model.device)
97
 
98
+ # Generate response
99
+ with torch.no_grad():
100
+ output = model.generate(
101
+ inputs,
102
+ max_new_tokens=preset["max_new_tokens"],
103
+ temperature=preset["temperature"],
104
+ top_p=preset["top_p"]
105
+ )
106
 
107
+ # Decode the output
108
+ response = tokenizer.decode(output[0], skip_special_tokens=True)
109
+ return response.split("AI:")[-1].strip() # Extract the AI's response
110
 
111
+ # Gradio interface setup
112
+ iface = gr.Interface(
113
+ fn=respond_with_pseudonym,
114
+ inputs=[
115
+ gr.inputs.Textbox(label="Message"),
116
+ gr.inputs.State(),
117
+ gr.inputs.Dropdown(choices=pseudonyms, label="Model"),
118
+ gr.inputs.Dropdown(choices=["Fast", "Normal", "Quality", "Unreal Performance"], label="Preset"),
119
+ gr.inputs.Textbox(label="Pseudonym", default="User ")
120
+ ],
121
+ outputs="text",
122
+ title="AI Chatbot",
123
+ description="Chat with AI models using your chosen pseudonym."
124
+ )
125
+
126
+ # Launch the Gradio app
127
+ iface.launch() launch_interface()