File size: 2,767 Bytes
643a619
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
from schemas.appointment import AppointmentInput
from core.database import SessionLocal
from core.models.appointment import Appointment

router = APIRouter()

# Dependency
def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

@router.post("/appointments/create")
def create_appointment(data: AppointmentInput, db: Session = Depends(get_db)):
    new_appointment = Appointment(
        patient_name=data.patient_name,
        age=data.age,
        symptoms=data.symptoms,
        specialist=data.specialist
    )
    db.add(new_appointment)
    db.commit()
    db.refresh(new_appointment)

    return {
        "message": "Appointment saved to database",
        "data": {
            "id": new_appointment.id,
            "patient_name": new_appointment.patient_name,
            "age": new_appointment.age,
            "symptoms": new_appointment.symptoms,
            "specialist": new_appointment.specialist
        }
    }
from fastapi import APIRouter, UploadFile, File
from utils.stt_processor import simulate_stt  # make sure path is correct
import shutil
import os
from utils.specialist_predictor import predict_specialist

@router.post("/appointments/voice")
async def create_appointment_from_voice(audio: UploadFile = File(...), db: Session = Depends(get_db)):
    # 1. Save temp audio
    temp_path = f"temp_{audio.filename}"
    with open(temp_path, "wb") as buffer:
        shutil.copyfileobj(audio.file, buffer)

    try:
        # 2. Extract symptoms from audio
        data = simulate_stt(temp_path)
        symptoms = data.get("symptoms", "")

        # 3. Predict specialist using model
        specialist, score = predict_specialist(symptoms)

        # 4. Create appointment object
        new_appointment = Appointment(
            patient_name=data.get("patient_name", "Unknown"),
            age=data.get("age", 0),
            symptoms=symptoms,
            specialist=specialist
        )

        db.add(new_appointment)
        db.commit()
        db.refresh(new_appointment)

        # 5. Return response
        return {
            "message": "Appointment created successfully from voice",
            "predicted_specialist": specialist,
            "similarity_score": round(score, 4),
            "appointment": {
                "id": new_appointment.id,
                "patient_name": new_appointment.patient_name,
                "age": new_appointment.age,
                "symptoms": symptoms,
                "specialist": specialist
            }
        }

    finally:
        # Clean up file
        os.remove(temp_path)