Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,103 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# Load
|
| 6 |
-
tokenizer = AutoTokenizer.from_pretrained("
|
| 7 |
-
model = AutoModelForCausalLM.from_pretrained("
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
model.to(device)
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
# Gradio interface
|
| 26 |
iface = gr.Interface(
|
| 27 |
-
fn=
|
| 28 |
inputs=gr.Textbox(label="You"),
|
| 29 |
outputs=gr.Textbox(label="AI"),
|
| 30 |
-
title="
|
| 31 |
-
description="
|
|
|
|
| 32 |
)
|
| 33 |
|
| 34 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
+
import time
|
| 5 |
+
import random
|
| 6 |
|
| 7 |
+
# Load tiny model
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained("nilq/mistral-1L-tiny")
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained("nilq/mistral-1L-tiny")
|
| 10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
model.to(device)
|
| 12 |
|
| 13 |
+
chat_history = []
|
| 14 |
+
|
| 15 |
+
def chaotic_ai(user_input):
|
| 16 |
+
global chat_history
|
| 17 |
+
# Keep last 5 messages only
|
| 18 |
+
chat_history = chat_history[-5:]
|
| 19 |
+
chat_history.append({"role": "user", "content": user_input})
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
inputs = tokenizer.apply_chat_template(
|
| 23 |
+
chat_history,
|
| 24 |
+
add_generation_prompt=True,
|
| 25 |
+
tokenize=True,
|
| 26 |
+
return_dict=True,
|
| 27 |
+
return_tensors="pt",
|
| 28 |
+
).to(device)
|
| 29 |
+
except Exception as e:
|
| 30 |
+
yield f"[!] Tokenizer crashed: {str(e)}\n"
|
| 31 |
+
return
|
| 32 |
+
|
| 33 |
+
output_ids = inputs["input_ids"].clone()
|
| 34 |
+
generated_text = ""
|
| 35 |
+
|
| 36 |
+
for _ in range(20): # low max_new_tokens
|
| 37 |
+
try:
|
| 38 |
+
# 10% chance skip token
|
| 39 |
+
if random.random() < 0.1:
|
| 40 |
+
continue
|
| 41 |
+
|
| 42 |
+
# 5% chance repeat last token
|
| 43 |
+
if random.random() < 0.05 and len(output_ids) > 0:
|
| 44 |
+
next_token_id = output_ids[0, -1].unsqueeze(0)
|
| 45 |
+
else:
|
| 46 |
+
new_tokens = model.generate(
|
| 47 |
+
**{"input_ids": output_ids},
|
| 48 |
+
max_new_tokens=1,
|
| 49 |
+
do_sample=True,
|
| 50 |
+
temperature=1.5,
|
| 51 |
+
top_k=50,
|
| 52 |
+
top_p=0.95
|
| 53 |
+
)
|
| 54 |
+
next_token_id = new_tokens[0, -1].unsqueeze(0)
|
| 55 |
+
|
| 56 |
+
# Randomly raise error 5% of time
|
| 57 |
+
if random.random() < 0.05:
|
| 58 |
+
raise RuntimeError("Random token generation failure!")
|
| 59 |
+
|
| 60 |
+
output_ids = torch.cat([output_ids, next_token_id.unsqueeze(0)], dim=1)
|
| 61 |
+
|
| 62 |
+
# Decode latest token
|
| 63 |
+
token_str = tokenizer.decode(next_token_id)
|
| 64 |
+
|
| 65 |
+
# Glitch characters 15%
|
| 66 |
+
token_glitch = "".join(c if random.random() > 0.15 else random.choice("@#$%&?") for c in token_str)
|
| 67 |
+
|
| 68 |
+
# Randomly truncate mid-token 5%
|
| 69 |
+
if random.random() < 0.05:
|
| 70 |
+
token_glitch = token_glitch[:max(1, len(token_glitch)//2)]
|
| 71 |
+
|
| 72 |
+
# Randomly erase previous characters 5%
|
| 73 |
+
if random.random() < 0.05 and len(generated_text) > 0:
|
| 74 |
+
erase_len = random.randint(1, min(3, len(generated_text)))
|
| 75 |
+
generated_text = generated_text[:-erase_len]
|
| 76 |
+
|
| 77 |
+
generated_text += token_glitch
|
| 78 |
+
|
| 79 |
+
# Random slow down
|
| 80 |
+
time.sleep(0.4 + random.random()*0.6)
|
| 81 |
+
|
| 82 |
+
# Yield live update
|
| 83 |
+
yield generated_text
|
| 84 |
+
|
| 85 |
+
except Exception as e:
|
| 86 |
+
yield f"[!] Crash: {str(e)}\n"
|
| 87 |
|
| 88 |
+
# Occasionally duplicate response in history
|
| 89 |
+
if random.random() < 0.1:
|
| 90 |
+
chat_history.append({"role": "assistant", "content": generated_text*2})
|
| 91 |
+
else:
|
| 92 |
+
chat_history.append({"role": "assistant", "content": generated_text})
|
| 93 |
|
|
|
|
| 94 |
iface = gr.Interface(
|
| 95 |
+
fn=chaotic_ai,
|
| 96 |
inputs=gr.Textbox(label="You"),
|
| 97 |
outputs=gr.Textbox(label="AI"),
|
| 98 |
+
title="💀 Insane Chaotic Tiny AI",
|
| 99 |
+
description="Slow, glitchy, buggy, erasing, repeating, chaotic AI. Every terrible behavior is real.",
|
| 100 |
+
live=True
|
| 101 |
)
|
| 102 |
|
| 103 |
iface.launch()
|