Spaces:
Running
on
Zero
Running
on
Zero
Commit
·
4d0b859
1
Parent(s):
7396944
Issues with ZeroGPU
Browse files
app.py
CHANGED
|
@@ -1,9 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from mosaic import Mosaic
|
| 3 |
import spaces
|
| 4 |
import traceback
|
| 5 |
-
from transformers import AutoModelForCausalLM
|
| 6 |
-
import torch
|
| 7 |
|
| 8 |
# Maximum number of model textboxes
|
| 9 |
MAX_MODELS = 10
|
|
@@ -28,9 +26,7 @@ def update_textboxes(n_visible):
|
|
| 28 |
if n_visible < MAX_MODELS:
|
| 29 |
n_visible += 1
|
| 30 |
tb_updates = [gr.update(visible=(i < n_visible)) for i in range(MAX_MODELS)]
|
| 31 |
-
|
| 32 |
-
status_updates = [gr.update(visible=(i < n_visible)) for i in range(MAX_MODELS)]
|
| 33 |
-
return (n_visible, *tb_updates, *btn_updates, *status_updates)
|
| 34 |
|
| 35 |
# Decrease model slots and clear removed entries
|
| 36 |
def remove_textboxes(n_visible):
|
|
@@ -41,17 +37,13 @@ def remove_textboxes(n_visible):
|
|
| 41 |
# Remove cached models for slots now hidden
|
| 42 |
for idx in range(new, old):
|
| 43 |
LOADED_MODELS.pop(idx+1, None)
|
| 44 |
-
tb_updates
|
| 45 |
for i in range(MAX_MODELS):
|
| 46 |
if i < n_visible:
|
| 47 |
tb_updates.append(gr.update(visible=True))
|
| 48 |
-
btn_updates.append(gr.update(visible=True))
|
| 49 |
-
status_updates.append(gr.update(visible=True))
|
| 50 |
else:
|
| 51 |
tb_updates.append(gr.update(visible=False, value=""))
|
| 52 |
-
|
| 53 |
-
status_updates.append(gr.update(visible=False, value="Not loaded"))
|
| 54 |
-
return (n_visible, *tb_updates, *btn_updates, *status_updates)
|
| 55 |
|
| 56 |
def apply_config1():
|
| 57 |
"""
|
|
@@ -61,23 +53,19 @@ def apply_config1():
|
|
| 61 |
- new visibility for each Load button & status box
|
| 62 |
"""
|
| 63 |
n_vis = len(GPT_CONFIG_MODELS)
|
| 64 |
-
tb_updates
|
| 65 |
|
| 66 |
for i in range(MAX_MODELS):
|
| 67 |
if i < n_vis:
|
| 68 |
# show this slot, set its value from CONFIG_MODELS
|
| 69 |
tb_updates.append(gr.update(visible=True, value=GPT_CONFIG_MODELS[i]))
|
| 70 |
-
btn_updates.append(gr.update(visible=True))
|
| 71 |
-
status_updates.append(gr.update(visible=True, value="Not loaded"))
|
| 72 |
else:
|
| 73 |
# hide all others
|
| 74 |
tb_updates.append(gr.update(visible=False, value=""))
|
| 75 |
-
btn_updates.append(gr.update(visible=False))
|
| 76 |
-
status_updates.append(gr.update(visible=False, value="Not loaded"))
|
| 77 |
|
| 78 |
# Return in the same shape as your update_textboxes/remove_textboxes:
|
| 79 |
# (n_models_state, *all textboxes, *all load buttons, *all status boxes)
|
| 80 |
-
return (n_vis, *tb_updates
|
| 81 |
|
| 82 |
def apply_config2():
|
| 83 |
"""
|
|
@@ -87,53 +75,20 @@ def apply_config2():
|
|
| 87 |
- new visibility for each Load button & status box
|
| 88 |
"""
|
| 89 |
n_vis = len(Falcon_CONFIG_MODELS)
|
| 90 |
-
tb_updates
|
| 91 |
|
| 92 |
for i in range(MAX_MODELS):
|
| 93 |
if i < n_vis:
|
| 94 |
# show this slot, set its value from CONFIG_MODELS
|
| 95 |
tb_updates.append(gr.update(visible=True, value=Falcon_CONFIG_MODELS[i]))
|
| 96 |
-
btn_updates.append(gr.update(visible=True))
|
| 97 |
-
status_updates.append(gr.update(visible=True, value="Not loaded"))
|
| 98 |
else:
|
| 99 |
# hide all others
|
| 100 |
tb_updates.append(gr.update(visible=False, value=""))
|
| 101 |
-
btn_updates.append(gr.update(visible=False))
|
| 102 |
-
status_updates.append(gr.update(visible=False, value="Not loaded"))
|
| 103 |
|
| 104 |
# Return in the same shape as your update_textboxes/remove_textboxes:
|
| 105 |
# (n_models_state, *all textboxes, *all load buttons, *all status boxes)
|
| 106 |
-
return (n_vis, *tb_updates
|
| 107 |
|
| 108 |
-
# Load a single model and report status
|
| 109 |
-
@spaces.GPU()
|
| 110 |
-
def load_single_model(model_path, use_bfloat16=True):
|
| 111 |
-
try:
|
| 112 |
-
repo = model_path
|
| 113 |
-
if not repo:
|
| 114 |
-
return "Error: No path provided"
|
| 115 |
-
if repo in LOADED_MODELS:
|
| 116 |
-
return "Loaded"
|
| 117 |
-
# actual load; may raise
|
| 118 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 119 |
-
repo,
|
| 120 |
-
device_map="auto",
|
| 121 |
-
trust_remote_code=True,
|
| 122 |
-
torch_dtype=torch.bfloat16 if use_bfloat16 else torch.float32,
|
| 123 |
-
)
|
| 124 |
-
model.eval()
|
| 125 |
-
LOADED_MODELS[repo] = model
|
| 126 |
-
return "Loaded"
|
| 127 |
-
except Exception as e:
|
| 128 |
-
return f"Error loading model: {e}"
|
| 129 |
-
|
| 130 |
-
# Determine interactive state for Run button
|
| 131 |
-
def check_all_loaded(n_visible, *status_texts):
|
| 132 |
-
# status_texts are strings: "Loaded" indicates success
|
| 133 |
-
needed = status_texts[:n_visible]
|
| 134 |
-
if all(s == "Loaded" for s in needed):
|
| 135 |
-
return gr.update(interactive=True)
|
| 136 |
-
return gr.update(interactive=False)
|
| 137 |
|
| 138 |
@spaces.GPU()
|
| 139 |
def run_scoring(input_text, *args):
|
|
@@ -148,7 +103,7 @@ def run_scoring(input_text, *args):
|
|
| 148 |
if len(models) < 2:
|
| 149 |
return "Please enter at least two model paths.", None, None
|
| 150 |
threshold = 0.0 if threshold_choice == "default" else custom_threshold
|
| 151 |
-
mosaic_instance = Mosaic(model_name_or_paths=models, one_model_mode=False
|
| 152 |
final_score = mosaic_instance.compute_end_score(input_text)
|
| 153 |
msg = "This text was probably generated." if final_score < threshold else "This text is likely human-written."
|
| 154 |
return msg, final_score, threshold
|
|
@@ -166,20 +121,11 @@ with demo:
|
|
| 166 |
gr.Markdown("**⚠️ Please make sure all models have the same tokenizer or it won’t work.**")
|
| 167 |
gr.Markdown("### Model Paths (at least 2 required)")
|
| 168 |
n_models_state = gr.State(4)
|
| 169 |
-
model_inputs
|
| 170 |
for i in range(1, MAX_MODELS+1):
|
| 171 |
with gr.Row():
|
| 172 |
tb = gr.Textbox(label=f"Model {i} Path", value="" if i > 4 else None, visible=(i <= 4))
|
| 173 |
-
btn = gr.Button("Load", elem_id=f"load_{i}", visible=(i <= 4))
|
| 174 |
-
status = gr.Textbox(label="Loading status", value="Not loaded", interactive=False, visible=(i <= 4))
|
| 175 |
-
btn.click(
|
| 176 |
-
fn=load_single_model,
|
| 177 |
-
inputs=[tb, gr.State(i)],
|
| 178 |
-
outputs=status
|
| 179 |
-
)
|
| 180 |
model_inputs.append(tb)
|
| 181 |
-
load_buttons.append(btn)
|
| 182 |
-
status_boxes.append(status)
|
| 183 |
with gr.Row():
|
| 184 |
plus = gr.Button("Add model slot", elem_id="plus_button")
|
| 185 |
minus = gr.Button("Remove model slot", elem_id="minus_button")
|
|
@@ -188,31 +134,27 @@ with demo:
|
|
| 188 |
plus.click(
|
| 189 |
fn=update_textboxes,
|
| 190 |
inputs=n_models_state,
|
| 191 |
-
outputs=[n_models_state, *model_inputs
|
| 192 |
)
|
| 193 |
minus.click(
|
| 194 |
fn=remove_textboxes,
|
| 195 |
inputs=n_models_state,
|
| 196 |
-
outputs=[n_models_state, *model_inputs
|
| 197 |
)
|
| 198 |
config1_btn.click(
|
| 199 |
fn=apply_config1,
|
| 200 |
-
inputs=None,
|
| 201 |
-
outputs=[
|
| 202 |
-
n_models_state,
|
| 203 |
-
*model_inputs
|
| 204 |
-
*load_buttons, # 3️⃣ your list of 10 Load Buttons
|
| 205 |
-
*status_boxes # 4️⃣ your list of 10 Status Textboxes
|
| 206 |
]
|
| 207 |
)
|
| 208 |
config2_btn.click(
|
| 209 |
fn=apply_config2,
|
| 210 |
-
inputs=None,
|
| 211 |
-
outputs=[
|
| 212 |
-
n_models_state,
|
| 213 |
-
*model_inputs
|
| 214 |
-
*load_buttons, # 3️⃣ your list of 10 Load Buttons
|
| 215 |
-
*status_boxes # 4️⃣ your list of 10 Status Textboxes
|
| 216 |
]
|
| 217 |
)
|
| 218 |
with gr.Row():
|
|
@@ -222,20 +164,8 @@ with demo:
|
|
| 222 |
output_message = gr.Textbox(label="Result Message")
|
| 223 |
output_score = gr.Number(label="Final Score")
|
| 224 |
output_threshold = gr.Number(label="Threshold Used")
|
| 225 |
-
gr.Markdown("**⚠️ All models need to be loaded
|
| 226 |
-
run_button = gr.Button("Run Scoring"
|
| 227 |
-
# Enable Run button when all statuses reflect "Loaded"
|
| 228 |
-
for status in status_boxes:
|
| 229 |
-
status.change(
|
| 230 |
-
fn=check_all_loaded,
|
| 231 |
-
inputs=[n_models_state, *status_boxes],
|
| 232 |
-
outputs=run_button
|
| 233 |
-
)
|
| 234 |
-
n_models_state.change(
|
| 235 |
-
fn=check_all_loaded,
|
| 236 |
-
inputs=[n_models_state, *status_boxes],
|
| 237 |
-
outputs=run_button
|
| 238 |
-
)
|
| 239 |
run_button.click(
|
| 240 |
fn=run_scoring,
|
| 241 |
inputs=[input_text, *model_inputs, threshold_choice, custom_threshold],
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from mosaic import Mosaic
|
| 3 |
import spaces
|
| 4 |
import traceback
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Maximum number of model textboxes
|
| 7 |
MAX_MODELS = 10
|
|
|
|
| 26 |
if n_visible < MAX_MODELS:
|
| 27 |
n_visible += 1
|
| 28 |
tb_updates = [gr.update(visible=(i < n_visible)) for i in range(MAX_MODELS)]
|
| 29 |
+
return (n_visible, *tb_updates)
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Decrease model slots and clear removed entries
|
| 32 |
def remove_textboxes(n_visible):
|
|
|
|
| 37 |
# Remove cached models for slots now hidden
|
| 38 |
for idx in range(new, old):
|
| 39 |
LOADED_MODELS.pop(idx+1, None)
|
| 40 |
+
tb_updates = []
|
| 41 |
for i in range(MAX_MODELS):
|
| 42 |
if i < n_visible:
|
| 43 |
tb_updates.append(gr.update(visible=True))
|
|
|
|
|
|
|
| 44 |
else:
|
| 45 |
tb_updates.append(gr.update(visible=False, value=""))
|
| 46 |
+
return (n_visible, *tb_updates)
|
|
|
|
|
|
|
| 47 |
|
| 48 |
def apply_config1():
|
| 49 |
"""
|
|
|
|
| 53 |
- new visibility for each Load button & status box
|
| 54 |
"""
|
| 55 |
n_vis = len(GPT_CONFIG_MODELS)
|
| 56 |
+
tb_updates = []
|
| 57 |
|
| 58 |
for i in range(MAX_MODELS):
|
| 59 |
if i < n_vis:
|
| 60 |
# show this slot, set its value from CONFIG_MODELS
|
| 61 |
tb_updates.append(gr.update(visible=True, value=GPT_CONFIG_MODELS[i]))
|
|
|
|
|
|
|
| 62 |
else:
|
| 63 |
# hide all others
|
| 64 |
tb_updates.append(gr.update(visible=False, value=""))
|
|
|
|
|
|
|
| 65 |
|
| 66 |
# Return in the same shape as your update_textboxes/remove_textboxes:
|
| 67 |
# (n_models_state, *all textboxes, *all load buttons, *all status boxes)
|
| 68 |
+
return (n_vis, *tb_updates)
|
| 69 |
|
| 70 |
def apply_config2():
|
| 71 |
"""
|
|
|
|
| 75 |
- new visibility for each Load button & status box
|
| 76 |
"""
|
| 77 |
n_vis = len(Falcon_CONFIG_MODELS)
|
| 78 |
+
tb_updates = []
|
| 79 |
|
| 80 |
for i in range(MAX_MODELS):
|
| 81 |
if i < n_vis:
|
| 82 |
# show this slot, set its value from CONFIG_MODELS
|
| 83 |
tb_updates.append(gr.update(visible=True, value=Falcon_CONFIG_MODELS[i]))
|
|
|
|
|
|
|
| 84 |
else:
|
| 85 |
# hide all others
|
| 86 |
tb_updates.append(gr.update(visible=False, value=""))
|
|
|
|
|
|
|
| 87 |
|
| 88 |
# Return in the same shape as your update_textboxes/remove_textboxes:
|
| 89 |
# (n_models_state, *all textboxes, *all load buttons, *all status boxes)
|
| 90 |
+
return (n_vis, *tb_updates)
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
@spaces.GPU()
|
| 94 |
def run_scoring(input_text, *args):
|
|
|
|
| 103 |
if len(models) < 2:
|
| 104 |
return "Please enter at least two model paths.", None, None
|
| 105 |
threshold = 0.0 if threshold_choice == "default" else custom_threshold
|
| 106 |
+
mosaic_instance = Mosaic(model_name_or_paths=models, one_model_mode=False)
|
| 107 |
final_score = mosaic_instance.compute_end_score(input_text)
|
| 108 |
msg = "This text was probably generated." if final_score < threshold else "This text is likely human-written."
|
| 109 |
return msg, final_score, threshold
|
|
|
|
| 121 |
gr.Markdown("**⚠️ Please make sure all models have the same tokenizer or it won’t work.**")
|
| 122 |
gr.Markdown("### Model Paths (at least 2 required)")
|
| 123 |
n_models_state = gr.State(4)
|
| 124 |
+
model_inputs = []
|
| 125 |
for i in range(1, MAX_MODELS+1):
|
| 126 |
with gr.Row():
|
| 127 |
tb = gr.Textbox(label=f"Model {i} Path", value="" if i > 4 else None, visible=(i <= 4))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
model_inputs.append(tb)
|
|
|
|
|
|
|
| 129 |
with gr.Row():
|
| 130 |
plus = gr.Button("Add model slot", elem_id="plus_button")
|
| 131 |
minus = gr.Button("Remove model slot", elem_id="minus_button")
|
|
|
|
| 134 |
plus.click(
|
| 135 |
fn=update_textboxes,
|
| 136 |
inputs=n_models_state,
|
| 137 |
+
outputs=[n_models_state, *model_inputs]
|
| 138 |
)
|
| 139 |
minus.click(
|
| 140 |
fn=remove_textboxes,
|
| 141 |
inputs=n_models_state,
|
| 142 |
+
outputs=[n_models_state, *model_inputs]
|
| 143 |
)
|
| 144 |
config1_btn.click(
|
| 145 |
fn=apply_config1,
|
| 146 |
+
inputs=None,
|
| 147 |
+
outputs=[
|
| 148 |
+
n_models_state,
|
| 149 |
+
*model_inputs
|
|
|
|
|
|
|
| 150 |
]
|
| 151 |
)
|
| 152 |
config2_btn.click(
|
| 153 |
fn=apply_config2,
|
| 154 |
+
inputs=None,
|
| 155 |
+
outputs=[
|
| 156 |
+
n_models_state,
|
| 157 |
+
*model_inputs
|
|
|
|
|
|
|
| 158 |
]
|
| 159 |
)
|
| 160 |
with gr.Row():
|
|
|
|
| 164 |
output_message = gr.Textbox(label="Result Message")
|
| 165 |
output_score = gr.Number(label="Final Score")
|
| 166 |
output_threshold = gr.Number(label="Threshold Used")
|
| 167 |
+
gr.Markdown("**⚠️ All models need to be loaded for scoring, this can take time**")
|
| 168 |
+
run_button = gr.Button("Run Scoring")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
run_button.click(
|
| 170 |
fn=run_scoring,
|
| 171 |
inputs=[input_text, *model_inputs, threshold_choice, custom_threshold],
|
mosaic.py
CHANGED
|
@@ -57,41 +57,28 @@ class Mosaic(object):
|
|
| 57 |
unigram: Optional[str] = None,
|
| 58 |
custom_config: Optional[List[bool]] = None,
|
| 59 |
stupid_mode: bool = False,
|
| 60 |
-
one_model_mode: bool = False
|
| 61 |
-
|
| 62 |
-
loaded_models: Optional[Dict[str, AutoModelForCausalLM]] = None,
|
| 63 |
-
) -> None:
|
| 64 |
"""
|
| 65 |
If `loaded_models` is provided, re-use any entries matching
|
| 66 |
model_name_or_paths; otherwise load and optionally register
|
| 67 |
into that dict.
|
| 68 |
"""
|
| 69 |
self.models = []
|
| 70 |
-
# ensure we have a dict to cache into if passed
|
| 71 |
-
cache = loaded_models if loaded_models is not None else {}
|
| 72 |
|
| 73 |
for model_name_or_path in model_name_or_paths:
|
| 74 |
-
#
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
trust_remote_code=True,
|
| 84 |
-
torch_dtype=torch.bfloat16 if use_bfloat16 else torch.float32,
|
| 85 |
-
)
|
| 86 |
-
model.eval()
|
| 87 |
-
# cache for reuse
|
| 88 |
-
if loaded_models is not None:
|
| 89 |
-
cache[model_name_or_path] = model
|
| 90 |
self.models.append(model)
|
| 91 |
print(f"Loaded model: {model_name_or_path}")
|
| 92 |
|
| 93 |
-
# store optional references
|
| 94 |
-
self.loaded_models = cache
|
| 95 |
self.one_model_mode = one_model_mode
|
| 96 |
|
| 97 |
if stupid_mode:
|
|
|
|
| 57 |
unigram: Optional[str] = None,
|
| 58 |
custom_config: Optional[List[bool]] = None,
|
| 59 |
stupid_mode: bool = False,
|
| 60 |
+
one_model_mode: bool = False
|
| 61 |
+
) -> None:
|
|
|
|
|
|
|
| 62 |
"""
|
| 63 |
If `loaded_models` is provided, re-use any entries matching
|
| 64 |
model_name_or_paths; otherwise load and optionally register
|
| 65 |
into that dict.
|
| 66 |
"""
|
| 67 |
self.models = []
|
|
|
|
|
|
|
| 68 |
|
| 69 |
for model_name_or_path in model_name_or_paths:
|
| 70 |
+
# load from pre-trained hub or path
|
| 71 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 72 |
+
model_name_or_path,
|
| 73 |
+
device_map="auto",
|
| 74 |
+
trust_remote_code=True,
|
| 75 |
+
torch_dtype=torch.bfloat16 if use_bfloat16 else torch.float32,
|
| 76 |
+
)
|
| 77 |
+
model.eval()
|
| 78 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
self.models.append(model)
|
| 80 |
print(f"Loaded model: {model_name_or_path}")
|
| 81 |
|
|
|
|
|
|
|
| 82 |
self.one_model_mode = one_model_mode
|
| 83 |
|
| 84 |
if stupid_mode:
|