Files
ds_sabaproject/ds_apis.py
T

67 lines
2.0 KiB
Python
Raw Normal View History

2024-10-22 23:37:05 +01:00
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
2024-10-28 23:20:31 +01:00
import uvicorn
2024-10-22 23:37:05 +01:00
from pydantic import BaseModel
from utils import product_categorizer
2024-10-28 23:20:31 +01:00
from social_media_collection import get_all_influencer_data
2024-11-13 00:41:30 +01:00
from names_collection import get_all_names
2024-10-22 23:37:05 +01:00
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]
2024-10-28 23:20:31 +01:00
@app.post("/api/categorize-products/")
2024-10-22 23:37:05 +01:00
async def categorize_products(request: ProductRequest):
categorized_output = product_categorizer(request.products)
return JSONResponse(content={"categorized_products": categorized_output})
2024-10-28 23:20:31 +01:00
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})
2024-11-13 00:41:30 +01:00
class CategoryInflencersData(BaseModel):
category: str
2024-10-28 23:20:31 +01:00
2024-11-13 00:41:30 +01:00
@app.post("/api/category-influencer-data")
async def category_influencers_data(request: CategoryInflencersData):
2024-11-27 20:44:26 +01:00
collected_data = get_all_names([request.category])
print(collected_data)
2024-11-13 00:41:30 +01:00
# 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})
2024-10-28 23:20:31 +01:00
# Example of how to run the FastAPI server with Uvicorn
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000)