saemstunes commited on
Commit
e99e7d4
·
verified ·
1 Parent(s): abbebe0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -15
app.py CHANGED
@@ -357,6 +357,7 @@ def create_gradio_interface():
357
 
358
  def setup_api_endpoints(demo):
359
  from fastapi import FastAPI, HTTPException
 
360
  from pydantic import BaseModel
361
  from typing import Optional
362
 
@@ -372,8 +373,9 @@ def setup_api_endpoints(demo):
372
  timestamp: str
373
  model_used: str
374
 
 
375
  @demo.app.post("/api/chat")
376
- async def api_chat(request: ChatRequest):
377
  try:
378
  if not request.message.strip():
379
  raise HTTPException(status_code=400, detail="Message cannot be empty")
@@ -386,13 +388,13 @@ def setup_api_endpoints(demo):
386
  response = ai_system.process_query(request.message, request.user_id, request.conversation_id)
387
  processing_time = time.time() - start_time
388
 
389
- return ChatResponse(
390
- response=response,
391
- processing_time=processing_time,
392
- conversation_id=request.conversation_id or f"conv_{int(time.time())}",
393
- timestamp=datetime.now().isoformat(),
394
- model_used=Config.MODEL_NAME
395
- )
396
 
397
  except HTTPException:
398
  raise
@@ -400,32 +402,46 @@ def setup_api_endpoints(demo):
400
  logger.error(f"API chat error: {e}")
401
  raise HTTPException(status_code=500, detail="Internal server error")
402
 
 
403
  @demo.app.get("/api/health")
404
- async def api_health():
405
- return get_system_status()
 
 
 
 
 
 
 
 
406
 
407
  @demo.app.get("/api/models")
408
- async def api_models():
409
- return {
410
  "available_models": ["microsoft/Phi-3.5-mini-instruct"],
411
  "current_model": Config.MODEL_NAME,
412
  "quantization": "Q4_K_M",
413
  "context_length": 4096,
414
  "parameters": "3.8B"
415
  }
 
416
 
417
  @demo.app.get("/api/stats")
418
- async def api_stats():
419
  if not monitor:
420
- return {"error": "Monitoring system not available"}
 
 
 
421
 
422
- return {
423
  "total_requests": len(monitor.inference_metrics),
424
  "average_response_time": monitor.get_average_response_time(),
425
  "error_rate": monitor.get_error_rate(),
426
  "uptime": monitor.get_uptime(),
427
  "system_health": get_system_status()
428
  }
 
429
 
430
  if __name__ == "__main__":
431
  logger.info("🎵 Starting Saem's Tunes AI on Hugging Face Spaces...")
 
357
 
358
  def setup_api_endpoints(demo):
359
  from fastapi import FastAPI, HTTPException
360
+ from fastapi.responses import JSONResponse
361
  from pydantic import BaseModel
362
  from typing import Optional
363
 
 
373
  timestamp: str
374
  model_used: str
375
 
376
+ # FIX: Remove async from endpoints that don't need it
377
  @demo.app.post("/api/chat")
378
+ def api_chat(request: ChatRequest): # Remove async
379
  try:
380
  if not request.message.strip():
381
  raise HTTPException(status_code=400, detail="Message cannot be empty")
 
388
  response = ai_system.process_query(request.message, request.user_id, request.conversation_id)
389
  processing_time = time.time() - start_time
390
 
391
+ return JSONResponse(content={
392
+ "response": response,
393
+ "processing_time": processing_time,
394
+ "conversation_id": request.conversation_id or f"conv_{int(time.time())}",
395
+ "timestamp": datetime.now().isoformat(),
396
+ "model_used": Config.MODEL_NAME
397
+ })
398
 
399
  except HTTPException:
400
  raise
 
402
  logger.error(f"API chat error: {e}")
403
  raise HTTPException(status_code=500, detail="Internal server error")
404
 
405
+ # FIX: Remove async and use direct dict return
406
  @demo.app.get("/api/health")
407
+ def api_health(): # Remove async
408
+ try:
409
+ status_data = get_system_status()
410
+ return status_data # FastAPI automatically converts dict to JSON
411
+ except Exception as e:
412
+ logger.error(f"Health endpoint error: {e}")
413
+ return JSONResponse(
414
+ content={"status": "error", "error": str(e)},
415
+ status_code=500
416
+ )
417
 
418
  @demo.app.get("/api/models")
419
+ def api_models(): # Remove async
420
+ models_info = {
421
  "available_models": ["microsoft/Phi-3.5-mini-instruct"],
422
  "current_model": Config.MODEL_NAME,
423
  "quantization": "Q4_K_M",
424
  "context_length": 4096,
425
  "parameters": "3.8B"
426
  }
427
+ return models_info # FastAPI automatically converts dict to JSON
428
 
429
  @demo.app.get("/api/stats")
430
+ def api_stats(): # Remove async
431
  if not monitor:
432
+ return JSONResponse(
433
+ content={"error": "Monitoring system not available"},
434
+ status_code=503
435
+ )
436
 
437
+ stats_data = {
438
  "total_requests": len(monitor.inference_metrics),
439
  "average_response_time": monitor.get_average_response_time(),
440
  "error_rate": monitor.get_error_rate(),
441
  "uptime": monitor.get_uptime(),
442
  "system_health": get_system_status()
443
  }
444
+ return stats_data # FastAPI automatically converts dict to JSON
445
 
446
  if __name__ == "__main__":
447
  logger.info("🎵 Starting Saem's Tunes AI on Hugging Face Spaces...")