BICORP commited on
Commit
52d8843
·
verified ·
1 Parent(s): 096fd04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -84
app.py CHANGED
@@ -1,39 +1,26 @@
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
- import torch
4
  import os
 
5
 
6
- # Get your Hugging Face token (set this in your environment variables or replace directly)
7
- hf_token = os.getenv("HF_TOKEN") # Ensure this variable is set correctly
8
-
9
- # Local model loading configuration
10
- models = {
11
- "mistralai/Mistral-7B-Instruct-v0.3": AutoModelForCausalLM.from_pretrained(
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
 
36
- # Model generation presets
 
 
 
 
 
 
 
 
37
  presets = {
38
  "mistralai/Mistral-7B-Instruct-v0.3": {
39
  "Fast": {"max_new_tokens": 256, "temperature": 1.0, "top_p": 0.8},
@@ -49,79 +36,79 @@ presets = {
49
  }
50
  }
51
 
52
- # System prompts configuration
53
  system_messages = {
54
  "mistralai/Mistral-7B-Instruct-v0.3": "Your name is Lake 1 Base but mine is User",
55
  "BICORP/Lake-1-Advanced": "Your name is Lake 1 Advanced [Alpha] but mine is User or what I will type as my name"
56
  }
57
 
58
- # Model selection options
59
  model_choices = [
60
  ("mistralai/Mistral-7B-Instruct-v0.3", "Lake 1 Base"),
61
  ("BICORP/Lake-1-Advanced", "Lake 1 Advanced [Alpha]")
62
  ]
 
 
63
  pseudonyms = [model[1] for model in model_choices]
64
 
65
  def respond(message, history: list, model_name, preset_name):
66
- # Get the correct model and tokenizer
 
 
67
  model = models[model_name]
68
  tokenizer = tokenizers[model_name]
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
76
- with torch.no_grad():
77
- output = model.generate(
78
- inputs,
79
- max_new_tokens=preset["max_new_tokens"],
80
- temperature=preset["temperature"],
81
- top_p=preset["top_p"]
82
- )
83
-
84
- # Decode the output
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()
 
1
  import gradio as gr
 
 
2
  import os
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
 
5
+ # Set paths for local model storage
6
+ cache_dir = "./cache" # Specify your cache directory within the Space
7
+ os.makedirs(cache_dir, exist_ok=True) # Create cache directory if it doesn't exist
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ # Load models and tokenizers locally (or download if not available)
10
+ model_paths = {
11
+ "mistralai/Mistral-7B-Instruct-v0.3": os.path.join(cache_dir, "mistral-7b-instruct"),
12
+ "BICORP/Lake-1-Advanced": os.path.join(cache_dir, "lake-1-advanced")
 
 
 
 
 
13
  }
14
 
15
+ models = {}
16
+ tokenizers = {}
17
+
18
+ # Load models and tokenizers from specified local paths or download
19
+ for model_name, path in model_paths.items():
20
+ models[model_name] = AutoModelForCausalLM.from_pretrained(model_name, cache_dir=path)
21
+ tokenizers[model_name] = AutoTokenizer.from_pretrained(model_name, cache_dir=path)
22
+
23
+ # Define presets for each model
24
  presets = {
25
  "mistralai/Mistral-7B-Instruct-v0.3": {
26
  "Fast": {"max_new_tokens": 256, "temperature": 1.0, "top_p": 0.8},
 
36
  }
37
  }
38
 
39
+ # System messages for each model
40
  system_messages = {
41
  "mistralai/Mistral-7B-Instruct-v0.3": "Your name is Lake 1 Base but mine is User",
42
  "BICORP/Lake-1-Advanced": "Your name is Lake 1 Advanced [Alpha] but mine is User or what I will type as my name"
43
  }
44
 
45
+ # Model names and their pseudonyms
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
+ # Extract pseudonyms for the dropdown
52
  pseudonyms = [model[1] for model in model_choices]
53
 
54
  def respond(message, history: list, model_name, preset_name):
55
+ """
56
+ Generate a response from the selected model based on the user's message and chat history.
57
+ """
58
  model = models[model_name]
59
  tokenizer = tokenizers[model_name]
60
+ system_message = system_messages[model_name]
61
+
62
+ messages = [{"role": "system", "content": system_message}]
63
+
64
+ for val in history:
65
+ if isinstance(val, dict) and 'role' in val and 'content' in val:
66
+ messages.append({"role": val['role'], "content": val['content']})
67
+
68
+ messages.append({"role": "user", "content": message})
69
 
70
  # Prepare the input for the model
71
+ inputs = tokenizer([messages], return_tensors="pt", padding=True, truncation=True)
72
+
73
+ # Get the preset settings
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  preset = presets[model_name][preset_name]
75
+ max_new_tokens = preset["max_new_tokens"]
76
+ temperature = preset["temperature"]
77
+ top_p = preset["top_p"]
78
+
79
+ # Generate the response from the model
80
+ response = model.generate(
81
+ input_ids=inputs['input_ids'],
82
+ attention_mask=inputs['attention_mask'],
83
+ max_new_tokens=max_new_tokens,
84
+ temperature=temperature,
85
+ top_p=top_p,
86
+ )
87
+
88
+ # Decode the generated response
89
+ final_response = tokenizer.decode(response[0], skip_special_tokens=True)
90
+
91
+ return final_response
92
+
93
+ def respond_with_pseudonym(message, history: list, selected_model, selected_preset):
94
+ """
95
+ Handle the user's message and determine which model to use based on the selected pseudonym.
96
+ """
97
+ try:
98
+ model_name = next(model[0] for model in model_choices if model[1] == selected_model)
99
+ except StopIteration:
100
+ return f"Error: The selected model '{selected_model}' is not valid. Please select a valid model."
101
+
102
+ return respond(message, history, model_name, selected_preset)
103
 
104
+ # Gradio Chat Interface
105
+ demo = gr.ChatInterface(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  fn=respond_with_pseudonym,
107
+ additional_inputs=[
108
+ gr.Dropdown(choices=pseudonyms, label="Select Model", value=pseudonyms[0]),
109
+ gr.Dropdown(choices=list(presets[model_choices[0][0]].keys()), label="Select Preset", value="Fast")
 
 
 
110
  ],
 
 
 
111
  )
112
 
113
+ if __name__ == "__main__":
114
+ demo.launch()