chatbot fix sherika
Browse files
phase/Student_view/chatbot.py
CHANGED
|
@@ -60,22 +60,25 @@ def _reply_with_hf():
|
|
| 60 |
if "client" not in globals():
|
| 61 |
raise RuntimeError("HF client not initialized")
|
| 62 |
|
| 63 |
-
# Build single instruction string FLAN-T5 prefers
|
| 64 |
convo = _format_history_for_flan(st.session_state.get("messages", []))
|
| 65 |
prompt = f"{TUTOR_PROMPT}\n\n{convo}\n\nTutor:"
|
| 66 |
|
| 67 |
try:
|
| 68 |
-
response = client.
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
| 72 |
)
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
elif isinstance(response, str):
|
| 76 |
return response.strip()
|
| 77 |
else:
|
| 78 |
-
raise ValueError(f"Unexpected response format: {
|
| 79 |
except Exception as e:
|
| 80 |
err_text = ''.join(traceback.format_exception_only(type(e), e)).strip()
|
| 81 |
raise RuntimeError(f"Hugging Face API Error: {err_text}")
|
|
|
|
| 60 |
if "client" not in globals():
|
| 61 |
raise RuntimeError("HF client not initialized")
|
| 62 |
|
|
|
|
| 63 |
convo = _format_history_for_flan(st.session_state.get("messages", []))
|
| 64 |
prompt = f"{TUTOR_PROMPT}\n\n{convo}\n\nTutor:"
|
| 65 |
|
| 66 |
try:
|
| 67 |
+
response = client.post(
|
| 68 |
+
json={
|
| 69 |
+
"inputs": prompt,
|
| 70 |
+
"parameters": {"max_new_tokens": 220, "temperature": 0.2},
|
| 71 |
+
}
|
| 72 |
)
|
| 73 |
+
# Handle both list-of-dicts and dict formats
|
| 74 |
+
if isinstance(response, list) and "generated_text" in response[0]:
|
| 75 |
+
return response[0]["generated_text"].strip()
|
| 76 |
+
elif isinstance(response, dict) and "generated_text" in response:
|
| 77 |
+
return response["generated_text"].strip()
|
| 78 |
elif isinstance(response, str):
|
| 79 |
return response.strip()
|
| 80 |
else:
|
| 81 |
+
raise ValueError(f"Unexpected response format: {response}")
|
| 82 |
except Exception as e:
|
| 83 |
err_text = ''.join(traceback.format_exception_only(type(e), e)).strip()
|
| 84 |
raise RuntimeError(f"Hugging Face API Error: {err_text}")
|