File size: 4,904 Bytes
a67bf56
 
 
cb8622e
732a51f
 
 
 
 
 
8c8a70d
a67bf56
732a51f
a67bf56
 
be396a2
9a6d956
 
8c8a70d
732a51f
cb8622e
9a6d956
 
732a51f
 
 
be396a2
8c8a70d
 
 
cb8622e
be396a2
732a51f
cb8622e
1b33596
cb8622e
8c8a70d
a67bf56
732a51f
 
1b33596
 
 
cb8622e
a67bf56
 
8c8a70d
732a51f
 
 
9a6d956
be396a2
8c8a70d
 
 
 
 
 
 
 
be396a2
732a51f
 
 
 
 
4110c99
 
 
732a51f
4110c99
732a51f
4110c99
 
 
732a51f
 
a67bf56
 
 
 
 
 
 
 
 
 
4110c99
a67bf56
732a51f
a67bf56
 
732a51f
 
 
 
a67bf56
 
732a51f
 
 
 
a67bf56
 
 
 
 
 
 
4110c99
a67bf56
 
 
 
732a51f
a67bf56
732a51f
a67bf56
 
 
 
 
 
732a51f
 
 
 
4110c99
 
 
732a51f
 
4110c99
732a51f
4110c99
732a51f
 
 
a67bf56
 
 
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import gradio as gr
import torch
from pathlib import Path
import traceback
import sys

# Force reload of model module
if 'model' in sys.modules:
    del sys.modules['model']

from model import miniGPT

DEVICE = "cuda" if torch.cuda.is_available() else ("mps" if torch.backends.mps.is_available() else "cpu")
print(f"Loading model on device: {DEVICE}")

SCRIPT_DIR = Path(__file__).parent
model_dir = SCRIPT_DIR / "trained_model_small"

trainer = None
error_message = None

if model_dir.exists():
    print(f"Loading model from {model_dir}")
    print(f"Model directory contents:")
    for item in model_dir.iterdir():
        print(f"  - {item.name}")
    try:
        trainer = miniGPT.load(str(model_dir), map_location=DEVICE)
        trainer = trainer.to(DEVICE)
        print("βœ“ Model loaded successfully!")
        print(f"βœ“ Model ready for inference on {DEVICE}!")
    except Exception as e:
        error_message = str(e)
        print(f"βœ— Error loading model: {e}")
        print(f"βœ— Full traceback:")
        print(traceback.format_exc())
        trainer = None
else:
    error_message = f"Model directory not found at {model_dir}"
    print(f"βœ— {error_message}")
    print(f"βœ— Contents of {SCRIPT_DIR}:")
    for item in SCRIPT_DIR.iterdir():
        print(f"  - {item.name}")


def generate_recipe(prompt, max_length=300, temperature=0.85, top_k=50, top_p=0.95):
    if trainer is None:
        if error_message:
            return f"❌ Error: Model not loaded.\n\nDetails: {error_message}"
        return "❌ Error: Model not loaded"
    
    try:
        generated = trainer.generate(
            prompt,
            max_length=max_length,
            temperature=temperature,
            top_k=top_k,
            top_p=top_p,
        )
        return generated
    except Exception as e:
        return f"❌ Error generating recipe: {str(e)}"

with gr.Blocks(title="Recipe Generator", theme=gr.themes.Soft()) as demo:
    gr.Markdown(
        """
        # Welcome eggs 1/4 cup - stupidcookgpt
        
        Generate recipes using a fine-tuned miniGPT model trained on recipe data.
        
        Verified to generate your next delicious meal in 20 seconds (unless it doesn't).
        
        **Model Info:** ~124M parameters, trained on corbt/all-recipes.

        *small print*: ppl this is a friggin demo not some gordon ramsay, pls use common sense and don't sue me if you eat its egg-chocolate-broccoli surprise.
        """
    )
    
    with gr.Row():
        with gr.Column(scale=2):
            prompt_input = gr.Textbox(
                label="Recipe Prompt",
                placeholder="e.g., Recipe for chocolate cake:\n\nIngredients:",
                lines=3,
                value="Recipe for chocolate ",
            )
        with gr.Column():
            generate_btn = gr.Button("Tell the chef...", variant="primary", size="lg")
    
    output = gr.Textbox(label="Generated Recipe", lines=12, max_lines=20)
    
    with gr.Row():
        with gr.Column():
            max_length = gr.Slider(50, 500, 300, step=50, label="Max Length", info="Maximum tokens to generate")
        with gr.Column():
            temperature = gr.Slider(0.1, 2.0, 0.85, step=0.1, label="Temperature", info="Higher = more creative")
    
    with gr.Row():
        with gr.Column():
            top_k = gr.Slider(0, 100, 50, step=5, label="Top-K", info="Sample from top K tokens")
        with gr.Column():
            top_p = gr.Slider(0.0, 1.0, 0.95, step=0.05, label="Top-P", info="Nucleus sampling threshold")
    
    generate_btn.click(
        fn=generate_recipe,
        inputs=[prompt_input, max_length, temperature, top_k, top_p],
        outputs=output,
    )
    
    # example prompts
    gr.Examples(
        examples=[
            ["Recipe for chocolate ", 300, 0.85, 50, 0.95],
            ["Recipe for a delicious apple pie:\n\nIngredients:\n", 300, 0.85, 50, 0.95],
            ["Ingredients: flour butter sugar eggs", 300, 0.85, 50, 0.95],
            ["Instructions: preheat oven to 350", 300, 0.85, 50, 0.95],
            ["How to make a perfect coffee cake", 300, 0.85, 50, 0.95],
        ],
        inputs=[prompt_input, max_length, temperature, top_k, top_p],
        outputs=output,
        fn=generate_recipe,
        cache_examples=False,
    )
    
    gr.Markdown(
        """
        ### How to use:
        1. Enter a prompt 4 the chef
        2. Adjust chef parameters if desired (make it gordon ramsay when Temp=2 or a chill cook with Temp=0.4)
        3. Click "Tell the chef..." to create a new recipe
        
        ### Tips:
        - **Temperature**: Higher values (1.0+) make the chef more creative but probably more sus.
        - **Top-K**: Limits sampling to top K most likely tokens
        - **Top-P**: Only considers tokens with cumulative probability <= P
        """
    )


if __name__ == "__main__":
    demo.launch()