forked from Varsha-1605/SocioSell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
103 lines (84 loc) · 3.13 KB
/
main.py
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
from fastapi import FastAPI, Request
from motor.motor_asyncio import AsyncIOMotorClient
import logging
import os
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse, JSONResponse
from routers import image, video, combined
from dotenv import load_dotenv
from time import time
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
app = FastAPI(
title="Social Media Product Listing Generator",
description="""
Product listing generator supporting both images and videos.
Sample testing guide:
1. Image Upload (/upload/):
Categories:
- Electronics: "Sony WH-1000XM4 Headphones"
- Fashion: "Nike Air Max 270"
- Home Decor: "Scandinavian Floor Lamp"
2. Video Upload (/upload/video/):
Categories:
- Electronics: "iPhone 15 Review", "MacBook Pro Review"
- Fashion: "Nike Collection", "Adidas Shoes"
- Beauty: "Makeup Tutorial", "Skincare Routine"
"""
)
# Load .env file
load_dotenv()
# MongoDB setup
MONGODB_URL = os.getenv("MONGODB_URL")
client = AsyncIOMotorClient(MONGODB_URL)
db = client.social_media_products
# Image Collections
product_collection = db["products"]
listing_collection = db["listings"]
analytics_collection = db["analytics"]
review_collection = db["reviews"]
# Video Collections
video_collection = db["videos"]
video_listings_collection = db["video_listings"]
video_analytics_collection = db["video_analytics"]
# Include Routers
app.include_router(image.router, prefix="/upload/image", tags=["Image"])
app.include_router(video.router, prefix="/upload/video", tags=["Video"])
app.include_router(combined.router, prefix="/search/all", tags=["Combined"])
# Static files and templates setup
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
@app.get("/", response_class=HTMLResponse)
async def home(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
@app.get("/health",tags=["Monitoring"])
async def health_check():
"""
Health check endpoint to monitor MongoDB connection and response time.
"""
start_time = time()
try:
await db.command("ping")
db_status = "connected"
except Exception as e:
db_status = f"disconnected: {str(e)}"
response_time = round((time() - start_time) * 1000, 2)
status_code = 200 if db_status == "connected" else 500
# Log status
logger.info(f"Health Check: DB status - {db_status}, Response Time - {response_time}ms")
return JSONResponse(
content={
"status": "healthy" if db_status == "connected" else "unhealthy",
"db_status": db_status,
"response_time_ms": response_time
},
status_code=status_code
)
if __name__ == "__main__":
import uvicorn
uvicorn.run("main:app", host="127.0.0.2", port=8002, reload=True)