recipe
Browse files- README.md +1 -1
- recipe_lora.py +124 -0
README.md
CHANGED
@@ -5,7 +5,7 @@ colorFrom: gray
|
|
5 |
colorTo: green
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.6.0
|
8 |
-
app_file:
|
9 |
pinned: false
|
10 |
python_version: 3.11.0
|
11 |
---
|
|
|
5 |
colorTo: green
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.6.0
|
8 |
+
app_file: recipe_lora.py
|
9 |
pinned: false
|
10 |
python_version: 3.11.0
|
11 |
---
|
recipe_lora.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from transformers import TextStreamer, TextIteratorStreamer
|
4 |
+
|
5 |
+
from threading import Thread
|
6 |
+
|
7 |
+
from unsloth import FastLanguageModel
|
8 |
+
|
9 |
+
load_in_4bit = True
|
10 |
+
|
11 |
+
peft_model_id = "ID2223JR/recipe_model"
|
12 |
+
|
13 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
14 |
+
model_name=peft_model_id,
|
15 |
+
load_in_4bit=load_in_4bit,
|
16 |
+
)
|
17 |
+
FastLanguageModel.for_inference(model)
|
18 |
+
|
19 |
+
|
20 |
+
# Data storage
|
21 |
+
ingredients_list = []
|
22 |
+
|
23 |
+
|
24 |
+
# Function to add ingredient
|
25 |
+
def add_ingredient(ingredient, quantity):
|
26 |
+
if ingredient and int(quantity) > 0:
|
27 |
+
ingredients_list.append(f"{ingredient}, {quantity} grams")
|
28 |
+
return (
|
29 |
+
"\n".join(ingredients_list),
|
30 |
+
gr.update(value="", interactive=True),
|
31 |
+
gr.update(value=None, interactive=True),
|
32 |
+
)
|
33 |
+
|
34 |
+
|
35 |
+
# Function to enable/disable add button
|
36 |
+
def validate_inputs(ingredient, quantity):
|
37 |
+
if ingredient and int(quantity) > 0:
|
38 |
+
return gr.update(interactive=True)
|
39 |
+
return gr.update(interactive=False)
|
40 |
+
|
41 |
+
|
42 |
+
# Function to handle model submission
|
43 |
+
def submit_to_model():
|
44 |
+
if not ingredients_list:
|
45 |
+
return "Ingredients list is empty! Please add ingredients first."
|
46 |
+
|
47 |
+
# Join ingredients into a single prompt
|
48 |
+
prompt = f"Using the following ingredients, suggest a recipe:\n\n" + "\n".join(
|
49 |
+
ingredients_list
|
50 |
+
)
|
51 |
+
ingredients_list.clear()
|
52 |
+
|
53 |
+
messages = [
|
54 |
+
{"role": "user", "content": prompt},
|
55 |
+
]
|
56 |
+
inputs = tokenizer.apply_chat_template(
|
57 |
+
messages,
|
58 |
+
tokenize=True,
|
59 |
+
add_generation_prompt=True, # Must add for generation
|
60 |
+
return_tensors="pt",
|
61 |
+
).to("cuda")
|
62 |
+
|
63 |
+
text_streamer = TextIteratorStreamer(tokenizer, skip_prompt=True)
|
64 |
+
|
65 |
+
generation_kwargs = dict(inputs=inputs, streamer=text_streamer, use_cache=True, temperature=0.3, min_p=0.1)
|
66 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
67 |
+
thread.start()
|
68 |
+
|
69 |
+
|
70 |
+
content = ""
|
71 |
+
for text in text_streamer:
|
72 |
+
print(text)
|
73 |
+
content += text
|
74 |
+
if content.endswith("<|eot_id|>"):
|
75 |
+
content = content.replace("<|eot_id|>", "")
|
76 |
+
yield content
|
77 |
+
|
78 |
+
|
79 |
+
# App
|
80 |
+
def app():
|
81 |
+
with gr.Blocks() as demo:
|
82 |
+
with gr.Row():
|
83 |
+
ingredient_input = gr.Textbox(
|
84 |
+
label="Ingredient", placeholder="Enter ingredient name"
|
85 |
+
)
|
86 |
+
quantity_input = gr.Number(label="Quantity (grams)", value=None)
|
87 |
+
|
88 |
+
add_button = gr.Button("Add Ingredient", interactive=False)
|
89 |
+
output = gr.Textbox(label="Ingredients List", lines=10, interactive=False)
|
90 |
+
|
91 |
+
submit_button = gr.Button("Give me a meal!")
|
92 |
+
|
93 |
+
with gr.Row():
|
94 |
+
model_output = gr.Textbox(
|
95 |
+
label="Recipe Suggestion", lines=10, interactive=False
|
96 |
+
)
|
97 |
+
|
98 |
+
# Validate inputs
|
99 |
+
ingredient_input.change(
|
100 |
+
validate_inputs, [ingredient_input, quantity_input], add_button
|
101 |
+
)
|
102 |
+
quantity_input.change(
|
103 |
+
validate_inputs, [ingredient_input, quantity_input], add_button
|
104 |
+
)
|
105 |
+
|
106 |
+
# Add ingredient logic
|
107 |
+
add_button.click(
|
108 |
+
add_ingredient,
|
109 |
+
[ingredient_input, quantity_input],
|
110 |
+
[output, ingredient_input, quantity_input],
|
111 |
+
)
|
112 |
+
|
113 |
+
# Submit to model logic
|
114 |
+
submit_button.click(
|
115 |
+
submit_to_model,
|
116 |
+
inputs=None, # No inputs required as it uses the global ingredients_list
|
117 |
+
outputs=model_output,
|
118 |
+
)
|
119 |
+
|
120 |
+
return demo
|
121 |
+
|
122 |
+
|
123 |
+
demo = app()
|
124 |
+
demo.launch()
|