Files
ds_sabaproject/ds_apis.py
T
timothyafolami d5cdeec299 new data and api
2024-11-13 00:41:30 +01:00

66 lines
2.0 KiB
Python

import sys, os
from fastapi import FastAPI, Form, Body
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, JSONResponse
from typing import List, Dict
import uvicorn
from pydantic import BaseModel
from utils import product_categorizer
from social_media_collection import get_all_influencer_data
from names_collection import get_all_names
app = FastAPI()
# Define allowed origins for CORS
origins = [
"http://localhost:5300",
"http://localhost:3000",
# Add other allowed origins here
]
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class ProductRequest(BaseModel):
products: List[str]
@app.post("/api/categorize-products/")
async def categorize_products(request: ProductRequest):
categorized_output = product_categorizer(request.products)
return JSONResponse(content={"categorized_products": categorized_output})
class InfluencerData(BaseModel):
influencer_names: List[str]
category : str
@app.post("/api/influencer-data/")
async def influencers_data(request: InfluencerData):
collected_data = get_all_influencer_data(request.influencer_names, request.category)
return JSONResponse(content={"categorized_products": collected_data})
class CategoryInflencersData(BaseModel):
category: str
@app.post("/api/category-influencer-data")
async def category_influencers_data(request: CategoryInflencersData):
collected_data = get_all_names(request.category)
# extracting influencer names
influencer_names = collected_data[request.category]['names']
# now collecting the influencer data
collected_data = get_all_influencer_data(influencer_names, request.category)
return JSONResponse(content={"categorized_products": collected_data})
# Example of how to run the FastAPI server with Uvicorn
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000)