Commit all
Browse files- app.py +88 -0
- trained_model_small/model.pt +3 -0
- trained_model_small/tokenizer.json +0 -0
- trained_model_small/tokenizer_config.json +12 -0
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
import sys
|
| 5 |
+
|
| 6 |
+
# Add Training directory to path to import model2
|
| 7 |
+
SCRIPT_DIR = Path(__file__).parent
|
| 8 |
+
TRAINING_DIR = SCRIPT_DIR / "Training"
|
| 9 |
+
sys.path.insert(0, str(TRAINING_DIR))
|
| 10 |
+
|
| 11 |
+
from model2 import miniGPT, GPTConfig
|
| 12 |
+
|
| 13 |
+
# Load model once at startup
|
| 14 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
+
print(f"Loading model on device: {DEVICE}")
|
| 16 |
+
|
| 17 |
+
# Use the trained checkpoint
|
| 18 |
+
checkpoint_path = SCRIPT_DIR / "Training" / "trained_model_54m400k_1epoch"
|
| 19 |
+
if checkpoint_path.exists():
|
| 20 |
+
print(f"Loading checkpoint from {checkpoint_path}")
|
| 21 |
+
trainer = miniGPT.load(str(checkpoint_path), map_location=DEVICE)
|
| 22 |
+
trainer = trainer.to(DEVICE)
|
| 23 |
+
trainer.model.eval()
|
| 24 |
+
print("Model loaded successfully!")
|
| 25 |
+
else:
|
| 26 |
+
raise FileNotFoundError(f"Checkpoint not found at {checkpoint_path}")
|
| 27 |
+
|
| 28 |
+
def generate_recipe(prompt, max_length=300, temperature=0.85, top_k=50, top_p=0.95):
|
| 29 |
+
"""Generate a recipe given a prompt."""
|
| 30 |
+
with torch.no_grad():
|
| 31 |
+
generated = trainer.generate(
|
| 32 |
+
prompt,
|
| 33 |
+
max_length=max_length,
|
| 34 |
+
temperature=temperature,
|
| 35 |
+
top_k=top_k,
|
| 36 |
+
top_p=top_p,
|
| 37 |
+
repetition_penalty=1.2,
|
| 38 |
+
)
|
| 39 |
+
return generated
|
| 40 |
+
|
| 41 |
+
# Gradio interface
|
| 42 |
+
with gr.Blocks(title="Recipe Generator") as demo:
|
| 43 |
+
gr.Markdown("# 🍳 Recipe Generator")
|
| 44 |
+
gr.Markdown("Generate creative recipes using a 54M parameter GPT model trained on recipe data.")
|
| 45 |
+
|
| 46 |
+
with gr.Row():
|
| 47 |
+
with gr.Column(scale=2):
|
| 48 |
+
prompt_input = gr.Textbox(
|
| 49 |
+
label="Recipe Prompt",
|
| 50 |
+
placeholder="e.g., Recipe for chocolate cake:\n\nIngredients:",
|
| 51 |
+
lines=3,
|
| 52 |
+
value="Recipe for chocolate ",
|
| 53 |
+
)
|
| 54 |
+
with gr.Column():
|
| 55 |
+
generate_btn = gr.Button("Generate Recipe", variant="primary")
|
| 56 |
+
|
| 57 |
+
output = gr.Textbox(label="Generated Recipe", lines=10)
|
| 58 |
+
|
| 59 |
+
with gr.Row():
|
| 60 |
+
max_length = gr.Slider(50, 500, 300, step=50, label="Max Length")
|
| 61 |
+
temperature = gr.Slider(0.1, 2.0, 0.85, step=0.1, label="Temperature")
|
| 62 |
+
|
| 63 |
+
with gr.Row():
|
| 64 |
+
top_k = gr.Slider(0, 100, 50, step=5, label="Top-K")
|
| 65 |
+
top_p = gr.Slider(0.0, 1.0, 0.95, step=0.05, label="Top-P")
|
| 66 |
+
|
| 67 |
+
generate_btn.click(
|
| 68 |
+
fn=generate_recipe,
|
| 69 |
+
inputs=[prompt_input, max_length, temperature, top_k, top_p],
|
| 70 |
+
outputs=output,
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# Example prompts
|
| 74 |
+
gr.Examples(
|
| 75 |
+
examples=[
|
| 76 |
+
["Recipe for chocolate ", 300, 0.85, 50, 0.95],
|
| 77 |
+
["Recipe for a delicious apple pie:\n\nIngredients:\n", 300, 0.85, 50, 0.95],
|
| 78 |
+
["Ingredients: flour butter sugar", 300, 0.85, 50, 0.95],
|
| 79 |
+
["Instructions: preheat oven to 350", 300, 0.85, 50, 0.95],
|
| 80 |
+
],
|
| 81 |
+
inputs=[prompt_input, max_length, temperature, top_k, top_p],
|
| 82 |
+
outputs=output,
|
| 83 |
+
fn=generate_recipe,
|
| 84 |
+
cache_examples=False,
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
demo.launch()
|
trained_model_small/model.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:53b2a9f9bcd41224d5b9b1fa4ff3c15dcea18f155d8a99e6b00d8aaca2a2f154
|
| 3 |
+
size 120199835
|
trained_model_small/tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
trained_model_small/tokenizer_config.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_prefix_space": false,
|
| 3 |
+
"backend": "tokenizers",
|
| 4 |
+
"bos_token": "<|endoftext|>",
|
| 5 |
+
"eos_token": "<|endoftext|>",
|
| 6 |
+
"errors": "replace",
|
| 7 |
+
"is_local": false,
|
| 8 |
+
"model_max_length": 1024,
|
| 9 |
+
"pad_token": "<|endoftext|>",
|
| 10 |
+
"tokenizer_class": "GPT2Tokenizer",
|
| 11 |
+
"unk_token": "<|endoftext|>"
|
| 12 |
+
}
|