Spaces:
Running
Running
Added model to app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,63 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import torch
|
3 |
-
from transformers import AutoModelForCausalLM, GPT2TokenizerFast
|
4 |
-
|
5 |
-
# Load fine-tuned model
|
6 |
-
model_path = "
|
7 |
-
tokenizer = GPT2TokenizerFast.from_pretrained(model_path)
|
8 |
-
model = AutoModelForCausalLM.from_pretrained(model_path)
|
9 |
-
model.eval()
|
10 |
-
|
11 |
-
# Device setup
|
12 |
-
device = "cuda" if torch.cuda.is_available() else ("mps" if torch.backends.mps.is_available() else "cpu")
|
13 |
-
model.to(device)
|
14 |
-
|
15 |
-
def generate(ingredient_text, temperature, top_k, top_p, max_length):
|
16 |
-
# Format ingredients into a list
|
17 |
-
ingredients = [line.strip("- ").strip() for line in ingredient_text.strip().splitlines() if line.strip()]
|
18 |
-
|
19 |
-
prompt = "<start>\nIngredients:\n"
|
20 |
-
for ing in ingredients:
|
21 |
-
prompt += f"- {ing}\n"
|
22 |
-
prompt += "Directions:\n"
|
23 |
-
|
24 |
-
inputs = tokenizer(prompt, return_tensors="pt")
|
25 |
-
input_ids = inputs.input_ids.to(device)
|
26 |
-
attention_mask = inputs.attention_mask.to(device)
|
27 |
-
|
28 |
-
with torch.no_grad():
|
29 |
-
output_ids = model.generate(
|
30 |
-
input_ids,
|
31 |
-
attention_mask=attention_mask,
|
32 |
-
do_sample=True,
|
33 |
-
temperature=temperature,
|
34 |
-
top_k=top_k,
|
35 |
-
top_p=top_p,
|
36 |
-
max_length=max_length,
|
37 |
-
eos_token_id=tokenizer.convert_tokens_to_ids("<end>")
|
38 |
-
)
|
39 |
-
|
40 |
-
generated = tokenizer.decode(output_ids[0], skip_special_tokens=False)
|
41 |
-
|
42 |
-
if "Directions:" in generated:
|
43 |
-
generated = generated.split("Directions:")[1]
|
44 |
-
if "<end>" in generated:
|
45 |
-
generated = generated.split("<end>")[0]
|
46 |
-
|
47 |
-
return generated.strip()
|
48 |
-
|
49 |
-
iface = gr.Interface(
|
50 |
-
fn=generate,
|
51 |
-
inputs=[
|
52 |
-
gr.Textbox(lines=8, label="Ingredients (one per line)"),
|
53 |
-
gr.Slider(minimum=0.5, maximum=1.5, value=0.7, step=0.1, label="Temperature"),
|
54 |
-
gr.Slider(minimum=0, maximum=100, value=40, step=5, label="Top-k"),
|
55 |
-
gr.Slider(minimum=0.5, maximum=1.0, value=0.9, step=0.05, label="Top-p"),
|
56 |
-
gr.Slider(minimum=50, maximum=150, value=120, step=10, label="Recipe Length"),
|
57 |
-
],
|
58 |
-
outputs=gr.Textbox(lines=12, label="Generated Recipe Directions"),
|
59 |
-
title="Recipe-GPT",
|
60 |
-
description="Enter a list of ingredients to generate step-by-step cooking directions. Adjust the sliders for more or less creativity."
|
61 |
-
)
|
62 |
-
|
63 |
-
iface.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForCausalLM, GPT2TokenizerFast
|
4 |
+
|
5 |
+
# Load fine-tuned model
|
6 |
+
model_path = "saksh-d/recipe-gpt"
|
7 |
+
tokenizer = GPT2TokenizerFast.from_pretrained(model_path)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_path)
|
9 |
+
model.eval()
|
10 |
+
|
11 |
+
# Device setup
|
12 |
+
device = "cuda" if torch.cuda.is_available() else ("mps" if torch.backends.mps.is_available() else "cpu")
|
13 |
+
model.to(device)
|
14 |
+
|
15 |
+
def generate(ingredient_text, temperature, top_k, top_p, max_length):
|
16 |
+
# Format ingredients into a list
|
17 |
+
ingredients = [line.strip("- ").strip() for line in ingredient_text.strip().splitlines() if line.strip()]
|
18 |
+
|
19 |
+
prompt = "<start>\nIngredients:\n"
|
20 |
+
for ing in ingredients:
|
21 |
+
prompt += f"- {ing}\n"
|
22 |
+
prompt += "Directions:\n"
|
23 |
+
|
24 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
25 |
+
input_ids = inputs.input_ids.to(device)
|
26 |
+
attention_mask = inputs.attention_mask.to(device)
|
27 |
+
|
28 |
+
with torch.no_grad():
|
29 |
+
output_ids = model.generate(
|
30 |
+
input_ids,
|
31 |
+
attention_mask=attention_mask,
|
32 |
+
do_sample=True,
|
33 |
+
temperature=temperature,
|
34 |
+
top_k=top_k,
|
35 |
+
top_p=top_p,
|
36 |
+
max_length=max_length,
|
37 |
+
eos_token_id=tokenizer.convert_tokens_to_ids("<end>")
|
38 |
+
)
|
39 |
+
|
40 |
+
generated = tokenizer.decode(output_ids[0], skip_special_tokens=False)
|
41 |
+
|
42 |
+
if "Directions:" in generated:
|
43 |
+
generated = generated.split("Directions:")[1]
|
44 |
+
if "<end>" in generated:
|
45 |
+
generated = generated.split("<end>")[0]
|
46 |
+
|
47 |
+
return generated.strip()
|
48 |
+
|
49 |
+
iface = gr.Interface(
|
50 |
+
fn=generate,
|
51 |
+
inputs=[
|
52 |
+
gr.Textbox(lines=8, label="Ingredients (one per line)"),
|
53 |
+
gr.Slider(minimum=0.5, maximum=1.5, value=0.7, step=0.1, label="Temperature"),
|
54 |
+
gr.Slider(minimum=0, maximum=100, value=40, step=5, label="Top-k"),
|
55 |
+
gr.Slider(minimum=0.5, maximum=1.0, value=0.9, step=0.05, label="Top-p"),
|
56 |
+
gr.Slider(minimum=50, maximum=150, value=120, step=10, label="Recipe Length"),
|
57 |
+
],
|
58 |
+
outputs=gr.Textbox(lines=12, label="Generated Recipe Directions"),
|
59 |
+
title="Recipe-GPT",
|
60 |
+
description="Enter a list of ingredients to generate step-by-step cooking directions. Adjust the sliders for more or less creativity."
|
61 |
+
)
|
62 |
+
|
63 |
+
iface.launch()
|