Spaces:
Sleeping
Sleeping
Delete main.py
Browse files
main.py
DELETED
@@ -1,158 +0,0 @@
|
|
1 |
-
from fastapi import FastAPI, Depends, HTTPException, Security, status
|
2 |
-
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
|
3 |
-
from fastapi.middleware.cors import CORSMiddleware
|
4 |
-
from jose import JWTError, jwt
|
5 |
-
from passlib.context import CryptContext
|
6 |
-
from datetime import datetime, timedelta
|
7 |
-
from typing import Optional, Dict, Any
|
8 |
-
import os
|
9 |
-
from pydantic import BaseModel
|
10 |
-
from config import settings
|
11 |
-
|
12 |
-
# Güvenlik yapılandırması
|
13 |
-
SECRET_KEY = os.environ.get("SECRET_KEY", "güvenli_bir_anahtar_oluşturun")
|
14 |
-
ALGORITHM = "HS256"
|
15 |
-
ACCESS_TOKEN_EXPIRE_MINUTES = 30
|
16 |
-
|
17 |
-
# Şifre hashleme
|
18 |
-
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
19 |
-
|
20 |
-
# Token doğrulama
|
21 |
-
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
22 |
-
|
23 |
-
# Kullanıcı modeli
|
24 |
-
class User(BaseModel):
|
25 |
-
username: str
|
26 |
-
full_name: str
|
27 |
-
email: str
|
28 |
-
role: str # "admin", "doctor", "specialist"
|
29 |
-
disabled: bool = False
|
30 |
-
|
31 |
-
# Token modeli
|
32 |
-
class Token(BaseModel):
|
33 |
-
access_token: str
|
34 |
-
token_type: str
|
35 |
-
|
36 |
-
# Örnek kullanıcı veritabanı (gerçek uygulamada güvenli bir veritabanı kullanın)
|
37 |
-
fake_users_db = {
|
38 |
-
"doktor": {
|
39 |
-
"username": "doktor",
|
40 |
-
"full_name": "Doktor Kullanıcı",
|
41 |
-
"email": "doktor@example.com",
|
42 |
-
"hashed_password": pwd_context.hash("gizlisifre"),
|
43 |
-
"role": "doctor",
|
44 |
-
"disabled": False
|
45 |
-
},
|
46 |
-
"bölüm_başkanı": {
|
47 |
-
"username": "bölüm_başkanı",
|
48 |
-
"full_name": "Bölüm Başkanı",
|
49 |
-
"email": "bolum@example.com",
|
50 |
-
"hashed_password": pwd_context.hash("gizlisifre2"),
|
51 |
-
"role": "specialist",
|
52 |
-
"disabled": False
|
53 |
-
}
|
54 |
-
}
|
55 |
-
|
56 |
-
# Uygulama
|
57 |
-
app = FastAPI(
|
58 |
-
title="Pediatrik ASR API",
|
59 |
-
description="Doktor viziteleri sırasında konuşmaları transkribe eden ve diyarize eden API",
|
60 |
-
version="0.1.0"
|
61 |
-
)
|
62 |
-
|
63 |
-
# CORS ayarları
|
64 |
-
app.add_middleware(
|
65 |
-
CORSMiddleware,
|
66 |
-
allow_origins=["*"],
|
67 |
-
allow_credentials=True,
|
68 |
-
allow_methods=["*"],
|
69 |
-
allow_headers=["*"],
|
70 |
-
)
|
71 |
-
|
72 |
-
# Yetkilendirme fonksiyonları
|
73 |
-
def verify_password(plain_password, hashed_password):
|
74 |
-
return pwd_context.verify(plain_password, hashed_password)
|
75 |
-
|
76 |
-
def get_user(db, username: str):
|
77 |
-
if username in db:
|
78 |
-
user_dict = db[username]
|
79 |
-
return User(**user_dict)
|
80 |
-
|
81 |
-
def authenticate_user(db, username: str, password: str):
|
82 |
-
user = get_user(db, username)
|
83 |
-
if not user:
|
84 |
-
return False
|
85 |
-
if not verify_password(password, db[username]["hashed_password"]):
|
86 |
-
return False
|
87 |
-
return user
|
88 |
-
|
89 |
-
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
|
90 |
-
to_encode = data.copy()
|
91 |
-
if expires_delta:
|
92 |
-
expire = datetime.utcnow() + expires_delta
|
93 |
-
else:
|
94 |
-
expire = datetime.utcnow() + timedelta(minutes=15)
|
95 |
-
to_encode.update({"exp": expire})
|
96 |
-
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
97 |
-
return encoded_jwt
|
98 |
-
|
99 |
-
async def get_current_user(token: str = Depends(oauth2_scheme)):
|
100 |
-
credentials_exception = HTTPException(
|
101 |
-
status_code=status.HTTP_401_UNAUTHORIZED,
|
102 |
-
detail="Geçersiz kimlik bilgileri",
|
103 |
-
headers={"WWW-Authenticate": "Bearer"},
|
104 |
-
)
|
105 |
-
try:
|
106 |
-
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
107 |
-
username: str = payload.get("sub")
|
108 |
-
if username is None:
|
109 |
-
raise credentials_exception
|
110 |
-
except JWTError:
|
111 |
-
raise credentials_exception
|
112 |
-
user = get_user(fake_users_db, username)
|
113 |
-
if user is None:
|
114 |
-
raise credentials_exception
|
115 |
-
return user
|
116 |
-
|
117 |
-
async def get_current_active_user(current_user: User = Depends(get_current_user)):
|
118 |
-
if current_user.disabled:
|
119 |
-
raise HTTPException(status_code=400, detail="Inactive user")
|
120 |
-
return current_user
|
121 |
-
|
122 |
-
# Doktor yetkisi kontrolü
|
123 |
-
def doctor_required(current_user: User = Depends(get_current_active_user)):
|
124 |
-
if current_user.role not in ["doctor", "specialist"]:
|
125 |
-
raise HTTPException(
|
126 |
-
status_code=status.HTTP_403_FORBIDDEN,
|
127 |
-
detail="Bu işlem için doktor yetkisi gereklidir"
|
128 |
-
)
|
129 |
-
return current_user
|
130 |
-
|
131 |
-
# Token endpoint
|
132 |
-
@app.post("/token", response_model=Token)
|
133 |
-
async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
|
134 |
-
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
|
135 |
-
if not user:
|
136 |
-
raise HTTPException(
|
137 |
-
status_code=status.HTTP_401_UNAUTHORIZED,
|
138 |
-
detail="Kullanıcı adı veya şifre hatalı",
|
139 |
-
headers={"WWW-Authenticate": "Bearer"},
|
140 |
-
)
|
141 |
-
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
|
142 |
-
access_token = create_access_token(
|
143 |
-
data={"sub": user.username}, expires_delta=access_token_expires
|
144 |
-
)
|
145 |
-
return {"access_token": access_token, "token_type": "bearer"}
|
146 |
-
|
147 |
-
# Türkçe dil desteği yapılandırması
|
148 |
-
app.state.asr_config = {
|
149 |
-
"language": "tr",
|
150 |
-
"model": "whisper-large-v3",
|
151 |
-
"domain": "medical",
|
152 |
-
# Güvenlik ayarları
|
153 |
-
"anonymize_data": True # Varsay��lan olarak veri anonimleştirme aktif
|
154 |
-
}
|
155 |
-
|
156 |
-
# Router'ı sonradan import et
|
157 |
-
from routes import router
|
158 |
-
app.include_router(router, prefix="/api/v1")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|