File size: 2,970 Bytes
840b176
 
028bd08
840b176
15e2a67
028bd08
15e2a67
 
 
 
 
 
 
 
 
 
 
840b176
028bd08
 
15e2a67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
028bd08
 
 
 
840b176
 
 
 
 
028bd08
840b176
 
028bd08
 
840b176
028bd08
840b176
028bd08
 
 
 
 
 
 
840b176
028bd08
 
 
 
 
840b176
 
 
028bd08
840b176
028bd08
1
2
3
4
5
6
7
8
9
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import gradio as gr
import torch
from transformers import GPT2LMHeadModel, GPT2Tokenizer

# Load model and tokenizer
def load_model():
    try:
        # Load the fine-tuned model
        model = GPT2LMHeadModel.from_pretrained("aayushraina/gpt2shakespeare")
        # Use the base GPT-2 tokenizer
        tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
        model.eval()
        print("Model and tokenizer loaded successfully!")
        return model, tokenizer
    except Exception as e:
        print(f"Error loading model: {e}")
        return None, None

# Text generation function
def generate_text(prompt, max_length=500, temperature=0.8, top_k=40, top_p=0.9):
    if model is None or tokenizer is None:
        return "Error: Model not loaded properly"
        
    try:
        # Encode the input prompt
        input_ids = tokenizer.encode(prompt, return_tensors='pt')
        
        # Generate text
        with torch.no_grad():
            output = model.generate(
                input_ids,
                max_length=max_length,
                temperature=temperature,
                top_k=top_k,
                top_p=top_p,
                do_sample=True,
                pad_token_id=tokenizer.eos_token_id,
                num_return_sequences=1
            )
        
        # Decode and return the generated text
        generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
        return generated_text
    except Exception as e:
        return f"Error during generation: {str(e)}"

# Load model and tokenizer globally
print("Loading model and tokenizer...")
model, tokenizer = load_model()

# Create Gradio interface
demo = gr.Interface(
    fn=generate_text,
    inputs=[
        gr.Textbox(label="Enter your prompt", placeholder="Start your text here...", lines=2),
        gr.Slider(minimum=10, maximum=1000, value=500, step=10, label="Maximum Length"),
        gr.Slider(minimum=0.1, maximum=2.0, value=0.8, step=0.1, label="Temperature"),
        gr.Slider(minimum=1, maximum=100, value=40, step=1, label="Top-k"),
        gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.1, label="Top-p"),
    ],
    outputs=gr.Textbox(label="Generated Text", lines=10),
    title="Shakespeare-style Text Generator",
    description="""Generate Shakespeare-style text using a fine-tuned GPT-2 model.
    
    Parameters:
    - Temperature: Higher values make the output more random, lower values more focused
    - Top-k: Number of highest probability vocabulary tokens to keep for top-k filtering
    - Top-p: Cumulative probability for nucleus sampling
    """,
    examples=[
        ["First Citizen:", 500, 0.8, 40, 0.9],
        ["To be, or not to be,", 500, 0.8, 40, 0.9],
        ["Friends, Romans, countrymen,", 500, 0.8, 40, 0.9],
        ["O Romeo, Romeo,", 500, 0.8, 40, 0.9],
        ["Now is the winter of our discontent", 500, 0.8, 40, 0.9]
    ]
)

# Launch the app
if __name__ == "__main__":
    demo.launch()