Spaces:
Running
Running
File size: 1,856 Bytes
3cfb6f8 c8c7a9e 3cfb6f8 c8c7a9e 3cfb6f8 c8c7a9e 7a1f6ff c8c7a9e 3cfb6f8 c8c7a9e 7a1f6ff 3cfb6f8 c8c7a9e 3cfb6f8 c8c7a9e 3cfb6f8 c8c7a9e 3cfb6f8 c8c7a9e 3cfb6f8 c8c7a9e 3cfb6f8 c8c7a9e 3cfb6f8 c8c7a9e 3cfb6f8 |
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 |
from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.middleware.trustedhost import TrustedHostMiddleware
from pydantic import BaseModel
from typing import List
import os
from dotenv import load_dotenv
from source import main
import logging
load_dotenv()
# Setup logging
logging.basicConfig(filename="error.log", level=logging.ERROR)
app = FastAPI()
# CORS Configuration
app.add_middleware(
CORSMiddleware,
allow_origins=["https://verbisense.vercel.app"], # Only allow frontend origin
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Trusted Hosts
app.add_middleware(
TrustedHostMiddleware, allowed_hosts=["verbisense.vercel.app", "harish20205-verbisense.hf.space"]
)
# Middleware to Block Unauthorized Origins
@app.middleware("http")
async def block_unauthorized_origins(request: Request, call_next):
origin = request.headers.get("origin")
if origin and origin != "https://verbisense.vercel.app":
raise HTTPException(status_code=403, detail="Unauthorized origin")
response = await call_next(request)
return response
# Request Model
class QueryChat(BaseModel):
userId: str
files: List
query: str
@app.get("/")
def read_root():
return {"message": "Welcome to Verbisense!"}
@app.post("/chat")
async def chat(data: QueryChat):
try:
print(f"userId: {data.userId}")
print(f"files: {data.files}")
print(f"query: {data.query}")
response = main(data.files, data.query)
print("\n" + "=" * 50)
print(response)
print("=" * 50)
return {"query": data.query, "response": response}
except Exception as e:
logging.error(f"An error occurred: {e}")
raise HTTPException(status_code=500, detail="An internal error occurred.")
|