Spaces:
Sleeping
Sleeping
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from contextlib import asynccontextmanager
|
| 4 |
+
from collections import defaultdict
|
| 5 |
+
import os, json, base64, time
|
| 6 |
+
|
| 7 |
+
import firebase_admin
|
| 8 |
+
from firebase_admin import credentials, firestore
|
| 9 |
+
from sentence_transformers import CrossEncoder
|
| 10 |
+
|
| 11 |
+
from community import router as community_router
|
| 12 |
+
from algorithm import UpnisoAlgorithmPipeline
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
db = None
|
| 16 |
+
pipeline = None
|
| 17 |
+
ranker = None
|
| 18 |
+
api_requests = defaultdict(list)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
@asynccontextmanager
|
| 22 |
+
async def lifespan(app: FastAPI):
|
| 23 |
+
global db, pipeline, ranker
|
| 24 |
+
|
| 25 |
+
# Firebase
|
| 26 |
+
key = os.getenv("FIREBASE_KEY_B64")
|
| 27 |
+
if key and not firebase_admin._apps:
|
| 28 |
+
cred = credentials.Certificate(json.loads(base64.b64decode(key)))
|
| 29 |
+
firebase_admin.initialize_app(cred)
|
| 30 |
+
db = firestore.client()
|
| 31 |
+
print("✅ Firebase connected")
|
| 32 |
+
|
| 33 |
+
# Core algorithm
|
| 34 |
+
pipeline = UpnisoAlgorithmPipeline()
|
| 35 |
+
print("✅ Algorithm loaded")
|
| 36 |
+
|
| 37 |
+
# AI ranker
|
| 38 |
+
ranker = CrossEncoder("cross-encoder/ms-marco-MiniLM-L6-v2")
|
| 39 |
+
print("✅ AI model loaded")
|
| 40 |
+
|
| 41 |
+
yield
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
app = FastAPI(title="Upniso Backend", version="2.0", lifespan=lifespan)
|
| 45 |
+
|
| 46 |
+
app.add_middleware(
|
| 47 |
+
CORSMiddleware,
|
| 48 |
+
allow_origins=["*"],
|
| 49 |
+
allow_methods=["*"],
|
| 50 |
+
allow_headers=["*"],
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
@app.get("/")
|
| 55 |
+
def health():
|
| 56 |
+
return {"status": "live", "time": time.time()}
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
# Community / Marketplace API
|
| 60 |
+
app.include_router(community_router, prefix="/api/v1")
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
@app.get("/feed")
|
| 64 |
+
async def feed():
|
| 65 |
+
if not db or not pipeline:
|
| 66 |
+
return {"status": "error", "feed": []}
|
| 67 |
+
|
| 68 |
+
creators = {d.id: d.to_dict() for d in db.collection("creators").stream()}
|
| 69 |
+
posts = {
|
| 70 |
+
d.id: d.to_dict()
|
| 71 |
+
for d in db.collection("posts").where("is_draft", "==", False).limit(50).stream()
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
ranked = pipeline.run_simulation(creators, posts)
|
| 75 |
+
|
| 76 |
+
if ranker and ranked:
|
| 77 |
+
query = "High quality merit-based content"
|
| 78 |
+
pairs = [[query, r.get("description", "")] for r in ranked]
|
| 79 |
+
scores = ranker.predict(pairs)
|
| 80 |
+
for i, r in enumerate(ranked):
|
| 81 |
+
r["ai_score"] = float(scores[i])
|
| 82 |
+
|
| 83 |
+
return {"status": "success", "count": len(ranked), "feed": ranked}
|