Spaces:
Sleeping
Sleeping
Ajit Panday commited on
Commit ·
96c5d66
1
Parent(s): 1de8ce6
Fix circular import by creating settings module
Browse files- app/auth.py +7 -17
- app/models.py +4 -6
- app/settings.py +25 -0
- main.py +7 -3
app/auth.py
CHANGED
|
@@ -6,19 +6,13 @@ from jose import JWTError, jwt
|
|
| 6 |
from passlib.context import CryptContext
|
| 7 |
from typing import Optional
|
| 8 |
import secrets
|
| 9 |
-
import
|
| 10 |
-
from
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
from .database import get_db
|
| 13 |
|
| 14 |
-
# Load environment variables
|
| 15 |
-
load_dotenv()
|
| 16 |
-
|
| 17 |
-
# Security configuration
|
| 18 |
-
SECRET_KEY = os.getenv("JWT_SECRET", "your-secret-key")
|
| 19 |
-
ALGORITHM = "HS256"
|
| 20 |
-
ACCESS_TOKEN_EXPIRE_MINUTES = 30
|
| 21 |
-
|
| 22 |
# Password hashing
|
| 23 |
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
| 24 |
|
|
@@ -28,10 +22,6 @@ api_key_header = APIKeyHeader(name="api-key")
|
|
| 28 |
|
| 29 |
router = APIRouter()
|
| 30 |
|
| 31 |
-
# Admin user credentials (in production, use database)
|
| 32 |
-
ADMIN_USERNAME = os.getenv("ADMIN_USERNAME", "admin")
|
| 33 |
-
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD", "admin")
|
| 34 |
-
|
| 35 |
def verify_api_key(api_key: str = Security(api_key_header)) -> models.Customer:
|
| 36 |
"""Verify API key and return customer"""
|
| 37 |
db = next(get_db())
|
|
@@ -64,7 +54,7 @@ def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
|
|
| 64 |
else:
|
| 65 |
expire = datetime.utcnow() + timedelta(minutes=15)
|
| 66 |
to_encode.update({"exp": expire})
|
| 67 |
-
encoded_jwt = jwt.encode(to_encode,
|
| 68 |
return encoded_jwt
|
| 69 |
|
| 70 |
def get_current_admin(token: str = Depends(oauth2_scheme)):
|
|
@@ -75,7 +65,7 @@ def get_current_admin(token: str = Depends(oauth2_scheme)):
|
|
| 75 |
headers={"WWW-Authenticate": "Bearer"},
|
| 76 |
)
|
| 77 |
try:
|
| 78 |
-
payload = jwt.decode(token,
|
| 79 |
username: str = payload.get("sub")
|
| 80 |
if username is None:
|
| 81 |
raise credentials_exception
|
|
|
|
| 6 |
from passlib.context import CryptContext
|
| 7 |
from typing import Optional
|
| 8 |
import secrets
|
| 9 |
+
from . import models
|
| 10 |
+
from .settings import (
|
| 11 |
+
JWT_SECRET, ALGORITHM, ACCESS_TOKEN_EXPIRE_MINUTES,
|
| 12 |
+
ADMIN_USERNAME, ADMIN_PASSWORD
|
| 13 |
+
)
|
| 14 |
from .database import get_db
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# Password hashing
|
| 17 |
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
| 18 |
|
|
|
|
| 22 |
|
| 23 |
router = APIRouter()
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
def verify_api_key(api_key: str = Security(api_key_header)) -> models.Customer:
|
| 26 |
"""Verify API key and return customer"""
|
| 27 |
db = next(get_db())
|
|
|
|
| 54 |
else:
|
| 55 |
expire = datetime.utcnow() + timedelta(minutes=15)
|
| 56 |
to_encode.update({"exp": expire})
|
| 57 |
+
encoded_jwt = jwt.encode(to_encode, JWT_SECRET, algorithm=ALGORITHM)
|
| 58 |
return encoded_jwt
|
| 59 |
|
| 60 |
def get_current_admin(token: str = Depends(oauth2_scheme)):
|
|
|
|
| 65 |
headers={"WWW-Authenticate": "Bearer"},
|
| 66 |
)
|
| 67 |
try:
|
| 68 |
+
payload = jwt.decode(token, JWT_SECRET, algorithms=[ALGORITHM])
|
| 69 |
username: str = payload.get("sub")
|
| 70 |
if username is None:
|
| 71 |
raise credentials_exception
|
app/models.py
CHANGED
|
@@ -7,13 +7,11 @@ from dotenv import load_dotenv
|
|
| 7 |
import json
|
| 8 |
import requests
|
| 9 |
from typing import Optional, List, Dict
|
|
|
|
| 10 |
|
| 11 |
# Load environment variables
|
| 12 |
load_dotenv()
|
| 13 |
|
| 14 |
-
# Get database URL from environment variables
|
| 15 |
-
DATABASE_URL = os.getenv("DATABASE_URL")
|
| 16 |
-
|
| 17 |
# Create SQLAlchemy engine
|
| 18 |
engine = create_engine(DATABASE_URL)
|
| 19 |
|
|
@@ -60,7 +58,7 @@ class Customer(Base):
|
|
| 60 |
|
| 61 |
def get_call_records(self, start_date: Optional[str] = None, end_date: Optional[str] = None) -> List[Dict]:
|
| 62 |
"""Get call records via API"""
|
| 63 |
-
url = f"{
|
| 64 |
params = {}
|
| 65 |
if start_date:
|
| 66 |
params['start_date'] = start_date
|
|
@@ -77,7 +75,7 @@ class Customer(Base):
|
|
| 77 |
|
| 78 |
def get_call_details(self, call_id: str) -> Dict:
|
| 79 |
"""Get specific call details via API"""
|
| 80 |
-
url = f"{
|
| 81 |
response = requests.get(
|
| 82 |
url,
|
| 83 |
headers={"api-key": self.api_key}
|
|
@@ -87,7 +85,7 @@ class Customer(Base):
|
|
| 87 |
|
| 88 |
def search_calls(self, query: Dict) -> List[Dict]:
|
| 89 |
"""Search calls via API"""
|
| 90 |
-
url = f"{
|
| 91 |
response = requests.get(
|
| 92 |
url,
|
| 93 |
headers={"api-key": self.api_key},
|
|
|
|
| 7 |
import json
|
| 8 |
import requests
|
| 9 |
from typing import Optional, List, Dict
|
| 10 |
+
from .settings import DATABASE_URL, API_BASE_URL
|
| 11 |
|
| 12 |
# Load environment variables
|
| 13 |
load_dotenv()
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
# Create SQLAlchemy engine
|
| 16 |
engine = create_engine(DATABASE_URL)
|
| 17 |
|
|
|
|
| 58 |
|
| 59 |
def get_call_records(self, start_date: Optional[str] = None, end_date: Optional[str] = None) -> List[Dict]:
|
| 60 |
"""Get call records via API"""
|
| 61 |
+
url = f"{API_BASE_URL}/api/v1/calls"
|
| 62 |
params = {}
|
| 63 |
if start_date:
|
| 64 |
params['start_date'] = start_date
|
|
|
|
| 75 |
|
| 76 |
def get_call_details(self, call_id: str) -> Dict:
|
| 77 |
"""Get specific call details via API"""
|
| 78 |
+
url = f"{API_BASE_URL}/api/v1/calls/{call_id}"
|
| 79 |
response = requests.get(
|
| 80 |
url,
|
| 81 |
headers={"api-key": self.api_key}
|
|
|
|
| 85 |
|
| 86 |
def search_calls(self, query: Dict) -> List[Dict]:
|
| 87 |
"""Search calls via API"""
|
| 88 |
+
url = f"{API_BASE_URL}/api/v1/calls/search"
|
| 89 |
response = requests.get(
|
| 90 |
url,
|
| 91 |
headers={"api-key": self.api_key},
|
app/settings.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
|
| 4 |
+
# Load environment variables
|
| 5 |
+
load_dotenv()
|
| 6 |
+
|
| 7 |
+
# API Settings
|
| 8 |
+
API_BASE_URL = os.getenv("API_BASE_URL", "https://your-huggingface-space-url")
|
| 9 |
+
|
| 10 |
+
# Database Settings
|
| 11 |
+
DATABASE_URL = os.getenv("DATABASE_URL")
|
| 12 |
+
|
| 13 |
+
# JWT Settings
|
| 14 |
+
JWT_SECRET = os.getenv("JWT_SECRET", "your-secret-key")
|
| 15 |
+
ALGORITHM = "HS256"
|
| 16 |
+
ACCESS_TOKEN_EXPIRE_MINUTES = 30
|
| 17 |
+
|
| 18 |
+
# Admin Settings
|
| 19 |
+
ADMIN_USERNAME = os.getenv("ADMIN_USERNAME", "admin")
|
| 20 |
+
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD", "admin")
|
| 21 |
+
|
| 22 |
+
# Hugging Face Models
|
| 23 |
+
WHISPER_MODEL = "openai/whisper-base"
|
| 24 |
+
SUMMARIZER_MODEL = "facebook/bart-large-cnn"
|
| 25 |
+
SENTIMENT_MODEL = "nlptown/bert-base-multilingual-uncased-sentiment"
|
main.py
CHANGED
|
@@ -11,6 +11,10 @@ from transformers import pipeline
|
|
| 11 |
from sqlalchemy.orm import Session
|
| 12 |
import asyncio
|
| 13 |
from app import models, auth
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
from sqlalchemy.orm import sessionmaker
|
| 15 |
from fastapi.security import OAuth2PasswordRequestForm
|
| 16 |
|
|
@@ -34,9 +38,9 @@ app.add_middleware(
|
|
| 34 |
)
|
| 35 |
|
| 36 |
# Initialize Hugging Face models
|
| 37 |
-
transcriber = pipeline("automatic-speech-recognition", model=
|
| 38 |
-
summarizer = pipeline("summarization", model=
|
| 39 |
-
sentiment_analyzer = pipeline("sentiment-analysis", model=
|
| 40 |
|
| 41 |
# Include auth router
|
| 42 |
app.include_router(auth.router, prefix="/api/v1", tags=["auth"])
|
|
|
|
| 11 |
from sqlalchemy.orm import Session
|
| 12 |
import asyncio
|
| 13 |
from app import models, auth
|
| 14 |
+
from app.settings import (
|
| 15 |
+
ADMIN_USERNAME, ADMIN_PASSWORD, WHISPER_MODEL,
|
| 16 |
+
SUMMARIZER_MODEL, SENTIMENT_MODEL
|
| 17 |
+
)
|
| 18 |
from sqlalchemy.orm import sessionmaker
|
| 19 |
from fastapi.security import OAuth2PasswordRequestForm
|
| 20 |
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
# Initialize Hugging Face models
|
| 41 |
+
transcriber = pipeline("automatic-speech-recognition", model=WHISPER_MODEL)
|
| 42 |
+
summarizer = pipeline("summarization", model=SUMMARIZER_MODEL)
|
| 43 |
+
sentiment_analyzer = pipeline("sentiment-analysis", model=SENTIMENT_MODEL)
|
| 44 |
|
| 45 |
# Include auth router
|
| 46 |
app.include_router(auth.router, prefix="/api/v1", tags=["auth"])
|