Spaces:
Running
Running
Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from motor.motor_asyncio import AsyncIOMotorClient
|
4 |
+
from typing import List, Optional
|
5 |
+
from fastapi.middleware.cors import CORSMiddleware
|
6 |
+
|
7 |
+
# MongoDB connection URI
|
8 |
+
MONGO_URI = "mongodb+srv://npanchayan:ramnagar@cluster0.7stwm.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"
|
9 |
+
|
10 |
+
# Initialize FastAPI app
|
11 |
+
app = FastAPI()
|
12 |
+
|
13 |
+
# Allow CORS for all origins
|
14 |
+
app.add_middleware(
|
15 |
+
CORSMiddleware,
|
16 |
+
allow_origins=["*"], # Allows all origins, change this to more specific if needed
|
17 |
+
allow_credentials=True,
|
18 |
+
allow_methods=["*"],
|
19 |
+
allow_headers=["*"],
|
20 |
+
)
|
21 |
+
|
22 |
+
# MongoDB client
|
23 |
+
client = AsyncIOMotorClient(MONGO_URI)
|
24 |
+
db = client.disease # MongoDB database
|
25 |
+
disease_collection = db.diseases # MongoDB collection
|
26 |
+
|
27 |
+
# Pydantic model to represent the Disease schema
|
28 |
+
class Disease(BaseModel):
|
29 |
+
diseaseName: str
|
30 |
+
description: str
|
31 |
+
cause: str
|
32 |
+
symptoms: List[str]
|
33 |
+
treatments: List[str]
|
34 |
+
preventionTips: Optional[List[str]] = []
|
35 |
+
imageURL: Optional[str] = None
|
36 |
+
createdAt: str
|
37 |
+
|
38 |
+
# API endpoint to fetch disease details by name
|
39 |
+
@app.get("/disease/{name}", response_model=Disease)
|
40 |
+
async def get_disease(name: str):
|
41 |
+
# Fetch the disease from MongoDB
|
42 |
+
disease = await disease_collection.find_one({"diseaseName": name})
|
43 |
+
|
44 |
+
if disease is None:
|
45 |
+
raise HTTPException(status_code=404, detail="Disease not found")
|
46 |
+
|
47 |
+
# Convert MongoDB document to Pydantic model
|
48 |
+
disease_data = Disease(
|
49 |
+
diseaseName=disease["diseaseName"],
|
50 |
+
description=disease["description"],
|
51 |
+
cause=disease["cause"],
|
52 |
+
symptoms=disease["symptoms"],
|
53 |
+
treatments=disease["treatments"],
|
54 |
+
preventionTips=disease.get("preventionTips", []),
|
55 |
+
imageURL=disease.get("imageURL", None),
|
56 |
+
createdAt=disease["createdAt"].isoformat(),
|
57 |
+
)
|
58 |
+
|
59 |
+
return disease_data
|
60 |
+
|
61 |
+
# Run the FastAPI server using Uvicorn
|
62 |
+
if __name__ == "__main__":
|
63 |
+
import uvicorn
|
64 |
+
uvicorn.run(app, host="0.0.0.0", port=5000)
|