tt
Browse files
app.py
CHANGED
|
@@ -1,70 +1,65 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
import torch.nn.functional as F
|
|
|
|
| 5 |
|
| 6 |
-
# Load tiny model (CPU-friendly)
|
| 7 |
MODEL_ID = "tiiuae/falcon-rw-1b"
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 9 |
model = AutoModelForCausalLM.from_pretrained(MODEL_ID).to("cpu")
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
outputs=output,
|
| 68 |
-
)
|
| 69 |
-
|
| 70 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
import torch.nn.functional as F
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
|
|
|
|
| 6 |
MODEL_ID = "tiiuae/falcon-rw-1b"
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(MODEL_ID).to("cpu")
|
| 9 |
|
| 10 |
+
def generate_stream(sysA, sysB, wa, wb, user_input, max_new_tokens=50, temperature=1.0, top_p=0.95):
|
| 11 |
+
promptA = f"<|system|>{sysA}\n<|user|>{user_input}<|assistant|>"
|
| 12 |
+
promptB = f"<|system|>{sysB}\n<|user|>{user_input}<|assistant|>"
|
| 13 |
+
|
| 14 |
+
idsA = tokenizer(promptA, return_tensors="pt").input_ids.to(model.device)
|
| 15 |
+
idsB = tokenizer(promptB, return_tensors="pt").input_ids.to(model.device)
|
| 16 |
+
|
| 17 |
+
outA = idsA.clone()
|
| 18 |
+
outB = idsB.clone()
|
| 19 |
+
response = ""
|
| 20 |
+
yield response # initial empty chunk
|
| 21 |
+
|
| 22 |
+
for _ in range(max_new_tokens):
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
logitsA = model(input_ids=outA).logits[:, -1, :]
|
| 25 |
+
logitsB = model(input_ids=outB).logits[:, -1, :]
|
| 26 |
+
|
| 27 |
+
logits = wa * logitsA + wb * logitsB
|
| 28 |
+
logits = logits / (temperature if temperature > 0 else 1.0)
|
| 29 |
+
probs = F.softmax(logits, dim=-1)
|
| 30 |
+
|
| 31 |
+
sorted_probs, sorted_idx = torch.sort(probs, descending=True)
|
| 32 |
+
cumulative = torch.cumsum(sorted_probs, dim=-1)
|
| 33 |
+
sorted_probs[cumulative > top_p] = 0
|
| 34 |
+
sorted_probs = sorted_probs / sorted_probs.sum(dim=-1, keepdim=True)
|
| 35 |
+
|
| 36 |
+
token = sorted_idx[:, torch.multinomial(sorted_probs, 1)].squeeze()
|
| 37 |
+
outA = torch.cat([outA, token.unsqueeze(0).unsqueeze(0)], dim=1)
|
| 38 |
+
outB = torch.cat([outB, token.unsqueeze(0).unsqueeze(0)], dim=1)
|
| 39 |
+
|
| 40 |
+
token_str = tokenizer.decode(token)
|
| 41 |
+
response += token_str
|
| 42 |
+
yield response
|
| 43 |
+
|
| 44 |
+
if token.item() == tokenizer.eos_token_id:
|
| 45 |
+
break
|
| 46 |
+
|
| 47 |
+
with gr.ChatInterface(
|
| 48 |
+
fn=generate_stream,
|
| 49 |
+
inputs=[
|
| 50 |
+
gr.Textbox(label="System Prompt A", value="You are assistant A"),
|
| 51 |
+
gr.Textbox(label="System Prompt B", value="You are assistant B"),
|
| 52 |
+
gr.Slider(label="Weight wA", minimum=-5.0, maximum=5.0, step=0.1, value=1.0),
|
| 53 |
+
gr.Slider(label="Weight wB", minimum=-5.0, maximum=5.0, step=0.1, value=1.0),
|
| 54 |
+
gr.Textbox(label="User Message", placeholder="Enter your message here..."),
|
| 55 |
+
gr.Slider(label="Max new tokens", minimum=1, maximum=200, step=1, value=50),
|
| 56 |
+
gr.Slider(label="Temperature", minimum=0.1, maximum=2.0, step=0.1, value=1.0),
|
| 57 |
+
gr.Slider(label="Top‑p", minimum=0.1, maximum=1.0, step=0.05, value=0.95),
|
| 58 |
+
],
|
| 59 |
+
title="Blended Two-System Streaming Chat",
|
| 60 |
+
description="Stream replies by blending logits from two system-prompts using weights wA and wB.",
|
| 61 |
+
):
|
| 62 |
+
pass
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|