File size: 2,322 Bytes
bd41e89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9ea72e
 
 
 
bd41e89
 
 
 
 
 
 
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
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, GPT2TokenizerFast

# Load fine-tuned model
model_path = "saksh-d/recipe-gpt"
tokenizer = GPT2TokenizerFast.from_pretrained(model_path)
model = AutoModelForCausalLM.from_pretrained(model_path)
model.eval()

# Device setup
device = "cuda" if torch.cuda.is_available() else ("mps" if torch.backends.mps.is_available() else "cpu")
model.to(device)

def generate(ingredient_text, temperature, top_k, top_p, max_length):
    # Format ingredients into a list
    ingredients = [line.strip("- ").strip() for line in ingredient_text.strip().splitlines() if line.strip()]
    
    prompt = "<start>\nIngredients:\n"
    for ing in ingredients:
        prompt += f"- {ing}\n"
    prompt += "Directions:\n"

    inputs = tokenizer(prompt, return_tensors="pt")
    input_ids = inputs.input_ids.to(device)
    attention_mask = inputs.attention_mask.to(device)

    with torch.no_grad():
        output_ids = model.generate(
            input_ids,
            attention_mask=attention_mask,
            do_sample=True,
            temperature=temperature,
            top_k=top_k,
            top_p=top_p,
            max_length=max_length,
            eos_token_id=tokenizer.convert_tokens_to_ids("<end>")
        )

    generated = tokenizer.decode(output_ids[0], skip_special_tokens=False)

    if "Directions:" in generated:
        generated = generated.split("Directions:")[1]
    if "<end>" in generated:
        generated = generated.split("<end>")[0]

    return generated.strip()

iface = gr.Interface(
    fn=generate,
    inputs=[
        gr.Textbox(lines=8, label="Ingredients (one per line)"),
        gr.Slider(minimum=0.5, maximum=1.5, value=0.7, step=0.1, label="Temperature (Creativity)"),
        gr.Slider(minimum=0, maximum=100, value=50, step=5, label="Top-k (Word choices at each step)"),
        gr.Slider(minimum=0.5, maximum=1.0, value=0.9, step=0.05, label="Top-p (Nucleus Sampling"),
        gr.Slider(minimum=50, maximum=150, value=90, step=10, label="Recipe Length"),
    ],
    outputs=gr.Textbox(lines=12, label="Generated Recipe Directions"),
    title="Recipe-GPT",
    description="Enter a list of ingredients to generate step-by-step cooking directions. Adjust the sliders for more or less creativity."
)

iface.launch()