Spaces:
Sleeping
Sleeping
frabbani commited on
Commit ·
2f70e33
1
Parent(s): d6b2572
Add HeAR audio support
Browse files- medgemma-hf-space/server.py +52 -7
- server.py +52 -7
medgemma-hf-space/server.py
CHANGED
|
@@ -18,6 +18,7 @@ from pydantic import BaseModel
|
|
| 18 |
|
| 19 |
# Configuration
|
| 20 |
LLAMA_SERVER_URL = os.getenv("LLAMA_SERVER_URL", "http://localhost:8081")
|
|
|
|
| 21 |
DB_PATH = os.getenv("DB_PATH", "data/fhir.db")
|
| 22 |
|
| 23 |
# Headers for LLM requests (ngrok requires this)
|
|
@@ -440,17 +441,61 @@ async def agent_chat_endpoint(request: ChatRequest):
|
|
| 440 |
return StreamingResponse(generate(), media_type="text/event-stream")
|
| 441 |
|
| 442 |
# ============================================================================
|
| 443 |
-
# Audio
|
| 444 |
# ============================================================================
|
| 445 |
|
|
|
|
|
|
|
| 446 |
@app.get("/api/audio/status")
|
| 447 |
async def audio_analyzer_status():
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
|
| 455 |
if __name__ == "__main__":
|
| 456 |
import uvicorn
|
|
|
|
| 18 |
|
| 19 |
# Configuration
|
| 20 |
LLAMA_SERVER_URL = os.getenv("LLAMA_SERVER_URL", "http://localhost:8081")
|
| 21 |
+
HEAR_SERVER_URL = os.getenv("HEAR_SERVER_URL", "") # Empty = disabled
|
| 22 |
DB_PATH = os.getenv("DB_PATH", "data/fhir.db")
|
| 23 |
|
| 24 |
# Headers for LLM requests (ngrok requires this)
|
|
|
|
| 441 |
return StreamingResponse(generate(), media_type="text/event-stream")
|
| 442 |
|
| 443 |
# ============================================================================
|
| 444 |
+
# Audio Analysis (proxies to remote HeAR server)
|
| 445 |
# ============================================================================
|
| 446 |
|
| 447 |
+
from fastapi import File, UploadFile
|
| 448 |
+
|
| 449 |
@app.get("/api/audio/status")
|
| 450 |
async def audio_analyzer_status():
|
| 451 |
+
if not HEAR_SERVER_URL:
|
| 452 |
+
return {
|
| 453 |
+
"available": False,
|
| 454 |
+
"model": None,
|
| 455 |
+
"message": "Audio analysis not configured. Set HEAR_SERVER_URL.",
|
| 456 |
+
"capabilities": []
|
| 457 |
+
}
|
| 458 |
+
|
| 459 |
+
# Check remote HeAR server
|
| 460 |
+
async with httpx.AsyncClient(timeout=5.0) as client:
|
| 461 |
+
try:
|
| 462 |
+
resp = await client.get(f"{HEAR_SERVER_URL}/status")
|
| 463 |
+
if resp.status_code == 200:
|
| 464 |
+
data = resp.json()
|
| 465 |
+
return {
|
| 466 |
+
"available": data.get("available", True),
|
| 467 |
+
"model": "HeAR (Remote)",
|
| 468 |
+
"model_type": "HeAR (Health Acoustic Representations)",
|
| 469 |
+
"message": "Connected to remote HeAR server",
|
| 470 |
+
"capabilities": data.get("capabilities", ["cough_detection", "covid_risk_screening", "tb_risk_screening"])
|
| 471 |
+
}
|
| 472 |
+
except Exception as e:
|
| 473 |
+
return {
|
| 474 |
+
"available": False,
|
| 475 |
+
"model": None,
|
| 476 |
+
"message": f"Cannot connect to HeAR server: {str(e)}",
|
| 477 |
+
"capabilities": []
|
| 478 |
+
}
|
| 479 |
+
|
| 480 |
+
@app.post("/api/audio/analyze")
|
| 481 |
+
async def analyze_audio(audio: UploadFile = File(...)):
|
| 482 |
+
if not HEAR_SERVER_URL:
|
| 483 |
+
return {"success": False, "error": "Audio analysis not configured"}
|
| 484 |
+
|
| 485 |
+
try:
|
| 486 |
+
audio_bytes = await audio.read()
|
| 487 |
+
|
| 488 |
+
async with httpx.AsyncClient(timeout=60.0) as client:
|
| 489 |
+
files = {"audio": ("recording.webm", audio_bytes, "audio/webm")}
|
| 490 |
+
resp = await client.post(f"{HEAR_SERVER_URL}/analyze", files=files)
|
| 491 |
+
|
| 492 |
+
if resp.status_code == 200:
|
| 493 |
+
result = resp.json()
|
| 494 |
+
return result
|
| 495 |
+
else:
|
| 496 |
+
return {"success": False, "error": f"HeAR server error: {resp.status_code}"}
|
| 497 |
+
except Exception as e:
|
| 498 |
+
return {"success": False, "error": str(e)}
|
| 499 |
|
| 500 |
if __name__ == "__main__":
|
| 501 |
import uvicorn
|
server.py
CHANGED
|
@@ -18,6 +18,7 @@ from pydantic import BaseModel
|
|
| 18 |
|
| 19 |
# Configuration
|
| 20 |
LLAMA_SERVER_URL = os.getenv("LLAMA_SERVER_URL", "http://localhost:8081")
|
|
|
|
| 21 |
DB_PATH = os.getenv("DB_PATH", "data/fhir.db")
|
| 22 |
|
| 23 |
# Headers for LLM requests (ngrok requires this)
|
|
@@ -440,17 +441,61 @@ async def agent_chat_endpoint(request: ChatRequest):
|
|
| 440 |
return StreamingResponse(generate(), media_type="text/event-stream")
|
| 441 |
|
| 442 |
# ============================================================================
|
| 443 |
-
# Audio
|
| 444 |
# ============================================================================
|
| 445 |
|
|
|
|
|
|
|
| 446 |
@app.get("/api/audio/status")
|
| 447 |
async def audio_analyzer_status():
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
|
| 455 |
if __name__ == "__main__":
|
| 456 |
import uvicorn
|
|
|
|
| 18 |
|
| 19 |
# Configuration
|
| 20 |
LLAMA_SERVER_URL = os.getenv("LLAMA_SERVER_URL", "http://localhost:8081")
|
| 21 |
+
HEAR_SERVER_URL = os.getenv("HEAR_SERVER_URL", "") # Empty = disabled
|
| 22 |
DB_PATH = os.getenv("DB_PATH", "data/fhir.db")
|
| 23 |
|
| 24 |
# Headers for LLM requests (ngrok requires this)
|
|
|
|
| 441 |
return StreamingResponse(generate(), media_type="text/event-stream")
|
| 442 |
|
| 443 |
# ============================================================================
|
| 444 |
+
# Audio Analysis (proxies to remote HeAR server)
|
| 445 |
# ============================================================================
|
| 446 |
|
| 447 |
+
from fastapi import File, UploadFile
|
| 448 |
+
|
| 449 |
@app.get("/api/audio/status")
|
| 450 |
async def audio_analyzer_status():
|
| 451 |
+
if not HEAR_SERVER_URL:
|
| 452 |
+
return {
|
| 453 |
+
"available": False,
|
| 454 |
+
"model": None,
|
| 455 |
+
"message": "Audio analysis not configured. Set HEAR_SERVER_URL.",
|
| 456 |
+
"capabilities": []
|
| 457 |
+
}
|
| 458 |
+
|
| 459 |
+
# Check remote HeAR server
|
| 460 |
+
async with httpx.AsyncClient(timeout=5.0) as client:
|
| 461 |
+
try:
|
| 462 |
+
resp = await client.get(f"{HEAR_SERVER_URL}/status")
|
| 463 |
+
if resp.status_code == 200:
|
| 464 |
+
data = resp.json()
|
| 465 |
+
return {
|
| 466 |
+
"available": data.get("available", True),
|
| 467 |
+
"model": "HeAR (Remote)",
|
| 468 |
+
"model_type": "HeAR (Health Acoustic Representations)",
|
| 469 |
+
"message": "Connected to remote HeAR server",
|
| 470 |
+
"capabilities": data.get("capabilities", ["cough_detection", "covid_risk_screening", "tb_risk_screening"])
|
| 471 |
+
}
|
| 472 |
+
except Exception as e:
|
| 473 |
+
return {
|
| 474 |
+
"available": False,
|
| 475 |
+
"model": None,
|
| 476 |
+
"message": f"Cannot connect to HeAR server: {str(e)}",
|
| 477 |
+
"capabilities": []
|
| 478 |
+
}
|
| 479 |
+
|
| 480 |
+
@app.post("/api/audio/analyze")
|
| 481 |
+
async def analyze_audio(audio: UploadFile = File(...)):
|
| 482 |
+
if not HEAR_SERVER_URL:
|
| 483 |
+
return {"success": False, "error": "Audio analysis not configured"}
|
| 484 |
+
|
| 485 |
+
try:
|
| 486 |
+
audio_bytes = await audio.read()
|
| 487 |
+
|
| 488 |
+
async with httpx.AsyncClient(timeout=60.0) as client:
|
| 489 |
+
files = {"audio": ("recording.webm", audio_bytes, "audio/webm")}
|
| 490 |
+
resp = await client.post(f"{HEAR_SERVER_URL}/analyze", files=files)
|
| 491 |
+
|
| 492 |
+
if resp.status_code == 200:
|
| 493 |
+
result = resp.json()
|
| 494 |
+
return result
|
| 495 |
+
else:
|
| 496 |
+
return {"success": False, "error": f"HeAR server error: {resp.status_code}"}
|
| 497 |
+
except Exception as e:
|
| 498 |
+
return {"success": False, "error": str(e)}
|
| 499 |
|
| 500 |
if __name__ == "__main__":
|
| 501 |
import uvicorn
|