sumitdotml commited on
Commit
c3ab613
·
verified ·
1 Parent(s): c576ff8

Remove dropdown, single text input, fix device handling

Browse files
Files changed (1) hide show
  1. app.py +25 -44
app.py CHANGED
@@ -30,31 +30,26 @@ SYSTEM_PROMPT = (
30
  "Preservation Notes, Constraint Check."
31
  )
32
 
33
- CONSTRAINTS = [
34
- "vegan",
35
- "dairy_free",
36
- "gluten_free",
37
- "vegetarian",
38
- "nut_free",
39
- "egg_free",
40
- "low_sodium",
41
- ]
42
-
43
- EXAMPLE_TONKOTSU = (
44
  "Adapt this tonkotsu ramen recipe for a vegan diet:\n\n"
45
  "Ingredients: pork bones, pork belly, eggs, wheat noodles, soy sauce, "
46
  "mirin, garlic, ginger, green onions, nori, sesame oil.\n\n"
47
  "Instructions: Boil pork bones for 12 hours for broth. Char pork belly. "
48
- "Soft-boil eggs. Cook wheat noodles. Assemble with toppings."
49
- )
50
 
51
- EXAMPLE_CURRY = (
52
  "Adapt this Japanese curry recipe for gluten-free:\n\n"
53
  "Ingredients: curry roux (contains wheat flour), chicken thighs, potatoes, "
54
  "carrots, onions, rice, soy sauce, mirin, dashi stock.\n\n"
55
  "Instructions: Saut\u00e9 onions, brown chicken, add vegetables, add water and "
56
- "curry roux, simmer 20 minutes. Serve over rice."
57
- )
 
 
 
 
 
 
 
58
 
59
  # ---------------------------------------------------------------------------
60
  # Model loading (once at startup)
@@ -88,22 +83,20 @@ print("Model loaded.")
88
  # ---------------------------------------------------------------------------
89
 
90
 
91
- def generate(recipe: str, constraint: str) -> str:
92
  """Generate an adapted recipe from the fine-tuned model."""
93
- if not recipe.strip():
94
- return "Please paste a recipe above."
95
 
96
- user_content = f"Constraint: {constraint}\n\n{recipe}"
97
  messages = [
98
  {"role": "system", "content": SYSTEM_PROMPT},
99
- {"role": "user", "content": user_content},
100
  ]
101
 
102
  text = tokenizer.apply_chat_template(
103
  messages, tokenize=False, add_generation_prompt=True,
104
  )
105
- inputs = tokenizer(text, return_tensors="pt")
106
- inputs = {k: v.to(model.device) for k, v in inputs.items()}
107
 
108
  gen_kwargs = {
109
  "max_new_tokens": MAX_NEW_TOKENS,
@@ -131,36 +124,24 @@ DESCRIPTION = """\
131
  Fine-tuned [Ministral 8B](https://huggingface.co/mistralai/Ministral-8B-Instruct-2410) \
132
  adapter for dietary-compliant recipe transformation.
133
 
134
- **How it works:** paste a recipe, pick a dietary constraint, and the model \
135
- generates an adapted version with substitution rationale, modified ingredients, \
136
- updated steps, and a compliance check.
137
 
138
  Adapter: [`sumitdotml/robuchan`](https://huggingface.co/sumitdotml/robuchan)
139
  """
140
 
141
- examples = [
142
- [EXAMPLE_TONKOTSU, "vegan"],
143
- [EXAMPLE_CURRY, "gluten_free"],
144
- ]
145
-
146
  demo = gr.Interface(
147
  fn=generate,
148
- inputs=[
149
- gr.Textbox(
150
- label="Recipe",
151
- placeholder="Paste ingredients + instructions here ...",
152
- lines=10,
153
- ),
154
- gr.Dropdown(
155
- choices=CONSTRAINTS,
156
- value="vegan",
157
- label="Dietary Constraint",
158
- ),
159
- ],
160
  outputs=gr.Markdown(label="Adapted Recipe"),
161
  title="Robuchan",
162
  description=DESCRIPTION,
163
- examples=examples,
164
  cache_examples=False,
165
  flagging_mode="never",
166
  )
 
30
  "Preservation Notes, Constraint Check."
31
  )
32
 
33
+ EXAMPLES = [
 
 
 
 
 
 
 
 
 
 
34
  "Adapt this tonkotsu ramen recipe for a vegan diet:\n\n"
35
  "Ingredients: pork bones, pork belly, eggs, wheat noodles, soy sauce, "
36
  "mirin, garlic, ginger, green onions, nori, sesame oil.\n\n"
37
  "Instructions: Boil pork bones for 12 hours for broth. Char pork belly. "
38
+ "Soft-boil eggs. Cook wheat noodles. Assemble with toppings.",
 
39
 
 
40
  "Adapt this Japanese curry recipe for gluten-free:\n\n"
41
  "Ingredients: curry roux (contains wheat flour), chicken thighs, potatoes, "
42
  "carrots, onions, rice, soy sauce, mirin, dashi stock.\n\n"
43
  "Instructions: Saut\u00e9 onions, brown chicken, add vegetables, add water and "
44
+ "curry roux, simmer 20 minutes. Serve over rice.",
45
+
46
+ "I want to make mapo tofu but I'm vegetarian. Here's the original recipe:\n\n"
47
+ "Ingredients: firm tofu, ground pork, doubanjiang, soy sauce, Sichuan "
48
+ "peppercorns, garlic, ginger, green onions, sesame oil, chicken stock.\n\n"
49
+ "Instructions: Fry ground pork until crispy, add doubanjiang and aromatics, "
50
+ "add chicken stock, slide in tofu cubes, simmer 5 minutes, finish with "
51
+ "Sichuan peppercorn oil.",
52
+ ]
53
 
54
  # ---------------------------------------------------------------------------
55
  # Model loading (once at startup)
 
83
  # ---------------------------------------------------------------------------
84
 
85
 
86
+ def generate(message: str) -> str:
87
  """Generate an adapted recipe from the fine-tuned model."""
88
+ if not message.strip():
89
+ return "Please enter a recipe adaptation request."
90
 
 
91
  messages = [
92
  {"role": "system", "content": SYSTEM_PROMPT},
93
+ {"role": "user", "content": message},
94
  ]
95
 
96
  text = tokenizer.apply_chat_template(
97
  messages, tokenize=False, add_generation_prompt=True,
98
  )
99
+ inputs = tokenizer(text, return_tensors="pt").to("cuda")
 
100
 
101
  gen_kwargs = {
102
  "max_new_tokens": MAX_NEW_TOKENS,
 
124
  Fine-tuned [Ministral 8B](https://huggingface.co/mistralai/Ministral-8B-Instruct-2410) \
125
  adapter for dietary-compliant recipe transformation.
126
 
127
+ **How it works:** describe what you want adapted and the dietary constraint, \
128
+ and the model generates a full adapted recipe with substitution rationale, \
129
+ modified ingredients, updated steps, and a compliance check.
130
 
131
  Adapter: [`sumitdotml/robuchan`](https://huggingface.co/sumitdotml/robuchan)
132
  """
133
 
 
 
 
 
 
134
  demo = gr.Interface(
135
  fn=generate,
136
+ inputs=gr.Textbox(
137
+ label="Recipe Adaptation Request",
138
+ placeholder="e.g. Adapt this tonkotsu ramen recipe for a vegan diet:\n\nIngredients: pork bones, pork belly, ...\n\nInstructions: Boil pork bones for 12 hours ...",
139
+ lines=10,
140
+ ),
 
 
 
 
 
 
 
141
  outputs=gr.Markdown(label="Adapted Recipe"),
142
  title="Robuchan",
143
  description=DESCRIPTION,
144
+ examples=EXAMPLES,
145
  cache_examples=False,
146
  flagging_mode="never",
147
  )