jedeland commited on
Commit
596d555
·
1 Parent(s): 1007453

model dropdown

Browse files
Files changed (3) hide show
  1. lora.py +1 -1
  2. recipe_lora.py +27 -23
  3. requirements.txt +0 -2
lora.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
 
3
- from transformers import TextStreamer, TextIteratorStreamer
4
 
5
  from threading import Thread
6
 
 
1
  import gradio as gr
2
 
3
+ from transformers import TextIteratorStreamer
4
 
5
  from threading import Thread
6
 
recipe_lora.py CHANGED
@@ -1,27 +1,28 @@
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")
@@ -31,20 +32,24 @@ def add_ingredient(ingredient, quantity):
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
  )
@@ -66,7 +71,6 @@ def submit_to_model():
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)
@@ -75,8 +79,6 @@ def submit_to_model():
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():
@@ -91,11 +93,15 @@ def app():
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
  )
@@ -103,17 +109,15 @@ def app():
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
 
 
1
  import gradio as gr
2
 
3
+ from transformers import TextIteratorStreamer
 
4
  from threading import Thread
 
5
  from unsloth import FastLanguageModel
6
 
7
  load_in_4bit = True
8
 
9
+ recipe_model_id = "ID2223JR/recipe_model"
10
+ lora_model_id = "ID2223JR/lora_model"
11
 
12
+ recipe_model, recipe_tokenizer = FastLanguageModel.from_pretrained(
13
+ model_name=recipe_model_id,
14
  load_in_4bit=load_in_4bit,
15
  )
16
+ FastLanguageModel.for_inference(recipe_model)
17
 
18
+ lora_model, lora_tokenizer = FastLanguageModel.from_pretrained(
19
+ model_name=lora_model_id,
20
+ load_in_4bit=load_in_4bit,
21
+ )
22
+ FastLanguageModel.for_inference(lora_model)
23
 
 
24
  ingredients_list = []
25
 
 
 
26
  def add_ingredient(ingredient, quantity):
27
  if ingredient and int(quantity) > 0:
28
  ingredients_list.append(f"{ingredient}, {quantity} grams")
 
32
  gr.update(value=None, interactive=True),
33
  )
34
 
 
 
35
  def validate_inputs(ingredient, quantity):
36
  if ingredient and int(quantity) > 0:
37
  return gr.update(interactive=True)
38
  return gr.update(interactive=False)
39
 
40
+ def submit_to_model(selected_model):
 
 
41
  if not ingredients_list:
42
  return "Ingredients list is empty! Please add ingredients first."
43
 
44
+ if selected_model == "Recipe Model":
45
+ model = recipe_model
46
+ tokenizer = recipe_tokenizer
47
+ elif selected_model == "LoRA Model":
48
+ model = lora_model
49
+ tokenizer = lora_tokenizer
50
+ else:
51
+ return "Invalid model selected!"
52
+
53
  prompt = f"Using the following ingredients, suggest a recipe:\n\n" + "\n".join(
54
  ingredients_list
55
  )
 
71
  thread = Thread(target=model.generate, kwargs=generation_kwargs)
72
  thread.start()
73
 
 
74
  content = ""
75
  for text in text_streamer:
76
  print(text)
 
79
  content = content.replace("<|eot_id|>", "")
80
  yield content
81
 
 
 
82
  def app():
83
  with gr.Blocks() as demo:
84
  with gr.Row():
 
93
  submit_button = gr.Button("Give me a meal!")
94
 
95
  with gr.Row():
96
+ model_dropdown = gr.Dropdown(
97
+ label="Choose Model",
98
+ choices=["Recipe Model", "LoRA Model"],
99
+ value="Recipe Model"
100
+ )
101
  model_output = gr.Textbox(
102
  label="Recipe Suggestion", lines=10, interactive=False
103
  )
104
 
 
105
  ingredient_input.change(
106
  validate_inputs, [ingredient_input, quantity_input], add_button
107
  )
 
109
  validate_inputs, [ingredient_input, quantity_input], add_button
110
  )
111
 
 
112
  add_button.click(
113
  add_ingredient,
114
  [ingredient_input, quantity_input],
115
  [output, ingredient_input, quantity_input],
116
  )
117
 
 
118
  submit_button.click(
119
  submit_to_model,
120
+ inputs=model_dropdown,
121
  outputs=model_output,
122
  )
123
 
requirements.txt CHANGED
@@ -1,4 +1,3 @@
1
- huggingface_hub==0.25.2
2
  unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git
3
  gradio==5.1.0
4
  bitsandbytes
@@ -7,4 +6,3 @@ trl<0.9.0
7
  peft
8
  accelerate
9
  transformers>=4.45.1
10
- llama-cpp-python==0.3.2
 
 
1
  unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git
2
  gradio==5.1.0
3
  bitsandbytes
 
6
  peft
7
  accelerate
8
  transformers>=4.45.1