Upload app/api/main.py with huggingface_hub
Browse files- app/api/main.py +44 -29
app/api/main.py
CHANGED
|
@@ -13,6 +13,18 @@ from ..agents.peer_learning_agent import PeerLearningAgent
|
|
| 13 |
from ..agents.hand_gesture_agent import HandGestureAgent, GestureSignalMapper
|
| 14 |
from datetime import datetime
|
| 15 |
import uuid
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
api = Blueprint('api', __name__)
|
| 18 |
|
|
@@ -37,7 +49,7 @@ def start_session():
|
|
| 37 |
subtopic = data.get('subtopic', '')
|
| 38 |
|
| 39 |
orchestrator = get_orchestrator(user_id)
|
| 40 |
-
session = orchestrator.start_session(topic, subtopic)
|
| 41 |
|
| 42 |
return jsonify({
|
| 43 |
'session_id': session.session_id,
|
|
@@ -65,14 +77,17 @@ def update_session():
|
|
| 65 |
captured_doubt = data.get('captured_doubt')
|
| 66 |
|
| 67 |
orchestrator = get_orchestrator(user_id)
|
| 68 |
-
orchestrator.update_session(behavioral_data, captured_doubt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
return jsonify({
|
| 71 |
'status': 'updated',
|
| 72 |
-
'active_predictions': orchestrator.state.active_predictions[-3:],
|
| 73 |
-
'confusion_level':
|
| 74 |
-
orchestrator.state.current_session.behavioral_signals[-5:]
|
| 75 |
-
) if orchestrator.state.current_session else 0
|
| 76 |
})
|
| 77 |
|
| 78 |
|
|
@@ -238,7 +253,7 @@ def get_due_reviews():
|
|
| 238 |
topic = request.args.get('topic')
|
| 239 |
|
| 240 |
agent = RecallAgent(user_id)
|
| 241 |
-
recalls = agent.get_due_recalls(topic)
|
| 242 |
|
| 243 |
return jsonify({
|
| 244 |
'due_count': len(recalls),
|
|
@@ -265,7 +280,7 @@ def complete_review():
|
|
| 265 |
quality = data.get('quality', 3)
|
| 266 |
|
| 267 |
agent = RecallAgent(user_id)
|
| 268 |
-
result = agent.complete_review(card_id, quality)
|
| 269 |
|
| 270 |
if result:
|
| 271 |
return jsonify({
|
|
@@ -273,7 +288,7 @@ def complete_review():
|
|
| 273 |
'quality': result.quality,
|
| 274 |
'xp_earned': result.xp_earned,
|
| 275 |
'next_interval': result.new_interval,
|
| 276 |
-
'next_review': result.next_review.isoformat()
|
| 277 |
})
|
| 278 |
|
| 279 |
return jsonify({'error': 'Card not found'}), 404
|
|
@@ -300,16 +315,16 @@ def get_peer_insights():
|
|
| 300 |
topic = request.args.get('topic', 'General')
|
| 301 |
|
| 302 |
agent = PeerLearningAgent('anonymous')
|
| 303 |
-
insights = agent.get_peer_insights(topic)
|
| 304 |
|
| 305 |
return jsonify({
|
| 306 |
'insights': [
|
| 307 |
{
|
| 308 |
-
'type': i.insight_type,
|
| 309 |
-
'content': i.content,
|
| 310 |
-
'related_topics': i.related_topics,
|
| 311 |
-
'confidence': i.confidence,
|
| 312 |
-
'peer_count': i.peer_count
|
| 313 |
}
|
| 314 |
for i in insights
|
| 315 |
]
|
|
@@ -323,16 +338,16 @@ def get_peer_doubts():
|
|
| 323 |
limit = int(request.args.get('limit', 10))
|
| 324 |
|
| 325 |
agent = PeerLearningAgent('anonymous')
|
| 326 |
-
doubts = agent.get_peer_doubts(topic, limit)
|
| 327 |
|
| 328 |
return jsonify({
|
| 329 |
'doubts': [
|
| 330 |
{
|
| 331 |
-
'id': d.doubt_id,
|
| 332 |
-
'content': d.content,
|
| 333 |
-
'resolved': d.resolved,
|
| 334 |
-
'upvotes': d.upvotes,
|
| 335 |
-
'similarity': d.similarity_score
|
| 336 |
}
|
| 337 |
for d in doubts
|
| 338 |
]
|
|
@@ -343,10 +358,10 @@ def get_peer_doubts():
|
|
| 343 |
def get_trending():
|
| 344 |
"""Get trending topics"""
|
| 345 |
agent = PeerLearningAgent('anonymous')
|
| 346 |
-
trending = agent.get_trending_topics()
|
| 347 |
|
| 348 |
return jsonify({
|
| 349 |
-
'trending': trending
|
| 350 |
})
|
| 351 |
|
| 352 |
|
|
@@ -617,16 +632,16 @@ def llm_query():
|
|
| 617 |
models=models
|
| 618 |
)
|
| 619 |
|
| 620 |
-
responses = orchestrator.query_parallel(request_obj)
|
| 621 |
|
| 622 |
return jsonify({
|
| 623 |
'responses': [
|
| 624 |
{
|
| 625 |
-
'provider': r.provider.value,
|
| 626 |
-
'content': r.content,
|
| 627 |
-
'success': r.success,
|
| 628 |
-
'error': r.error,
|
| 629 |
-
'latency_ms': r.latency_ms
|
| 630 |
}
|
| 631 |
for r in responses
|
| 632 |
],
|
|
|
|
| 13 |
from ..agents.hand_gesture_agent import HandGestureAgent, GestureSignalMapper
|
| 14 |
from datetime import datetime
|
| 15 |
import uuid
|
| 16 |
+
import asyncio
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def run_async(coro):
|
| 20 |
+
"""Run async coroutine in sync context"""
|
| 21 |
+
try:
|
| 22 |
+
loop = asyncio.get_running_loop()
|
| 23 |
+
# If already in async context, create new event loop
|
| 24 |
+
return asyncio.run(coro)
|
| 25 |
+
except RuntimeError:
|
| 26 |
+
# No running loop, safe to use asyncio.run
|
| 27 |
+
return asyncio.run(coro)
|
| 28 |
|
| 29 |
api = Blueprint('api', __name__)
|
| 30 |
|
|
|
|
| 49 |
subtopic = data.get('subtopic', '')
|
| 50 |
|
| 51 |
orchestrator = get_orchestrator(user_id)
|
| 52 |
+
session = run_async(orchestrator.start_session(topic, subtopic))
|
| 53 |
|
| 54 |
return jsonify({
|
| 55 |
'session_id': session.session_id,
|
|
|
|
| 77 |
captured_doubt = data.get('captured_doubt')
|
| 78 |
|
| 79 |
orchestrator = get_orchestrator(user_id)
|
| 80 |
+
run_async(orchestrator.update_session(behavioral_data, captured_doubt))
|
| 81 |
+
|
| 82 |
+
confusion_score = 0
|
| 83 |
+
if orchestrator.state.current_session and orchestrator.state.current_session.behavioral_signals:
|
| 84 |
+
signals = orchestrator.state.current_session.behavioral_signals[-5:]
|
| 85 |
+
confusion_score = orchestrator.behavioral_agent.calculate_confusion_score(signals)
|
| 86 |
|
| 87 |
return jsonify({
|
| 88 |
'status': 'updated',
|
| 89 |
+
'active_predictions': [p.predicted_doubt for p in orchestrator.state.active_predictions[-3:]],
|
| 90 |
+
'confusion_level': confusion_score
|
|
|
|
|
|
|
| 91 |
})
|
| 92 |
|
| 93 |
|
|
|
|
| 253 |
topic = request.args.get('topic')
|
| 254 |
|
| 255 |
agent = RecallAgent(user_id)
|
| 256 |
+
recalls = run_async(agent.get_due_recalls(topic))
|
| 257 |
|
| 258 |
return jsonify({
|
| 259 |
'due_count': len(recalls),
|
|
|
|
| 280 |
quality = data.get('quality', 3)
|
| 281 |
|
| 282 |
agent = RecallAgent(user_id)
|
| 283 |
+
result = run_async(agent.complete_review(card_id, quality))
|
| 284 |
|
| 285 |
if result:
|
| 286 |
return jsonify({
|
|
|
|
| 288 |
'quality': result.quality,
|
| 289 |
'xp_earned': result.xp_earned,
|
| 290 |
'next_interval': result.new_interval,
|
| 291 |
+
'next_review': result.next_review.isoformat() if hasattr(result, 'next_review') else None
|
| 292 |
})
|
| 293 |
|
| 294 |
return jsonify({'error': 'Card not found'}), 404
|
|
|
|
| 315 |
topic = request.args.get('topic', 'General')
|
| 316 |
|
| 317 |
agent = PeerLearningAgent('anonymous')
|
| 318 |
+
insights = run_async(agent.get_peer_insights(topic))
|
| 319 |
|
| 320 |
return jsonify({
|
| 321 |
'insights': [
|
| 322 |
{
|
| 323 |
+
'type': i.insight_type if hasattr(i, 'insight_type') else 'general',
|
| 324 |
+
'content': i.content if hasattr(i, 'content') else str(i),
|
| 325 |
+
'related_topics': i.related_topics if hasattr(i, 'related_topics') else [],
|
| 326 |
+
'confidence': i.confidence if hasattr(i, 'confidence') else 0.5,
|
| 327 |
+
'peer_count': i.peer_count if hasattr(i, 'peer_count') else 1
|
| 328 |
}
|
| 329 |
for i in insights
|
| 330 |
]
|
|
|
|
| 338 |
limit = int(request.args.get('limit', 10))
|
| 339 |
|
| 340 |
agent = PeerLearningAgent('anonymous')
|
| 341 |
+
doubts = run_async(agent.get_peer_doubts(topic, limit))
|
| 342 |
|
| 343 |
return jsonify({
|
| 344 |
'doubts': [
|
| 345 |
{
|
| 346 |
+
'id': d.doubt_id if hasattr(d, 'doubt_id') else str(d),
|
| 347 |
+
'content': d.content if hasattr(d, 'content') else str(d),
|
| 348 |
+
'resolved': d.resolved if hasattr(d, 'resolved') else False,
|
| 349 |
+
'upvotes': d.upvotes if hasattr(d, 'upvotes') else 0,
|
| 350 |
+
'similarity': d.similarity_score if hasattr(d, 'similarity_score') else 0.5
|
| 351 |
}
|
| 352 |
for d in doubts
|
| 353 |
]
|
|
|
|
| 358 |
def get_trending():
|
| 359 |
"""Get trending topics"""
|
| 360 |
agent = PeerLearningAgent('anonymous')
|
| 361 |
+
trending = run_async(agent.get_trending_topics())
|
| 362 |
|
| 363 |
return jsonify({
|
| 364 |
+
'trending': trending if isinstance(trending, list) else []
|
| 365 |
})
|
| 366 |
|
| 367 |
|
|
|
|
| 632 |
models=models
|
| 633 |
)
|
| 634 |
|
| 635 |
+
responses = run_async(orchestrator.query_parallel(request_obj))
|
| 636 |
|
| 637 |
return jsonify({
|
| 638 |
'responses': [
|
| 639 |
{
|
| 640 |
+
'provider': r.provider.value if hasattr(r, 'provider') else 'unknown',
|
| 641 |
+
'content': r.content if hasattr(r, 'content') else str(r),
|
| 642 |
+
'success': r.success if hasattr(r, 'success') else True,
|
| 643 |
+
'error': r.error if hasattr(r, 'error') else None,
|
| 644 |
+
'latency_ms': r.latency_ms if hasattr(r, 'latency_ms') else 0
|
| 645 |
}
|
| 646 |
for r in responses
|
| 647 |
],
|