Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -72,15 +72,39 @@ def parse_raw_text_to_json(raw_text):
|
|
| 72 |
tips = []
|
| 73 |
quote = "Every step forward counts!"
|
| 74 |
|
| 75 |
-
|
|
|
|
|
|
|
| 76 |
for line in lines:
|
| 77 |
line = line.strip()
|
| 78 |
-
if line
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
checklist.append(line[2:].strip())
|
| 80 |
-
elif line.startswith("* "):
|
| 81 |
tips.append(line[2:].strip())
|
| 82 |
-
elif
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
# Fallback if parsing fails
|
| 86 |
if not checklist:
|
|
@@ -107,6 +131,29 @@ def health_check():
|
|
| 107 |
"model_load_status": model_load_status
|
| 108 |
})
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
@app.route("/generate_coaching", methods=["POST"])
|
| 111 |
def generate_coaching():
|
| 112 |
logger.debug("Generate coaching endpoint accessed")
|
|
@@ -132,10 +179,14 @@ def generate_coaching():
|
|
| 132 |
})
|
| 133 |
|
| 134 |
try:
|
| 135 |
-
# Prepare input text
|
| 136 |
input_text = (
|
| 137 |
f"Role: {data['role']}, Project: {data['project_id']}, "
|
| 138 |
-
f"Milestones: {data['milestones']}, Reflection: {data['reflection_log']}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
)
|
| 140 |
|
| 141 |
# Tokenize input
|
|
@@ -153,6 +204,7 @@ def generate_coaching():
|
|
| 153 |
|
| 154 |
# Decode response
|
| 155 |
response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
| 156 |
|
| 157 |
# Try parsing as JSON first
|
| 158 |
try:
|
|
@@ -177,5 +229,4 @@ if __name__ == "__main__":
|
|
| 177 |
# Run Flask app with waitress for production-ready WSGI server
|
| 178 |
from waitress import serve
|
| 179 |
logger.debug("Starting Flask app with Waitress")
|
| 180 |
-
serve(app, host="0.0.0.0", port=7860)
|
| 181 |
-
|
|
|
|
| 72 |
tips = []
|
| 73 |
quote = "Every step forward counts!"
|
| 74 |
|
| 75 |
+
checklist_section = False
|
| 76 |
+
tips_section = False
|
| 77 |
+
|
| 78 |
for line in lines:
|
| 79 |
line = line.strip()
|
| 80 |
+
if not line:
|
| 81 |
+
continue
|
| 82 |
+
if line.lower().startswith("checklist:"):
|
| 83 |
+
checklist_section = True
|
| 84 |
+
tips_section = False
|
| 85 |
+
continue
|
| 86 |
+
elif line.lower().startswith("tips:"):
|
| 87 |
+
checklist_section = False
|
| 88 |
+
tips_section = True
|
| 89 |
+
continue
|
| 90 |
+
elif line.lower().startswith("quote:"):
|
| 91 |
+
checklist_section = False
|
| 92 |
+
tips_section = False
|
| 93 |
+
quote = line[6:].strip() or quote
|
| 94 |
+
continue
|
| 95 |
+
|
| 96 |
+
if checklist_section and line.startswith("- "):
|
| 97 |
checklist.append(line[2:].strip())
|
| 98 |
+
elif tips_section and line.startswith("* "):
|
| 99 |
tips.append(line[2:].strip())
|
| 100 |
+
elif not checklist and not tips and line:
|
| 101 |
+
# If no sections are defined, try to infer structure
|
| 102 |
+
if line.startswith("- "):
|
| 103 |
+
checklist.append(line[2:].strip())
|
| 104 |
+
elif line.startswith("* "):
|
| 105 |
+
tips.append(line[2:].strip())
|
| 106 |
+
else:
|
| 107 |
+
quote = line
|
| 108 |
|
| 109 |
# Fallback if parsing fails
|
| 110 |
if not checklist:
|
|
|
|
| 131 |
"model_load_status": model_load_status
|
| 132 |
})
|
| 133 |
|
| 134 |
+
@app.route("/debug", methods=["POST"])
|
| 135 |
+
def debug():
|
| 136 |
+
logger.debug("Debug endpoint accessed")
|
| 137 |
+
data = request.get_json()
|
| 138 |
+
if not data:
|
| 139 |
+
return jsonify({"error": "Invalid request: JSON data required"}), 400
|
| 140 |
+
|
| 141 |
+
required_fields = ["role", "project_id", "milestones", "reflection_log"]
|
| 142 |
+
missing_fields = [field for field in required_fields if field not in data]
|
| 143 |
+
if missing_fields:
|
| 144 |
+
return jsonify({"error": f"Missing required fields: {missing_fields}"}), 400
|
| 145 |
+
|
| 146 |
+
input_text = (
|
| 147 |
+
f"Role: {data['role']}, Project: {data['project_id']}, "
|
| 148 |
+
f"Milestones: {data['milestones']}, Reflection: {data['reflection_log']}"
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
return jsonify({
|
| 152 |
+
"model_load_status": model_load_status,
|
| 153 |
+
"input_text": input_text,
|
| 154 |
+
"model_ready": model is not None and tokenizer is not None
|
| 155 |
+
})
|
| 156 |
+
|
| 157 |
@app.route("/generate_coaching", methods=["POST"])
|
| 158 |
def generate_coaching():
|
| 159 |
logger.debug("Generate coaching endpoint accessed")
|
|
|
|
| 179 |
})
|
| 180 |
|
| 181 |
try:
|
| 182 |
+
# Prepare input text with a structured prompt to encourage formatted output
|
| 183 |
input_text = (
|
| 184 |
f"Role: {data['role']}, Project: {data['project_id']}, "
|
| 185 |
+
f"Milestones: {data['milestones']}, Reflection: {data['reflection_log']}\n\n"
|
| 186 |
+
"Generate a coaching response in the following format:\n"
|
| 187 |
+
"Checklist:\n- <item1>\n- <item2>\n"
|
| 188 |
+
"Tips:\n* <tip1>\n* <tip2>\n"
|
| 189 |
+
"Quote: <motivational quote>"
|
| 190 |
)
|
| 191 |
|
| 192 |
# Tokenize input
|
|
|
|
| 204 |
|
| 205 |
# Decode response
|
| 206 |
response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 207 |
+
logger.debug(f"Raw model output: {response_text}")
|
| 208 |
|
| 209 |
# Try parsing as JSON first
|
| 210 |
try:
|
|
|
|
| 229 |
# Run Flask app with waitress for production-ready WSGI server
|
| 230 |
from waitress import serve
|
| 231 |
logger.debug("Starting Flask app with Waitress")
|
| 232 |
+
serve(app, host="0.0.0.0", port=7860)
|
|
|