File size: 3,721 Bytes
9f48b33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import os
import uvicorn
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from speaker_api import SpeakerRecognitionAPI

# FastAPI uygulamasını başlat
app = FastAPI()

# CORS ayarları
origins = [
    "http://localhost:5173",  # Front end adresini buraya ekleyin
    "http://127.0.0.1:5173",
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

try:
    api = SpeakerRecognitionAPI()
except Exception as e:
    print(f"API başlatılırken bir hata oluştu: {e}")
    api = None

@app.get("/")
def read_root():
    return {"message": "Ses Kimlik Tespiti API'si çalışıyor."}

@app.post("/register/")
async def register_speaker(name: str, audio_files: list[UploadFile] = File(...)):
    if not api:
        return JSONResponse(status_code=500, content={"error": "API başlatılamadı."})

    temp_audio_paths = []
    for audio_file in audio_files:
        temp_audio_path = os.path.join(os.getcwd(), audio_file.filename)
        with open(temp_audio_path, "wb") as f:
            f.write(await audio_file.read())
        temp_audio_paths.append(temp_audio_path)

    success, message = api.register_speaker_with_multiple_files(name, temp_audio_paths)

    for temp_path in temp_audio_paths:
        os.remove(temp_path)

    if success:
        return JSONResponse(status_code=200, content={"message": message})
    else:
        return JSONResponse(status_code=400, content={"error": message})

@app.post("/recognize/")
async def recognize_speaker(audio_file: UploadFile = File(...)):
    if not api:
        return JSONResponse(status_code=500, content={"error": "API başlatılamadı."})
    
    temp_audio_path = os.path.join(os.getcwd(), audio_file.filename)
    with open(temp_audio_path, "wb") as f:
        f.write(await audio_file.read())

    success, result = api.recognize_speaker(temp_audio_path)

    os.remove(temp_audio_path)

    if success:
        return JSONResponse(status_code=200, content=result)
    else:
        return JSONResponse(status_code=404, content={"error": result})

@app.post("/update_speaker/")
async def update_speaker(name: str, audio_files: list[UploadFile] = File(...)):
    if not api:
        return JSONResponse(status_code=500, content={"error": "API başlatılamadı."})
    
    temp_audio_paths = []
    for audio_file in audio_files:
        temp_audio_path = os.path.join(os.getcwd(), audio_file.filename)
        with open(temp_audio_path, "wb") as f:
            f.write(await audio_file.read())
        temp_audio_paths.append(temp_audio_path)

    success, message = api.update_speaker(name, temp_audio_paths)

    for temp_path in temp_audio_paths:
        os.remove(temp_path)

    if success:
        return JSONResponse(status_code=200, content={"message": message})
    else:
        return JSONResponse(status_code=400, content={"error": message})

@app.post("/correct_guess/")
async def correct_guess(name: str, audio_file: UploadFile = File(...)):
    if not api:
        return JSONResponse(status_code=500, content={"error": "API başlatılamadı."})
    
    temp_audio_path = os.path.join(os.getcwd(), audio_file.filename)
    with open(temp_audio_path, "wb") as f:
        f.write(await audio_file.read())

    success, message = api.correct_guess(name, [temp_audio_path])

    os.remove(temp_audio_path)

    if success:
        return JSONResponse(status_code=200, content={"message": message})
    else:
        return JSONResponse(status_code=400, content={"error": message})

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)