Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import torch
|
3 |
+
import gradio as gr
|
4 |
+
from train_optimized import GPT, GPTConfig
|
5 |
+
from huggingface_hub import hf_hub_download
|
6 |
+
import json
|
7 |
+
|
8 |
+
# Cache for model and tokenizer
|
9 |
+
MODEL = None
|
10 |
+
CHARS = None
|
11 |
+
STOI = None
|
12 |
+
ITOS = None
|
13 |
+
|
14 |
+
def initialize():
|
15 |
+
global MODEL, CHARS, STOI, ITOS
|
16 |
+
|
17 |
+
if MODEL is None:
|
18 |
+
print("Loading model and tokenizer...")
|
19 |
+
# Download model files from HF Hub
|
20 |
+
config_path = hf_hub_download(repo_id="jatingocodeo/shakespeare-decoder", filename="config.json")
|
21 |
+
model_path = hf_hub_download(repo_id="jatingocodeo/shakespeare-decoder", filename="pytorch_model.bin")
|
22 |
+
|
23 |
+
# Load config
|
24 |
+
with open(config_path, 'r') as f:
|
25 |
+
config_dict = json.load(f)
|
26 |
+
|
27 |
+
# Initialize model with config
|
28 |
+
config = GPTConfig(
|
29 |
+
vocab_size=config_dict['vocab_size'],
|
30 |
+
n_layer=config_dict['n_layer'],
|
31 |
+
n_head=config_dict['n_head'],
|
32 |
+
n_embd=config_dict['n_embd'],
|
33 |
+
block_size=config_dict['block_size'],
|
34 |
+
dropout=config_dict['dropout'],
|
35 |
+
bias=config_dict['bias']
|
36 |
+
)
|
37 |
+
model = GPT(config)
|
38 |
+
|
39 |
+
# Load model weights
|
40 |
+
state_dict = torch.load(model_path, map_location='cpu')
|
41 |
+
model.load_state_dict(state_dict)
|
42 |
+
model.eval()
|
43 |
+
MODEL = model
|
44 |
+
|
45 |
+
# Initialize tokenizer
|
46 |
+
# Download input.txt from the repository
|
47 |
+
try:
|
48 |
+
input_path = hf_hub_download(repo_id="jatingocodeo/shakespeare-decoder", filename="input.txt")
|
49 |
+
with open(input_path, 'r', encoding='utf-8') as f:
|
50 |
+
text = f.read()
|
51 |
+
except:
|
52 |
+
# Fallback to Shakespeare text if input.txt is not in the repo
|
53 |
+
text = """
|
54 |
+
First Citizen:
|
55 |
+
Before we proceed any further, hear me speak.
|
56 |
+
|
57 |
+
All:
|
58 |
+
Speak, speak.
|
59 |
+
|
60 |
+
First Citizen:
|
61 |
+
You are all resolved rather to die than to famish?
|
62 |
+
"""
|
63 |
+
|
64 |
+
CHARS = sorted(list(set(text)))
|
65 |
+
STOI = {ch:i for i,ch in enumerate(CHARS)}
|
66 |
+
ITOS = {i:ch for i,ch in enumerate(CHARS)}
|
67 |
+
|
68 |
+
print("Model and tokenizer loaded successfully!")
|
69 |
+
|
70 |
+
def generate_text(
|
71 |
+
prompt,
|
72 |
+
max_new_tokens=100,
|
73 |
+
temperature=0.8,
|
74 |
+
top_k=50
|
75 |
+
):
|
76 |
+
# Initialize if not already done
|
77 |
+
if MODEL is None:
|
78 |
+
initialize()
|
79 |
+
|
80 |
+
# Encode the prompt
|
81 |
+
encode = lambda s: [STOI[c] for c in s]
|
82 |
+
decode = lambda l: ''.join([ITOS[i] for i in l])
|
83 |
+
|
84 |
+
try:
|
85 |
+
# Convert prompt to tensor
|
86 |
+
x = torch.tensor(encode(prompt), dtype=torch.long)[None,...]
|
87 |
+
|
88 |
+
# Generate
|
89 |
+
with torch.no_grad():
|
90 |
+
y = MODEL.generate(x, max_new_tokens, temperature, top_k)[0]
|
91 |
+
|
92 |
+
# Decode and return
|
93 |
+
generated_text = decode(y.tolist())
|
94 |
+
return generated_text
|
95 |
+
except KeyError:
|
96 |
+
return "Error: The prompt contains characters that are not in the training data. Please use only standard English characters."
|
97 |
+
except Exception as e:
|
98 |
+
return f"Error generating text: {str(e)}"
|
99 |
+
|
100 |
+
# Initialize on startup
|
101 |
+
initialize()
|
102 |
+
|
103 |
+
# Create Gradio interface
|
104 |
+
demo = gr.Interface(
|
105 |
+
fn=generate_text,
|
106 |
+
inputs=[
|
107 |
+
gr.Textbox(
|
108 |
+
label="Prompt",
|
109 |
+
placeholder="Enter your prompt here...",
|
110 |
+
lines=5
|
111 |
+
),
|
112 |
+
gr.Slider(
|
113 |
+
label="Max New Tokens",
|
114 |
+
minimum=10,
|
115 |
+
maximum=500,
|
116 |
+
value=100,
|
117 |
+
step=10
|
118 |
+
),
|
119 |
+
gr.Slider(
|
120 |
+
label="Temperature",
|
121 |
+
minimum=0.1,
|
122 |
+
maximum=2.0,
|
123 |
+
value=0.8,
|
124 |
+
step=0.1
|
125 |
+
),
|
126 |
+
gr.Slider(
|
127 |
+
label="Top-k",
|
128 |
+
minimum=1,
|
129 |
+
maximum=100,
|
130 |
+
value=50,
|
131 |
+
step=1
|
132 |
+
),
|
133 |
+
],
|
134 |
+
outputs=gr.Textbox(label="Generated Text", lines=10),
|
135 |
+
title="Shakespeare GPT",
|
136 |
+
description="""
|
137 |
+
This is a GPT model trained on Shakespeare's text. Enter a prompt and the model will continue it in Shakespeare's style.
|
138 |
+
|
139 |
+
Parameters:
|
140 |
+
- Temperature: Higher values make the output more random, lower values make it more deterministic
|
141 |
+
- Top-k: Number of highest probability tokens to consider at each step
|
142 |
+
- Max New Tokens: Maximum number of tokens to generate
|
143 |
+
""",
|
144 |
+
examples=[
|
145 |
+
["To be, or not to be,", 100, 0.8, 50],
|
146 |
+
["Friends, Romans, countrymen,", 150, 0.7, 40],
|
147 |
+
["Now is the winter of", 200, 0.9, 30],
|
148 |
+
]
|
149 |
+
)
|
150 |
+
|
151 |
+
if __name__ == "__main__":
|
152 |
+
demo.launch()
|