Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -762,7 +762,7 @@ def build_inputs(tokenizer, model_id: str, prompt: str):
|
|
| 762 |
def is_diffusion_model(model_id: str) -> bool:
|
| 763 |
return "metadiffusion" in model_id.lower()
|
| 764 |
|
| 765 |
-
def generate_for_model(model_id: str, prompt: str) -> str:
|
| 766 |
ensure_models_loaded()
|
| 767 |
if model_id not in models or model_id not in tokenizers:
|
| 768 |
short = MODEL_DISPLAY.get(model_id, model_id)
|
|
@@ -773,6 +773,8 @@ def generate_for_model(model_id: str, prompt: str) -> str:
|
|
| 773 |
model = models[model_id]
|
| 774 |
cfg = GEN_DEFAULTS.get(model_id, {})
|
| 775 |
max_new = cfg.get("max_new_tokens", 128)
|
|
|
|
|
|
|
| 776 |
try:
|
| 777 |
if is_diffusion_model(model_id):
|
| 778 |
return generate_diffusion(model, tokenizer, prompt, cfg) # type: ignore
|
|
@@ -894,6 +896,14 @@ def create_demo() -> gr.Blocks:
|
|
| 894 |
label="Your prompt",
|
| 895 |
placeholder="Ask anything... e.g. 'Explain quantum computing in simple terms' or 'Write a haiku about rain'",
|
| 896 |
lines=3,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 897 |
)
|
| 898 |
with gr.Row():
|
| 899 |
submit_btn = gr.Button("⚔️ Battle", variant="primary", scale=1)
|
|
@@ -928,6 +938,7 @@ def create_demo() -> gr.Blocks:
|
|
| 928 |
|
| 929 |
return {
|
| 930 |
"prompt": prompt,
|
|
|
|
| 931 |
"submit_btn": submit_btn,
|
| 932 |
"clear_btn": clear_btn,
|
| 933 |
"response_a": response_a,
|
|
@@ -1012,7 +1023,7 @@ def create_demo() -> gr.Blocks:
|
|
| 1012 |
return None
|
| 1013 |
|
| 1014 |
@spaces.GPU(duration=120)
|
| 1015 |
-
def battle_gpu(spec: ArenaSpec, user_prompt: str, last_pair_val):
|
| 1016 |
reason = moderate_prompt_impl(user_prompt)
|
| 1017 |
if reason is not None:
|
| 1018 |
return reason, None, None, None, None
|
|
@@ -1020,11 +1031,11 @@ def create_demo() -> gr.Blocks:
|
|
| 1020 |
if random.random() < 0.5:
|
| 1021 |
a, b = b, a
|
| 1022 |
ensure_models_loaded()
|
| 1023 |
-
resp_a = generate_for_model(a, user_prompt)
|
| 1024 |
-
resp_b = generate_for_model(b, user_prompt)
|
| 1025 |
return None, resp_a, resp_b, a, b
|
| 1026 |
|
| 1027 |
-
def on_submit(spec: ArenaSpec, user_prompt: str, last_pair_val):
|
| 1028 |
user_prompt = (user_prompt or "").strip()
|
| 1029 |
if not user_prompt:
|
| 1030 |
return (
|
|
@@ -1041,7 +1052,9 @@ def create_demo() -> gr.Blocks:
|
|
| 1041 |
"", "", False, user_prompt, last_pair_val,
|
| 1042 |
leaderboard_dataframe(load_elo(spec), spec)
|
| 1043 |
)
|
| 1044 |
-
|
|
|
|
|
|
|
| 1045 |
if reason is not None:
|
| 1046 |
flag_text = f"This prompt was flagged for: {reason}"
|
| 1047 |
return (
|
|
@@ -1196,13 +1209,13 @@ def create_demo() -> gr.Blocks:
|
|
| 1196 |
vote_outputs = [u["reveal_a"], u["reveal_b"], u["status"], u["vote_a"], u["vote_tie"], u["vote_both_bad"], u["vote_b"], u["new_round_btn"], u["voted_state"], b["leaderboard"]]
|
| 1197 |
|
| 1198 |
u["submit_btn"].click(
|
| 1199 |
-
fn=lambda p, lp, s=spec: on_submit(s, p, lp),
|
| 1200 |
-
inputs=[u["prompt"], u["last_pair"]],
|
| 1201 |
outputs=submit_outputs,
|
| 1202 |
)
|
| 1203 |
u["prompt"].submit(
|
| 1204 |
-
fn=lambda p, lp, s=spec: on_submit(s, p, lp),
|
| 1205 |
-
inputs=[u["prompt"], u["last_pair"]],
|
| 1206 |
outputs=submit_outputs,
|
| 1207 |
)
|
| 1208 |
for btn, choice in ((u["vote_a"], "A"), (u["vote_tie"], "Tie"), (u["vote_both_bad"], "Both Bad"), (u["vote_b"], "B")):
|
|
|
|
| 762 |
def is_diffusion_model(model_id: str) -> bool:
|
| 763 |
return "metadiffusion" in model_id.lower()
|
| 764 |
|
| 765 |
+
def generate_for_model(model_id: str, prompt: str, max_new_tokens: int = 0) -> str:
|
| 766 |
ensure_models_loaded()
|
| 767 |
if model_id not in models or model_id not in tokenizers:
|
| 768 |
short = MODEL_DISPLAY.get(model_id, model_id)
|
|
|
|
| 773 |
model = models[model_id]
|
| 774 |
cfg = GEN_DEFAULTS.get(model_id, {})
|
| 775 |
max_new = cfg.get("max_new_tokens", 128)
|
| 776 |
+
if max_new_tokens and int(max_new_tokens) > 0:
|
| 777 |
+
max_new = max(16, min(int(max_new_tokens), 512))
|
| 778 |
try:
|
| 779 |
if is_diffusion_model(model_id):
|
| 780 |
return generate_diffusion(model, tokenizer, prompt, cfg) # type: ignore
|
|
|
|
| 896 |
label="Your prompt",
|
| 897 |
placeholder="Ask anything... e.g. 'Explain quantum computing in simple terms' or 'Write a haiku about rain'",
|
| 898 |
lines=3,
|
| 899 |
+
max_length=256,
|
| 900 |
+
)
|
| 901 |
+
max_new = gr.Slider(
|
| 902 |
+
minimum=64,
|
| 903 |
+
maximum=512,
|
| 904 |
+
step=64,
|
| 905 |
+
value=128,
|
| 906 |
+
label="Response length (tokens)",
|
| 907 |
)
|
| 908 |
with gr.Row():
|
| 909 |
submit_btn = gr.Button("⚔️ Battle", variant="primary", scale=1)
|
|
|
|
| 938 |
|
| 939 |
return {
|
| 940 |
"prompt": prompt,
|
| 941 |
+
"max_new": max_new,
|
| 942 |
"submit_btn": submit_btn,
|
| 943 |
"clear_btn": clear_btn,
|
| 944 |
"response_a": response_a,
|
|
|
|
| 1023 |
return None
|
| 1024 |
|
| 1025 |
@spaces.GPU(duration=120)
|
| 1026 |
+
def battle_gpu(spec: ArenaSpec, user_prompt: str, last_pair_val, max_new_tokens: int = 0):
|
| 1027 |
reason = moderate_prompt_impl(user_prompt)
|
| 1028 |
if reason is not None:
|
| 1029 |
return reason, None, None, None, None
|
|
|
|
| 1031 |
if random.random() < 0.5:
|
| 1032 |
a, b = b, a
|
| 1033 |
ensure_models_loaded()
|
| 1034 |
+
resp_a = generate_for_model(a, user_prompt, max_new_tokens)
|
| 1035 |
+
resp_b = generate_for_model(b, user_prompt, max_new_tokens)
|
| 1036 |
return None, resp_a, resp_b, a, b
|
| 1037 |
|
| 1038 |
+
def on_submit(spec: ArenaSpec, user_prompt: str, last_pair_val, max_new_tokens: int = 128):
|
| 1039 |
user_prompt = (user_prompt or "").strip()
|
| 1040 |
if not user_prompt:
|
| 1041 |
return (
|
|
|
|
| 1052 |
"", "", False, user_prompt, last_pair_val,
|
| 1053 |
leaderboard_dataframe(load_elo(spec), spec)
|
| 1054 |
)
|
| 1055 |
+
if len(user_prompt) > 256:
|
| 1056 |
+
user_prompt = user_prompt[:256]
|
| 1057 |
+
reason, resp_a, resp_b, a, b = battle_gpu(spec, user_prompt, last_pair_val, int(max_new_tokens or 128))
|
| 1058 |
if reason is not None:
|
| 1059 |
flag_text = f"This prompt was flagged for: {reason}"
|
| 1060 |
return (
|
|
|
|
| 1209 |
vote_outputs = [u["reveal_a"], u["reveal_b"], u["status"], u["vote_a"], u["vote_tie"], u["vote_both_bad"], u["vote_b"], u["new_round_btn"], u["voted_state"], b["leaderboard"]]
|
| 1210 |
|
| 1211 |
u["submit_btn"].click(
|
| 1212 |
+
fn=lambda p, lp, mn, s=spec: on_submit(s, p, lp, mn),
|
| 1213 |
+
inputs=[u["prompt"], u["last_pair"], u["max_new"]],
|
| 1214 |
outputs=submit_outputs,
|
| 1215 |
)
|
| 1216 |
u["prompt"].submit(
|
| 1217 |
+
fn=lambda p, lp, mn, s=spec: on_submit(s, p, lp, mn),
|
| 1218 |
+
inputs=[u["prompt"], u["last_pair"], u["max_new"]],
|
| 1219 |
outputs=submit_outputs,
|
| 1220 |
)
|
| 1221 |
for btn, choice in ((u["vote_a"], "A"), (u["vote_tie"], "Tie"), (u["vote_both_bad"], "Both Bad"), (u["vote_b"], "B")):
|