Spaces:
Running on T4
Running on T4
Commit ·
5c8d948
1
Parent(s): fe43813
fix: Clean up LLM plant care tips output
Browse filesIssues fixed:
- Remove leaked prompt instructions ('Keep it concise', etc)
- Bold plant names in teal color for better readability
- Improved prompt to avoid instruction leakage
- Better formatting with HTML for display
- Cleaner header and spinner text
- Teal background for tips container
Result: Clean, professional plant care output with bold plant names
- app.py +9 -8
- src/backend/chatbot.py +48 -15
app.py
CHANGED
|
@@ -735,13 +735,10 @@ if page == "Garden Optimization":
|
|
| 735 |
# Add vertical space
|
| 736 |
add_vertical_space(4)
|
| 737 |
# show plant care tips
|
| 738 |
-
st.header("Plant
|
| 739 |
-
with st.spinner("
|
| 740 |
-
st.write(
|
| 741 |
-
"Here are some plant care tips for your plants. Good luck!"
|
| 742 |
-
)
|
| 743 |
if st.session_state.demo_lite:
|
| 744 |
-
st.session_state.plant_care_tips = "Plant care tips are not available in
|
| 745 |
else:
|
| 746 |
# if 'plant_care_tips' not in st.session_state:
|
| 747 |
st.session_state.plant_care_tips = get_plant_care_tips(
|
|
@@ -749,8 +746,12 @@ if page == "Garden Optimization":
|
|
| 749 |
st.session_state.model,
|
| 750 |
st.session_state.demo_lite,
|
| 751 |
)
|
| 752 |
-
|
| 753 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 754 |
|
| 755 |
|
| 756 |
if page == "About":
|
|
|
|
| 735 |
# Add vertical space
|
| 736 |
add_vertical_space(4)
|
| 737 |
# show plant care tips
|
| 738 |
+
st.header("Plant Care Tips")
|
| 739 |
+
with st.spinner("Generating plant care tips..."):
|
|
|
|
|
|
|
|
|
|
| 740 |
if st.session_state.demo_lite:
|
| 741 |
+
st.session_state.plant_care_tips = "Plant care tips are not available in lite demo mode. Select Llama3.2-1b_CPP for full functionality."
|
| 742 |
else:
|
| 743 |
# if 'plant_care_tips' not in st.session_state:
|
| 744 |
st.session_state.plant_care_tips = get_plant_care_tips(
|
|
|
|
| 746 |
st.session_state.model,
|
| 747 |
st.session_state.demo_lite,
|
| 748 |
)
|
| 749 |
+
# Use markdown container with teal background for better formatting
|
| 750 |
+
st.markdown(f"""
|
| 751 |
+
<div style="background-color: rgba(32,178,170,0.15); padding: 20px; border-radius: 8px; border-left: 4px solid #20B2AA;">
|
| 752 |
+
{st.session_state.plant_care_tips}
|
| 753 |
+
</div>
|
| 754 |
+
""", unsafe_allow_html=True)
|
| 755 |
|
| 756 |
|
| 757 |
if page == "About":
|
src/backend/chatbot.py
CHANGED
|
@@ -217,25 +217,25 @@ def get_plant_care_tips(plant_list, model, demo_lite):
|
|
| 217 |
if len(st.session_state.input_plants_raw) > 8:
|
| 218 |
plant_names += f" (and {len(st.session_state.input_plants_raw) - 8} more)"
|
| 219 |
|
| 220 |
-
#
|
| 221 |
-
template = "You are a gardening expert.
|
| 222 |
-
text = f"""
|
| 223 |
|
| 224 |
-
|
| 225 |
-
- Sunlight
|
| 226 |
-
- Watering
|
| 227 |
- USDA hardiness zones
|
| 228 |
-
- One
|
| 229 |
|
| 230 |
-
|
| 231 |
|
| 232 |
-
|
| 233 |
-
Sunlight:
|
| 234 |
-
Water:
|
| 235 |
-
Zones:
|
| 236 |
-
Tip:
|
| 237 |
|
| 238 |
-
|
| 239 |
|
| 240 |
plant_care_tips = chat_response(template, text, model, demo_lite)
|
| 241 |
print("Plant care tips response:", plant_care_tips)
|
|
@@ -244,9 +244,42 @@ Do NOT repeat yourself. Do NOT add extra headers or explanations. Just the plant
|
|
| 244 |
if plant_care_tips is None:
|
| 245 |
return "Error: Could not generate plant care tips. Please try again or select a different model."
|
| 246 |
|
| 247 |
-
# Clean up the response
|
| 248 |
plant_care_tips = plant_care_tips.strip()
|
| 249 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
return plant_care_tips
|
| 251 |
|
| 252 |
|
|
|
|
| 217 |
if len(st.session_state.input_plants_raw) > 8:
|
| 218 |
plant_names += f" (and {len(st.session_state.input_plants_raw) - 8} more)"
|
| 219 |
|
| 220 |
+
# Clear prompt that won't leak instructions into output
|
| 221 |
+
template = "You are a helpful gardening expert."
|
| 222 |
+
text = f"""Provide care tips for these plants: {plant_names}
|
| 223 |
|
| 224 |
+
For each plant, give:
|
| 225 |
+
- Sunlight requirements
|
| 226 |
+
- Watering schedule
|
| 227 |
- USDA hardiness zones
|
| 228 |
+
- One practical tip
|
| 229 |
|
| 230 |
+
Format each plant like this example:
|
| 231 |
|
| 232 |
+
Tomatoes
|
| 233 |
+
Sunlight: Full sun (6-8 hours)
|
| 234 |
+
Water: Deep watering 2-3 times per week
|
| 235 |
+
Zones: 3-11
|
| 236 |
+
Tip: Prune suckers for larger fruit
|
| 237 |
|
| 238 |
+
Now provide tips for my plants. Start immediately with the first plant name."""
|
| 239 |
|
| 240 |
plant_care_tips = chat_response(template, text, model, demo_lite)
|
| 241 |
print("Plant care tips response:", plant_care_tips)
|
|
|
|
| 244 |
if plant_care_tips is None:
|
| 245 |
return "Error: Could not generate plant care tips. Please try again or select a different model."
|
| 246 |
|
| 247 |
+
# Clean up the response - remove any leaked instructions
|
| 248 |
plant_care_tips = plant_care_tips.strip()
|
| 249 |
|
| 250 |
+
# Remove common leaked phrases
|
| 251 |
+
phrases_to_remove = [
|
| 252 |
+
"Keep it concise",
|
| 253 |
+
"Keep it BRIEF",
|
| 254 |
+
"Do NOT repeat yourself",
|
| 255 |
+
"Do NOT add extra headers",
|
| 256 |
+
"Just the plant tips",
|
| 257 |
+
"Start immediately with the first plant name"
|
| 258 |
+
]
|
| 259 |
+
for phrase in phrases_to_remove:
|
| 260 |
+
if phrase in plant_care_tips:
|
| 261 |
+
plant_care_tips = plant_care_tips.replace(phrase, "")
|
| 262 |
+
|
| 263 |
+
# Bold the plant names by detecting lines that are likely plant names
|
| 264 |
+
# (lines with no colons that come before lines with colons)
|
| 265 |
+
# Use HTML <strong> tags since we'll be displaying in an HTML div
|
| 266 |
+
lines = plant_care_tips.split('\n')
|
| 267 |
+
formatted_lines = []
|
| 268 |
+
for i, line in enumerate(lines):
|
| 269 |
+
line = line.strip()
|
| 270 |
+
if not line:
|
| 271 |
+
formatted_lines.append('<br>')
|
| 272 |
+
continue
|
| 273 |
+
|
| 274 |
+
# If this line has no colon and the next line has a colon, it's likely a plant name
|
| 275 |
+
if ':' not in line and i + 1 < len(lines) and ':' in lines[i + 1]:
|
| 276 |
+
# Bold the plant name with HTML
|
| 277 |
+
formatted_lines.append(f"<strong style='color: #20B2AA; font-size: 1.1em;'>{line}</strong>")
|
| 278 |
+
else:
|
| 279 |
+
formatted_lines.append(line)
|
| 280 |
+
|
| 281 |
+
plant_care_tips = '<br>'.join(formatted_lines)
|
| 282 |
+
|
| 283 |
return plant_care_tips
|
| 284 |
|
| 285 |
|