Spaces:
Sleeping
Sleeping
github-actions[bot] commited on
Commit ยท
4f74e1c
1
Parent(s): 3fcd79f
๐ Auto-deploy from GitHub (7fbf150)
Browse files
main.py
CHANGED
|
@@ -255,12 +255,40 @@ def get_client() -> InferenceClient:
|
|
| 255 |
# โโโ HF Serverless Chat Helper (requests-based) โโโโโโโโโโโโโโโ
|
| 256 |
|
| 257 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
def call_hf_chat(
|
| 259 |
messages: List[Dict[str, str]],
|
| 260 |
*,
|
| 261 |
max_tokens: int = 2048,
|
| 262 |
temperature: float = 0.2,
|
| 263 |
top_p: float = 0.9,
|
|
|
|
| 264 |
model: Optional[str] = None,
|
| 265 |
) -> str:
|
| 266 |
"""
|
|
@@ -282,6 +310,7 @@ def call_hf_chat(
|
|
| 282 |
"max_tokens": max_tokens,
|
| 283 |
"temperature": temperature,
|
| 284 |
"top_p": top_p,
|
|
|
|
| 285 |
}
|
| 286 |
|
| 287 |
for attempt in range(3):
|
|
@@ -297,7 +326,8 @@ def call_hf_chat(
|
|
| 297 |
# OpenAI-compatible format: {"choices": [{"message": {"content": "..."}}]}
|
| 298 |
choices = data.get("choices", [])
|
| 299 |
if choices:
|
| 300 |
-
|
|
|
|
| 301 |
|
| 302 |
raise RuntimeError(f"Unexpected HF response format: {data}")
|
| 303 |
|
|
@@ -458,28 +488,21 @@ async def root():
|
|
| 458 |
# โโโ AI Chat Tutor โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 459 |
|
| 460 |
|
| 461 |
-
MATH_TUTOR_SYSTEM_PROMPT = """You are MathPulse AI, a
|
| 462 |
-
- Algebra, Geometry, Calculus, Trigonometry, Statistics, and all math topics
|
| 463 |
-
- Step-by-step problem solving with clear, verifiable explanations
|
| 464 |
-
- Practice problems and concept reinforcement
|
| 465 |
-
- Building confidence in mathematics
|
| 466 |
|
| 467 |
-
|
| 468 |
-
1.
|
| 469 |
-
2.
|
| 470 |
-
3.
|
| 471 |
-
4.
|
| 472 |
-
5. **Admit Uncertainty**: If you are unsure about any step or the problem is ambiguous, explicitly say so. Never guess.
|
| 473 |
-
6. **Cross-Verify**: For computational problems, verify your answer with a different method when possible (e.g., substitute back, use estimation, or solve via an alternate approach).
|
| 474 |
|
| 475 |
-
|
| 476 |
-
-
|
| 477 |
-
-
|
| 478 |
-
-
|
| 479 |
-
-
|
| 480 |
-
-
|
| 481 |
-
-
|
| 482 |
-
- If a problem has multiple valid interpretations, address the most likely one and note alternatives"""
|
| 483 |
|
| 484 |
|
| 485 |
@app.post("/api/chat", response_model=ChatResponse)
|
|
@@ -497,7 +520,7 @@ async def chat_tutor(request: ChatRequest):
|
|
| 497 |
|
| 498 |
# Call HF serverless with retry (handled inside call_hf_chat)
|
| 499 |
try:
|
| 500 |
-
answer = call_hf_chat(messages, max_tokens=
|
| 501 |
except Exception as hf_err:
|
| 502 |
logger.error(f"HF chat failed: {hf_err}")
|
| 503 |
raise HTTPException(
|
|
|
|
| 255 |
# โโโ HF Serverless Chat Helper (requests-based) โโโโโโโโโโโโโโโ
|
| 256 |
|
| 257 |
|
| 258 |
+
def _strip_repetition(text: str, min_chunk: int = 40) -> str:
|
| 259 |
+
"""Remove repeated blocks from model output (a common issue with smaller LLMs)."""
|
| 260 |
+
lines = text.split("\n")
|
| 261 |
+
seen_blocks: list[str] = []
|
| 262 |
+
result_lines: list[str] = []
|
| 263 |
+
i = 0
|
| 264 |
+
while i < len(lines):
|
| 265 |
+
# Try to match a block of 2-4 lines that repeats
|
| 266 |
+
matched = False
|
| 267 |
+
for blen in (4, 3, 2):
|
| 268 |
+
if i + blen > len(lines):
|
| 269 |
+
continue
|
| 270 |
+
block = "\n".join(lines[i : i + blen]).strip()
|
| 271 |
+
if len(block) < min_chunk:
|
| 272 |
+
continue
|
| 273 |
+
if block in seen_blocks:
|
| 274 |
+
# Skip this repeated block
|
| 275 |
+
i += blen
|
| 276 |
+
matched = True
|
| 277 |
+
break
|
| 278 |
+
seen_blocks.append(block)
|
| 279 |
+
if not matched:
|
| 280 |
+
result_lines.append(lines[i])
|
| 281 |
+
i += 1
|
| 282 |
+
return "\n".join(result_lines).strip()
|
| 283 |
+
|
| 284 |
+
|
| 285 |
def call_hf_chat(
|
| 286 |
messages: List[Dict[str, str]],
|
| 287 |
*,
|
| 288 |
max_tokens: int = 2048,
|
| 289 |
temperature: float = 0.2,
|
| 290 |
top_p: float = 0.9,
|
| 291 |
+
repetition_penalty: float = 1.15,
|
| 292 |
model: Optional[str] = None,
|
| 293 |
) -> str:
|
| 294 |
"""
|
|
|
|
| 310 |
"max_tokens": max_tokens,
|
| 311 |
"temperature": temperature,
|
| 312 |
"top_p": top_p,
|
| 313 |
+
"repetition_penalty": repetition_penalty,
|
| 314 |
}
|
| 315 |
|
| 316 |
for attempt in range(3):
|
|
|
|
| 326 |
# OpenAI-compatible format: {"choices": [{"message": {"content": "..."}}]}
|
| 327 |
choices = data.get("choices", [])
|
| 328 |
if choices:
|
| 329 |
+
raw = (choices[0].get("message", {}).get("content", "") or "").strip()
|
| 330 |
+
return _strip_repetition(raw)
|
| 331 |
|
| 332 |
raise RuntimeError(f"Unexpected HF response format: {data}")
|
| 333 |
|
|
|
|
| 488 |
# โโโ AI Chat Tutor โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 489 |
|
| 490 |
|
| 491 |
+
MATH_TUTOR_SYSTEM_PROMPT = """You are MathPulse AI, a friendly and concise expert math tutor for students.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 492 |
|
| 493 |
+
Problem-Solving Protocol:
|
| 494 |
+
1. Restate the problem briefly.
|
| 495 |
+
2. Solve step by step, showing each equation clearly.
|
| 496 |
+
3. State the final answer with a label like "**Final Answer: x = 5**".
|
| 497 |
+
4. Verify once at the end by substituting back (do NOT repeat verification steps).
|
|
|
|
|
|
|
| 498 |
|
| 499 |
+
Rules:
|
| 500 |
+
- Be concise โ aim for under 200 words.
|
| 501 |
+
- Use math notation where helpful (xยฒ, โ, ฯ).
|
| 502 |
+
- Never repeat yourself. Once a step is shown, move forward.
|
| 503 |
+
- If unsure, say so rather than guessing.
|
| 504 |
+
- Encourage the student briefly at the end.
|
| 505 |
+
- If the question is not about math, politely say you can only help with math."""
|
|
|
|
| 506 |
|
| 507 |
|
| 508 |
@app.post("/api/chat", response_model=ChatResponse)
|
|
|
|
| 520 |
|
| 521 |
# Call HF serverless with retry (handled inside call_hf_chat)
|
| 522 |
try:
|
| 523 |
+
answer = call_hf_chat(messages, max_tokens=1024, temperature=0.3, top_p=0.9)
|
| 524 |
except Exception as hf_err:
|
| 525 |
logger.error(f"HF chat failed: {hf_err}")
|
| 526 |
raise HTTPException(
|