test app
Browse files
app.py
CHANGED
@@ -1,5 +1,12 @@
|
|
1 |
import gradio as gr
|
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
# Data storage
|
4 |
ingredients_list = []
|
5 |
|
@@ -7,7 +14,7 @@ ingredients_list = []
|
|
7 |
# Function to add ingredient
|
8 |
def add_ingredient(ingredient, quantity):
|
9 |
if ingredient and quantity > 0:
|
10 |
-
ingredients_list.append(f"{ingredient}
|
11 |
return (
|
12 |
"\n".join(ingredients_list),
|
13 |
gr.update(value="", interactive=True),
|
@@ -22,6 +29,25 @@ def validate_inputs(ingredient, quantity):
|
|
22 |
return gr.update(interactive=False)
|
23 |
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
# App
|
26 |
def app():
|
27 |
with gr.Blocks() as demo:
|
@@ -34,6 +60,12 @@ def app():
|
|
34 |
add_button = gr.Button("Add Ingredient", interactive=False)
|
35 |
output = gr.Textbox(label="Ingredients List", lines=10, interactive=False)
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
# Validate inputs
|
38 |
ingredient_input.change(
|
39 |
validate_inputs, [ingredient_input, quantity_input], add_button
|
@@ -49,6 +81,13 @@ def app():
|
|
49 |
[output, ingredient_input, quantity_input],
|
50 |
)
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
return demo
|
53 |
|
54 |
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
# Load model directly
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
+
|
6 |
+
# Load the LoRA model and tokenizer
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained("ID2223JR/lora_model")
|
8 |
+
model = AutoModelForCausalLM.from_pretrained("ID2223JR/lora_model")
|
9 |
+
|
10 |
# Data storage
|
11 |
ingredients_list = []
|
12 |
|
|
|
14 |
# Function to add ingredient
|
15 |
def add_ingredient(ingredient, quantity):
|
16 |
if ingredient and quantity > 0:
|
17 |
+
ingredients_list.append(f"{ingredient}, {quantity} grams")
|
18 |
return (
|
19 |
"\n".join(ingredients_list),
|
20 |
gr.update(value="", interactive=True),
|
|
|
29 |
return gr.update(interactive=False)
|
30 |
|
31 |
|
32 |
+
# Function to handle model submission
|
33 |
+
def submit_to_model():
|
34 |
+
if not ingredients_list:
|
35 |
+
return "Ingredients list is empty! Please add ingredients first."
|
36 |
+
|
37 |
+
# Join ingredients into a single prompt
|
38 |
+
prompt = f"Using the following ingredients, suggest a recipe:\n\n" + "\n".join(
|
39 |
+
ingredients_list
|
40 |
+
)
|
41 |
+
|
42 |
+
# Tokenize and pass the prompt to the model
|
43 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
44 |
+
outputs = model.generate(**inputs, max_new_tokens=100)
|
45 |
+
|
46 |
+
# Decode the model output
|
47 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
48 |
+
return response
|
49 |
+
|
50 |
+
|
51 |
# App
|
52 |
def app():
|
53 |
with gr.Blocks() as demo:
|
|
|
60 |
add_button = gr.Button("Add Ingredient", interactive=False)
|
61 |
output = gr.Textbox(label="Ingredients List", lines=10, interactive=False)
|
62 |
|
63 |
+
with gr.Row():
|
64 |
+
submit_button = gr.Button("Submit")
|
65 |
+
model_output = gr.Textbox(
|
66 |
+
label="Recipe Suggestion", lines=10, interactive=False
|
67 |
+
)
|
68 |
+
|
69 |
# Validate inputs
|
70 |
ingredient_input.change(
|
71 |
validate_inputs, [ingredient_input, quantity_input], add_button
|
|
|
81 |
[output, ingredient_input, quantity_input],
|
82 |
)
|
83 |
|
84 |
+
# Submit to model logic
|
85 |
+
submit_button.click(
|
86 |
+
submit_to_model,
|
87 |
+
inputs=None, # No inputs required as it uses the global ingredients_list
|
88 |
+
outputs=model_output,
|
89 |
+
)
|
90 |
+
|
91 |
return demo
|
92 |
|
93 |
|