Spaces:
Runtime error
Runtime error
Commit ·
3cfb6f8
1
Parent(s): 885f5bc
production
Browse files
main.py
CHANGED
|
@@ -1,32 +1,49 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
-
|
| 4 |
-
# from firebase_admin import auth, storage
|
| 5 |
from pydantic import BaseModel
|
| 6 |
-
from typing import
|
| 7 |
import os
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
from source import main
|
|
|
|
| 10 |
|
| 11 |
load_dotenv()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
app = FastAPI()
|
| 13 |
|
|
|
|
| 14 |
app.add_middleware(
|
| 15 |
CORSMiddleware,
|
| 16 |
-
allow_origins=["https://verbisense.vercel.app"],
|
| 17 |
-
allow_credentials=True,
|
| 18 |
-
allow_methods=[
|
| 19 |
-
allow_headers=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
)
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
class QueryChat(BaseModel):
|
| 23 |
userId: str
|
| 24 |
files: List
|
| 25 |
query: str
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# bucket = storage.bucket("verbisense.appspot.com")
|
| 29 |
-
|
| 30 |
@app.get("/")
|
| 31 |
def read_root():
|
| 32 |
return {"message": "Welcome to Verbisense!"}
|
|
@@ -34,18 +51,19 @@ def read_root():
|
|
| 34 |
@app.post("/chat")
|
| 35 |
async def chat(data: QueryChat):
|
| 36 |
try:
|
| 37 |
-
print("userId
|
| 38 |
-
print("files
|
| 39 |
-
print("query
|
| 40 |
-
|
| 41 |
-
response = main(data.files,data.query)
|
| 42 |
-
|
| 43 |
-
print("\n" + "="*50)
|
| 44 |
print(response)
|
| 45 |
-
print("="*50)
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
return {"query":data.query,"response":response}
|
| 49 |
-
|
| 50 |
except Exception as e:
|
| 51 |
-
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Request
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from fastapi.middleware.trustedhost import TrustedHostMiddleware
|
|
|
|
| 4 |
from pydantic import BaseModel
|
| 5 |
+
from typing import List
|
| 6 |
import os
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
from source import main
|
| 9 |
+
import logging
|
| 10 |
|
| 11 |
load_dotenv()
|
| 12 |
+
|
| 13 |
+
# Setup logging
|
| 14 |
+
logging.basicConfig(filename="error.log", level=logging.ERROR)
|
| 15 |
+
|
| 16 |
app = FastAPI()
|
| 17 |
|
| 18 |
+
# CORS Configuration
|
| 19 |
app.add_middleware(
|
| 20 |
CORSMiddleware,
|
| 21 |
+
allow_origins=["https://verbisense.vercel.app"], # Only allow frontend origin
|
| 22 |
+
allow_credentials=True,
|
| 23 |
+
allow_methods=["*"],
|
| 24 |
+
allow_headers=["*"],
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# Trusted Hosts
|
| 28 |
+
app.add_middleware(
|
| 29 |
+
TrustedHostMiddleware, allowed_hosts=["verbisense.vercel.app", "harish20205-verbisense.hf.space"]
|
| 30 |
)
|
| 31 |
|
| 32 |
+
# Middleware to Block Unauthorized Origins
|
| 33 |
+
@app.middleware("http")
|
| 34 |
+
async def block_unauthorized_origins(request: Request, call_next):
|
| 35 |
+
origin = request.headers.get("origin")
|
| 36 |
+
if origin and origin != "https://verbisense.vercel.app":
|
| 37 |
+
raise HTTPException(status_code=403, detail="Unauthorized origin")
|
| 38 |
+
response = await call_next(request)
|
| 39 |
+
return response
|
| 40 |
+
|
| 41 |
+
# Request Model
|
| 42 |
class QueryChat(BaseModel):
|
| 43 |
userId: str
|
| 44 |
files: List
|
| 45 |
query: str
|
| 46 |
+
|
|
|
|
|
|
|
|
|
|
| 47 |
@app.get("/")
|
| 48 |
def read_root():
|
| 49 |
return {"message": "Welcome to Verbisense!"}
|
|
|
|
| 51 |
@app.post("/chat")
|
| 52 |
async def chat(data: QueryChat):
|
| 53 |
try:
|
| 54 |
+
print(f"userId: {data.userId}")
|
| 55 |
+
print(f"files: {data.files}")
|
| 56 |
+
print(f"query: {data.query}")
|
| 57 |
+
|
| 58 |
+
response = main(data.files, data.query)
|
| 59 |
+
|
| 60 |
+
print("\n" + "=" * 50)
|
| 61 |
print(response)
|
| 62 |
+
print("=" * 50)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
return {"query": data.query, "response": response}
|
| 66 |
+
|
| 67 |
except Exception as e:
|
| 68 |
+
logging.error(f"An error occurred: {e}")
|
| 69 |
+
raise HTTPException(status_code=500, detail="An internal error occurred.")
|