Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load models and tokenizers
|
| 6 |
+
models = {
|
| 7 |
+
"GPT From Scratch (benchaffe/shakespeare-gpt-mini)": "benchaffe/shakespeare-gpt-mini",
|
| 8 |
+
"Fine-tuned DistilGPT2 (benchaffe/shakespeare-distilgpt2)": "benchaffe/shakespeare-distilgpt2"
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
model_objects = {}
|
| 12 |
+
tokenizer_objects = {}
|
| 13 |
+
|
| 14 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
+
|
| 16 |
+
for name, path in models.items():
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained(path)
|
| 18 |
+
model = AutoModelForCausalLM.from_pretrained(path).to(device)
|
| 19 |
+
model.eval()
|
| 20 |
+
model_objects[name] = model
|
| 21 |
+
tokenizer_objects[name] = tokenizer
|
| 22 |
+
|
| 23 |
+
# Generation function
|
| 24 |
+
def generate_comparison(prompt, max_length, temperature, top_p, top_k, do_sample):
|
| 25 |
+
results = {}
|
| 26 |
+
for name in models:
|
| 27 |
+
tokenizer = tokenizer_objects[name]
|
| 28 |
+
model = model_objects[name]
|
| 29 |
+
|
| 30 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 31 |
+
output = model.generate(
|
| 32 |
+
**inputs,
|
| 33 |
+
max_length=max_length,
|
| 34 |
+
temperature=temperature,
|
| 35 |
+
top_p=top_p,
|
| 36 |
+
top_k=top_k,
|
| 37 |
+
do_sample=do_sample,
|
| 38 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 39 |
+
)
|
| 40 |
+
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 41 |
+
results[name] = generated_text
|
| 42 |
+
return [results[name] for name in models]
|
| 43 |
+
|
| 44 |
+
# Examples
|
| 45 |
+
example_prompts = [
|
| 46 |
+
"What light through yonder window breaks",
|
| 47 |
+
"To be or not to be, that is the question",
|
| 48 |
+
"Hark! Who goes there?",
|
| 49 |
+
"My love is deep; the more I give to thee",
|
| 50 |
+
"Thou art more lovely and more temperate"
|
| 51 |
+
]
|
| 52 |
+
|
| 53 |
+
with gr.Blocks(title="Shakespeare Model Comparison") as demo:
|
| 54 |
+
gr.Markdown("# 🧠 Shakespeare GPT Model Comparison")
|
| 55 |
+
gr.Markdown("Compare outputs from two models: one trained from scratch and one fine-tuned from DistilGPT2. Adjust generation parameters below.")
|
| 56 |
+
|
| 57 |
+
with gr.Row():
|
| 58 |
+
with gr.Column():
|
| 59 |
+
prompt = gr.Textbox(label="Input Prompt", placeholder="Enter a Shakespearean prompt here...")
|
| 60 |
+
example = gr.Dropdown(example_prompts, label="Select an Example Prompt")
|
| 61 |
+
def set_example(e): return gr.Textbox.update(value=e)
|
| 62 |
+
example.change(fn=set_example, inputs=example, outputs=prompt)
|
| 63 |
+
|
| 64 |
+
max_length = gr.Slider(32, 256, value=80, step=8, label="Max Length")
|
| 65 |
+
temperature = gr.Slider(0.1, 1.5, value=0.8, step=0.1, label="Temperature")
|
| 66 |
+
top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
| 67 |
+
top_k = gr.Slider(0, 100, value=50, step=5, label="Top-k")
|
| 68 |
+
do_sample = gr.Checkbox(value=True, label="Use Sampling (if unchecked = greedy decoding)")
|
| 69 |
+
|
| 70 |
+
submit_btn = gr.Button("Generate")
|
| 71 |
+
|
| 72 |
+
with gr.Column():
|
| 73 |
+
outputs = [gr.Textbox(label=name) for name in models]
|
| 74 |
+
|
| 75 |
+
submit_btn.click(fn=generate_comparison, inputs=[prompt, max_length, temperature, top_p, top_k, do_sample], outputs=outputs)
|
| 76 |
+
|
| 77 |
+
demo.launch()
|