tt
Browse files
app.py
CHANGED
|
@@ -1,65 +1,70 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
import torch.nn.functional as F
|
| 4 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
-
|
| 6 |
-
# Model selection
|
| 7 |
-
MODEL_ID = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 8 |
|
|
|
|
|
|
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 10 |
-
model = AutoModelForCausalLM.from_pretrained(MODEL_ID
|
| 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 |
-
gr.
|
| 53 |
-
gr.
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
gr.Slider(
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
def get_logits(prompt, system_msg):
|
| 13 |
+
"""Run the model and return logits for the next token."""
|
| 14 |
+
input_text = f"<|system|>{system_msg}\n<|user|>{prompt}<|assistant|>"
|
| 15 |
+
inputs = tokenizer(input_text, return_tensors="pt")
|
| 16 |
+
with torch.no_grad():
|
| 17 |
+
outputs = model(**inputs)
|
| 18 |
+
logits = outputs.logits[:, -1, :] # Only final token logits
|
| 19 |
+
return logits
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def blended_generate(prompt, sys1, sys2, wa, wb, temperature=1.0):
|
| 23 |
+
# Get logits from both system prompts
|
| 24 |
+
logits1 = get_logits(prompt, sys1)
|
| 25 |
+
logits2 = get_logits(prompt, sys2)
|
| 26 |
+
|
| 27 |
+
# Weighted sum
|
| 28 |
+
blended_logits = wa * logits1 + wb * logits2
|
| 29 |
+
|
| 30 |
+
# Apply temperature
|
| 31 |
+
blended_logits = blended_logits / temperature
|
| 32 |
+
|
| 33 |
+
# Convert to probabilities
|
| 34 |
+
probs = F.softmax(blended_logits, dim=-1)
|
| 35 |
+
|
| 36 |
+
# Sample one token from the distribution
|
| 37 |
+
token_id = torch.multinomial(probs, num_samples=1)
|
| 38 |
+
next_token = tokenizer.decode(token_id[0])
|
| 39 |
+
|
| 40 |
+
return next_token.strip()
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# Gradio UI
|
| 44 |
+
with gr.Blocks() as demo:
|
| 45 |
+
gr.Markdown("## 🔀 Blended System Prompts using Falcon-RW-1B")
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
prompt = gr.Textbox(label="User Prompt", value="Tell me a joke about computers.")
|
| 49 |
+
|
| 50 |
+
with gr.Row():
|
| 51 |
+
sys1 = gr.Textbox(label="System Prompt A", value="You are a polite assistant.")
|
| 52 |
+
sys2 = gr.Textbox(label="System Prompt B", value="You are a sarcastic assistant.")
|
| 53 |
+
|
| 54 |
+
with gr.Row():
|
| 55 |
+
wa = gr.Slider(-10, 10, value=1.0, step=0.1, label="Weight A")
|
| 56 |
+
wb = gr.Slider(-10, 10, value=1.0, step=0.1, label="Weight B")
|
| 57 |
+
|
| 58 |
+
with gr.Row():
|
| 59 |
+
temperature = gr.Slider(0.1, 2.0, value=1.0, step=0.1, label="Temperature")
|
| 60 |
+
|
| 61 |
+
output = gr.Textbox(label="Next Token")
|
| 62 |
+
|
| 63 |
+
generate_btn = gr.Button("Generate Next Token")
|
| 64 |
+
generate_btn.click(
|
| 65 |
+
fn=blended_generate,
|
| 66 |
+
inputs=[prompt, sys1, sys2, wa, wb, temperature],
|
| 67 |
+
outputs=output,
|
| 68 |
+
)
|
| 69 |
|
| 70 |
+
demo.launch()
|