|
import os |
|
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline |
|
import gradio as gr |
|
import wandb |
|
|
|
|
|
hf_api_key = os.getenv("HF_TOKEN") |
|
wandb_api_key = os.getenv("WANDB_API_KEY") |
|
|
|
|
|
if not hf_api_key: |
|
raise ValueError("Hugging Face API key not found. Please ensure it's set as a secret in the Space.") |
|
if not wandb_api_key: |
|
raise ValueError("Weights & Biases API key not found. Please ensure it's set as a secret in the Space.") |
|
|
|
|
|
wandb.login(key=wandb_api_key) |
|
|
|
|
|
def load_pipeline(model_name, fine_tuned=False): |
|
|
|
paths = { |
|
"gpt2": ("gpt2-medium", "sbicy/finetuned-gpt2"), |
|
"gpt_neo": ("EleutherAI/gpt-neo-1.3B", "sbicy/finetuned-gpt-neo"), |
|
"gpt_j": ("EleutherAI/gpt-j-6B", "sbicy/finetuned-gpt-j") |
|
} |
|
|
|
pretrained_model_name, finetuned_model_path = paths[model_name] |
|
model_path = finetuned_model_path if fine_tuned else pretrained_model_name |
|
|
|
|
|
model = AutoModelForCausalLM.from_pretrained(model_path, use_auth_token=hf_api_key) |
|
tokenizer = AutoTokenizer.from_pretrained(model_path, use_auth_token=hf_api_key) |
|
tokenizer.pad_token = tokenizer.eos_token |
|
|
|
|
|
return pipeline("text-generation", model=model, tokenizer=tokenizer) |
|
|
|
|
|
def compare_single_model(prompt, model_choice, temperature, top_p, max_length): |
|
|
|
pretrained_pipeline = load_pipeline(model_choice, fine_tuned=False) |
|
finetuned_pipeline = load_pipeline(model_choice, fine_tuned=True) |
|
|
|
|
|
pretrained_response = pretrained_pipeline(prompt, temperature=temperature, top_p=top_p, max_length=int(max_length))[0]["generated_text"] |
|
finetuned_response = finetuned_pipeline(prompt, temperature=temperature, top_p=top_p, max_length=int(max_length))[0]["generated_text"] |
|
|
|
return pretrained_response, finetuned_response |
|
|
|
|
|
interface = gr.Interface( |
|
fn=compare_single_model, |
|
inputs=[ |
|
"text", |
|
gr.Dropdown(choices=["gpt2", "gpt_neo", "gpt_j"], label="Select Model"), |
|
gr.Slider(0.1, 1.0, value=0.7, step=0.1, label="Temperature"), |
|
gr.Slider(0.1, 1.0, value=0.9, step=0.1, label="Top-p"), |
|
gr.Slider(10, 100, value=50, step=10, label="Max Length") |
|
], |
|
outputs=[ |
|
gr.Textbox(label="Pre-trained Response"), |
|
gr.Textbox(label="Fine-tuned Response") |
|
], |
|
title="Single Model Comparison: Pre-trained vs Fine-tuned", |
|
description="Enter a prompt and select a model to compare responses from pre-trained and fine-tuned versions. Adjust parameters with the sliders." |
|
) |
|
|
|
|
|
interface.launch() |