Nagesh Muralidhar
Initial commit of PodCraft application
fd52f31
raw
history blame
6.63 kB
from fastapi import FastAPI, Request, HTTPException, Depends, status, File, UploadFile, Form
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from fastapi.responses import JSONResponse, FileResponse, HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import Optional, Dict, List, Union, Any
from datetime import datetime, timedelta
import jwt
from jwt.exceptions import PyJWTError
from passlib.context import CryptContext
import os
import shutil
import logging
import json
from motor.motor_asyncio import AsyncIOMotorClient
from decouple import config
import uuid
from bson.objectid import ObjectId
import asyncio
import time
import sys
from pathlib import Path
# Import the original app modules
from app.models import *
from app.agents.podcast_manager import PodcastManager
from app.agents.researcher import Researcher
from app.agents.debate_agent import DebateAgent
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Initialize FastAPI app
app = FastAPI(title="PodCraft API")
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allow all origins in production
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Get MongoDB connection string from environment or config
MONGODB_URL = os.getenv("MONGODB_URL", config("MONGODB_URL", default="mongodb://localhost:27017"))
# MongoDB client
client = AsyncIOMotorClient(MONGODB_URL)
db = client.podcraft
users = db.users
podcasts = db.podcasts
agents = db.agents
workflows = db.workflows
# Initialize podcast manager
podcast_manager = PodcastManager()
# Initialize researcher
researcher = Researcher()
# Initialize debate agent
debate_agent = DebateAgent()
# Password hashing
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
# JWT settings
SECRET_KEY = os.getenv("SECRET_KEY", config("SECRET_KEY", default="your-secret-key"))
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 60 * 24 * 7 # 1 week
# OAuth2 scheme
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
# Keep the original authentication and API routes
# Include all the functions and routes from the original main.py
def create_access_token(data: dict):
to_encode = data.copy()
expire = datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt
def verify_password(plain_password, hashed_password):
return pwd_context.verify(plain_password, hashed_password)
def get_password_hash(password):
return pwd_context.hash(password)
async def get_current_user(token: str = Depends(oauth2_scheme)):
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
username: str = payload.get("sub")
if username is None:
raise credentials_exception
except PyJWTError:
raise credentials_exception
user = await users.find_one({"username": username})
if user is None:
raise credentials_exception
# Convert ObjectId to string for JSON serialization
user["_id"] = str(user["_id"])
return user
# Include all the API routes from the original main.py here
# ...
# Mount static files for frontend
static_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "static"))
app.mount("/static", StaticFiles(directory=static_path), name="static")
# Add route to serve the frontend
@app.get("/", response_class=HTMLResponse)
async def serve_frontend():
html_file = os.path.join(static_path, "index.html")
if os.path.exists(html_file):
with open(html_file, "r") as f:
return f.read()
else:
return HTMLResponse(content="<html><body><h1>PodCraft API</h1><p>Frontend not found.</p></body></html>")
# Add route to serve audio files
@app.get("/audio/{path:path}")
async def serve_audio(path: str):
audio_file = os.path.join("/app/temp_audio", path)
if os.path.exists(audio_file):
return FileResponse(audio_file)
else:
raise HTTPException(status_code=404, detail="Audio file not found")
# Route for health check
@app.get("/health")
async def health():
return {"status": "healthy"}
# Include all the original API routes here from the original main.py
@app.post("/signup")
async def signup(user: UserCreate):
# Check if username exists
existing_user = await users.find_one({"username": user.username})
if existing_user:
raise HTTPException(status_code=400, detail="Username already registered")
# Hash the password
hashed_password = get_password_hash(user.password)
# Create new user
user_obj = {"username": user.username, "password": hashed_password}
new_user = await users.insert_one(user_obj)
# Create access token
access_token = create_access_token(data={"sub": user.username})
return {"access_token": access_token, "token_type": "bearer"}
@app.post("/token", response_model=Token)
async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
user = await users.find_one({"username": form_data.username})
if not user or not verify_password(form_data.password, user["password"]):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"},
)
access_token = create_access_token(data={"sub": form_data.username})
return {"access_token": access_token, "token_type": "bearer"}
@app.post("/login", response_model=Token)
async def login(request: Request, user: UserLogin):
db_user = await users.find_one({"username": user.username})
if not db_user or not verify_password(user.password, db_user["password"]):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password"
)
access_token = create_access_token(data={"sub": user.username})
return {"access_token": access_token, "token_type": "bearer"}
# Add all the other API routes from the original main.py
# ...
if __name__ == "__main__":
import uvicorn
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)