File size: 13,716 Bytes
87b09dd 705031b 1b86d8a 87b09dd d919708 5243061 1b86d8a f841fdd 1b86d8a 379310a 1b86d8a 87b09dd 1b86d8a 87b09dd b249bd4 e16eacd 87b09dd 705031b 87b09dd 705031b 87b09dd 705031b 8f6aeec 705031b 1b86d8a f5c6ef3 87b09dd 1b86d8a 87b09dd 7fa30a6 87b09dd 1b86d8a 2ee9112 87b09dd 1b86d8a bb3c951 87b09dd 1b86d8a 7250ede 87b09dd 8f6aeec 87b09dd 486f759 87b09dd 1b86d8a 87b09dd 8f6aeec 1b86d8a 87b09dd 1b86d8a e16eacd 1b86d8a 87b09dd 1b86d8a ead0f7d 1b86d8a ead0f7d 1b86d8a cd321ac 1b86d8a cd321ac 1b86d8a 87b09dd 8f6aeec cd321ac 87b09dd 5243061 f841fdd 5243061 f841fdd 2404de6 5243061 e16eacd cd321ac 5243061 87b09dd d919708 87b09dd 1b86d8a 87b09dd 00df592 87b09dd 00df592 87b09dd 00df592 87b09dd 1b86d8a 87b09dd 1b86d8a 87b09dd 1b86d8a 87b09dd 1b86d8a 87b09dd 1b86d8a 87b09dd 1b86d8a 87b09dd 1b86d8a 87b09dd 1b86d8a 6346369 87b09dd 5243061 87b09dd f841fdd 87b09dd 1b86d8a 87b09dd 1b86d8a 87b09dd 1b86d8a 87b09dd 1b86d8a 87b09dd 1b86d8a 87b09dd 1b86d8a 87b09dd 1b86d8a 87b09dd 1b86d8a 87b09dd 1b86d8a 87b09dd 1b86d8a 87b09dd 1b86d8a 87b09dd 1b86d8a 5243061 1b86d8a 87b09dd 5243061 87b09dd 705031b a374e98 |
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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# SPDX-FileCopyrightText: Hadad <[email protected]>
# SPDX-License-Identifier: Apache-2.0
import os
import logging
from fastapi import FastAPI, Request, Depends, HTTPException, status, Query
from fastapi.responses import HTMLResponse, RedirectResponse, PlainTextResponse, FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.middleware.sessions import SessionMiddleware
from fastapi.openapi.docs import get_swagger_ui_html
from fastapi.middleware.cors import CORSMiddleware
from api.endpoints import router as api_router
from api.auth import fastapi_users, auth_backend, current_active_user, google_oauth_client, github_oauth_client
from api.database import get_db, engine, Base
from api.models import User, UserRead, UserCreate, Conversation
from motor.motor_asyncio import AsyncIOMotorClient
from pydantic import BaseModel
from typing import List
import uvicorn
import markdown2
from sqlalchemy.orm import Session
from pathlib import Path
from hashlib import md5
from datetime import datetime
import re
from httpx_oauth.clients.google import GoogleOAuth2
from httpx_oauth.exceptions import GetIdEmailError
# Setup logging for debugging and monitoring
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.info("Files in current dir: %s", os.listdir(os.getcwd()))
# Check environment variables for required configurations
HF_TOKEN = os.getenv("HF_TOKEN")
if not HF_TOKEN:
logger.error("HF_TOKEN is not set in environment variables.")
raise ValueError("HF_TOKEN is required for Inference API.")
MONGO_URI = os.getenv("MONGODB_URI")
if not MONGO_URI:
logger.error("MONGODB_URI is not set in environment variables.")
raise ValueError("MONGODB_URI is required for MongoDB.")
JWT_SECRET = os.getenv("JWT_SECRET")
if not JWT_SECRET or len(JWT_SECRET) < 32:
logger.error("JWT_SECRET is not set or too short.")
raise ValueError("JWT_SECRET is required (at least 32 characters).")
# MongoDB setup for blog posts and session message counts
client = AsyncIOMotorClient(MONGO_URI)
mongo_db = client["hager"]
session_message_counts = mongo_db["session_message_counts"]
# Create MongoDB index for session_id to ensure uniqueness
async def setup_mongo_index():
await session_message_counts.create_index("session_id", unique=True)
# Jinja2 setup with Markdown filter for rendering Markdown content
templates = Jinja2Templates(directory="templates")
templates.env.filters['markdown'] = lambda text: markdown2.markdown(text)
# Pydantic model for blog posts
class BlogPost(BaseModel):
id: str
title: str
content: str
author: str
date: str
created_at: str
# Application settings from environment variables
QUEUE_SIZE = int(os.getenv("QUEUE_SIZE", 80))
CONCURRENCY_LIMIT = int(os.getenv("CONCURRENCY_LIMIT", 20))
# Initialize FastAPI app
app = FastAPI(title="MGZon Chatbot API")
# Add SessionMiddleware for handling non-logged-in user sessions
app.add_middleware(SessionMiddleware, secret_key=JWT_SECRET)
# Create SQLAlchemy database tables
Base.metadata.create_all(bind=engine)
# Mount static files directory
os.makedirs("static", exist_ok=True)
app.mount("/static", StaticFiles(directory="static"), name="static")
# CORS setup to allow requests from specific origins
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Kept as wildcard for multiple projects as per request
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include routers for authentication, user management, and API endpoints
app.include_router(
fastapi_users.get_auth_router(auth_backend),
prefix="/auth/jwt",
tags=["auth"],
)
app.include_router(
fastapi_users.get_register_router(UserRead, UserCreate),
prefix="/auth",
tags=["auth"],
)
app.include_router(
fastapi_users.get_users_router(UserRead, UserCreate),
prefix="/users",
tags=["users"],
)
app.include_router(
fastapi_users.get_oauth_router(
google_oauth_client,
auth_backend,
JWT_SECRET,
redirect_url="/auth/google/callback" # Updated to callback endpoint
),
prefix="/auth/google",
tags=["auth"],
)
app.include_router(
fastapi_users.get_oauth_router(
github_oauth_client,
auth_backend,
JWT_SECRET,
redirect_url="/auth/github/callback" # Updated to callback endpoint
),
prefix="/auth/github",
tags=["auth"],
)
app.include_router(api_router)
# Custom OAuth callbacks to redirect to /chat
@app.get("/auth/google/callback", response_class=RedirectResponse)
async def google_oauth_callback(request: Request):
logger.info(f"Processing Google OAuth callback: {request.url}")
return RedirectResponse(url="/chat", status_code=302)
@app.get("/auth/github/callback", response_class=RedirectResponse)
async def github_oauth_callback(request: Request):
logger.info(f"Processing GitHub OAuth callback: {request.url}")
return RedirectResponse(url="/chat", status_code=302)
# Custom middleware for handling 404 and 500 errors
class NotFoundMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
try:
response = await call_next(request)
if response.status_code == 404:
logger.warning(f"404 Not Found: {request.url}")
return templates.TemplateResponse("404.html", {"request": request}, status_code=404)
return response
except Exception as e:
logger.exception(f"Error processing request {request.url}: {e}")
return templates.TemplateResponse("500.html", {"request": request, "error": str(e)}, status_code=500)
app.add_middleware(NotFoundMiddleware)
# Exception handler for OAuth errors
@app.exception_handler(GetIdEmailError)
async def handle_oauth_error(request: Request, exc: GetIdEmailError):
logger.error(f"OAuth error: {exc}")
error_message = "Failed to authenticate with OAuth. Please try again or contact support."
return templates.TemplateResponse(
"login.html",
{"request": request, "error": error_message},
status_code=400
)
# Root endpoint for homepage
@app.get("/", response_class=HTMLResponse)
async def root(request: Request, user: User = Depends(current_active_user)):
return templates.TemplateResponse("index.html", {"request": request, "user": user})
# Google verification endpoint
@app.get("/google97468ef1f6b6e804.html", response_class=PlainTextResponse)
async def google_verification():
return "google-site-verification: google97468ef1f6b6e804.html"
# Login page
@app.get("/login", response_class=HTMLResponse)
async def login_page(request: Request, user: User = Depends(current_active_user)):
if user:
return RedirectResponse(url="/chat", status_code=302)
return templates.TemplateResponse("login.html", {"request": request})
# Register page
@app.get("/register", response_class=HTMLResponse)
async def register_page(request: Request, user: User = Depends(current_active_user)):
if user:
return RedirectResponse(url="/chat", status_code=302)
return templates.TemplateResponse("register.html", {"request": request})
# Chat page
@app.get("/chat", response_class=HTMLResponse)
async def chat(request: Request, user: User = Depends(current_active_user)):
return templates.TemplateResponse("chat.html", {"request": request, "user": user})
# Specific conversation page
@app.get("/chat/{conversation_id}", response_class=HTMLResponse)
async def chat_conversation(
request: Request,
conversation_id: str,
user: User = Depends(current_active_user),
db: Session = Depends(get_db)
):
if not user:
return RedirectResponse(url="/login", status_code=302)
conversation = db.query(Conversation).filter(
Conversation.conversation_id == conversation_id,
Conversation.user_id == user.id
).first()
if not conversation:
raise HTTPException(status_code=404, detail="Conversation not found")
return templates.TemplateResponse(
"chat.html",
{
"request": request,
"user": user,
"conversation_id": conversation.conversation_id,
"conversation_title": conversation.title or "Untitled Conversation"
}
)
# About page
@app.get("/about", response_class=HTMLResponse)
async def about(request: Request, user: User = Depends(current_active_user)):
return templates.TemplateResponse("about.html", {"request": request, "user": user})
# Serve static files with caching and ETag support
@app.get("/static/{path:path}")
async def serve_static(path: str):
# Remove query parameters (e.g., ?v=1.0) for versioning
clean_path = re.sub(r'\?.*', '', path)
file_path = Path("static") / clean_path
if not file_path.exists():
raise HTTPException(status_code=404, detail="File not found")
# Set cache duration: 1 year for images, 1 hour for JS/CSS
cache_duration = 31536000 # 1 year
if clean_path.endswith(('.js', '.css')):
cache_duration = 3600 # 1 hour
# Generate ETag and Last-Modified headers
with open(file_path, "rb") as f:
file_hash = md5(f.read()).hexdigest()
headers = {
"Cache-Control": f"public, max-age={cache_duration}",
"ETag": file_hash,
"Last-Modified": datetime.utcfromtimestamp(file_path.stat().st_mtime).strftime('%a, %d %b %Y %H:%M:%S GMT')
}
return FileResponse(file_path, headers=headers)
# Blog page with pagination
@app.get("/blog", response_class=HTMLResponse)
async def blog(request: Request, skip: int = Query(0, ge=0), limit: int = Query(10, ge=1, le=100)):
posts = await mongo_db.blog_posts.find().skip(skip).limit(limit).to_list(limit)
return templates.TemplateResponse("blog.html", {"request": request, "posts": posts})
# Individual blog post
@app.get("/blog/{post_id}", response_class=HTMLResponse)
async def blog_post(request: Request, post_id: str):
post = await mongo_db.blog_posts.find_one({"id": post_id})
if not post:
raise HTTPException(status_code=404, detail="Post not found")
return templates.TemplateResponse("blog_post.html", {"request": request, "post": post})
# Docs page
@app.get("/docs", response_class=HTMLResponse)
async def docs(request: Request):
return templates.TemplateResponse("docs.html", {"request": request})
# Swagger UI for API documentation
@app.get("/swagger", response_class=HTMLResponse)
async def swagger_ui():
return get_swagger_ui_html(openapi_url="/openapi.json", title="MGZon API Documentation")
# Sitemap with dynamic dates
@app.get("/sitemap.xml", response_class=PlainTextResponse)
async def sitemap():
posts = await mongo_db.blog_posts.find().to_list(100)
current_date = datetime.utcnow().strftime('%Y-%m-%d')
xml = '<?xml version="1.0" encoding="UTF-8"?>\n'
xml += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n'
# Main pages with dynamic lastmod
xml += ' <url>\n'
xml += ' <loc>https://mgzon-mgzon-app.hf.space/</loc>\n'
xml += f' <lastmod>{current_date}</lastmod>\n'
xml += ' <changefreq>daily</changefreq>\n'
xml += ' <priority>1.0</priority>\n'
xml += ' </url>\n'
xml += ' <url>\n'
xml += ' <loc>https://mgzon-mgzon-app.hf.space/chat</loc>\n'
xml += f' <lastmod>{current_date}</lastmod>\n'
xml += ' <changefreq>daily</changefreq>\n'
xml += ' <priority>0.8</priority>\n'
xml += ' </url>\n'
xml += ' <url>\n'
xml += ' <loc>https://mgzon-mgzon-app.hf.space/about</loc>\n'
xml += f' <lastmod>{current_date}</lastmod>\n'
xml += ' <changefreq>weekly</changefreq>\n'
xml += ' <priority>0.7</priority>\n'
xml += ' </url>\n'
xml += ' <url>\n'
xml += ' <loc>https://mgzon-mgzon-app.hf.space/login</loc>\n'
xml += f' <lastmod>{current_date}</lastmod>\n'
xml += ' <changefreq>weekly</changefreq>\n'
xml += ' <priority>0.8</priority>\n'
xml += ' </url>\n'
xml += ' <url>\n'
xml += ' <loc>https://mgzon-mgzon-app.hf.space/register</loc>\n'
xml += f' <lastmod>{current_date}</lastmod>\n'
xml += ' <changefreq>weekly</changefreq>\n'
xml += ' <priority>0.8</priority>\n'
xml += ' </url>\n'
xml += ' <url>\n'
xml += ' <loc>https://mgzon-mgzon-app.hf.space/docs</loc>\n'
xml += f' <lastmod>{current_date}</lastmod>\n'
xml += ' <changefreq>weekly</changefreq>\n'
xml += ' <priority>0.9</priority>\n'
xml += ' </url>\n'
xml += ' <url>\n'
xml += ' <loc>https://mgzon-mgzon-app.hf.space/blog</loc>\n'
xml += f' <lastmod>{current_date}</lastmod>\n'
xml += ' <changefreq>daily</changefreq>\n'
xml += ' <priority>0.9</priority>\n'
xml += ' </url>\n'
# Blog posts from MongoDB
for post in posts:
xml += ' <url>\n'
xml += f' <loc>https://mgzon-mgzon-app.hf.space/blog/{post["id"]}</loc>\n'
xml += f' <lastmod>{post["date"]}</lastmod>\n'
xml += ' <changefreq>weekly</changefreq>\n'
xml += ' <priority>0.9</priority>\n'
xml += ' </url>\n'
xml += '</urlset>'
return xml
# Redirect /gradio to /chat
@app.get("/gradio", response_class=RedirectResponse)
async def launch_chatbot():
return RedirectResponse(url="/chat", status_code=302)
# Startup event to initialize MongoDB index
@app.on_event("startup")
async def startup_event():
await setup_mongo_index()
# Run the app
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", 7860)))
|