navpan2 commited on
Commit
4686d63
·
verified ·
1 Parent(s): f1cf749

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +36 -26
main.py CHANGED
@@ -3,9 +3,10 @@ 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
- import os
 
7
  # MongoDB connection URI
8
- MONGO_URI = os.getenv("MONGO_URI") #""
9
 
10
  # Initialize FastAPI app
11
  app = FastAPI()
@@ -25,43 +26,52 @@ 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
  # Root route to show a welcome message
39
  @app.get("/")
40
  def welcome():
41
  return {"message": "Welcome to the Disease Information API! Visit /docs for API documentation."}
42
 
43
-
44
- # API endpoint to fetch disease details by name
45
- @app.get("/disease/{name}", response_model=Disease)
46
- async def get_disease(name: str):
47
  # Fetch the disease from MongoDB
48
- disease = await disease_collection.find_one({"diseaseName": name})
49
-
50
  if disease is None:
51
  raise HTTPException(status_code=404, detail="Disease not found")
52
-
53
  # Convert MongoDB document to Pydantic model
54
  disease_data = Disease(
55
- diseaseName=disease["diseaseName"],
56
- description=disease["description"],
57
- cause=disease["cause"],
58
- symptoms=disease["symptoms"],
59
- treatments=disease["treatments"],
60
- preventionTips=disease.get("preventionTips", []),
61
- imageURL=disease.get("imageURL", None),
62
- createdAt=disease["createdAt"].isoformat(),
 
 
 
 
63
  )
64
-
65
  return disease_data
66
 
67
  # Run the FastAPI server using Uvicorn
 
3
  from motor.motor_asyncio import AsyncIOMotorClient
4
  from typing import List, Optional
5
  from fastapi.middleware.cors import CORSMiddleware
6
+ import os
7
+
8
  # MongoDB connection URI
9
+ MONGO_URI = os.getenv("MONGO_URI") # Replace with your MongoDB URI if not using environment variables
10
 
11
  # Initialize FastAPI app
12
  app = FastAPI()
 
26
  disease_collection = db.diseases # MongoDB collection
27
 
28
  # Pydantic model to represent the Disease schema
29
+ class DiseaseDesc(BaseModel):
30
  diseaseName: str
31
+ symptoms: str
32
+ diseaseCauses: str
33
+
34
+ class DiseaseRemedy(BaseModel):
35
+ title: str
36
+ diseaseRemedyShortDesc: str
37
+ diseaseRemedy: str
38
+
39
+ class Disease(BaseModel):
40
+ plantName: str
41
+ botanicalName: str
42
+ diseaseDesc: DiseaseDesc
43
+ diseaseRemedyList: List[DiseaseRemedy]
44
 
45
  # Root route to show a welcome message
46
  @app.get("/")
47
  def welcome():
48
  return {"message": "Welcome to the Disease Information API! Visit /docs for API documentation."}
49
 
50
+ # API endpoint to fetch disease details by plant name
51
+ @app.get("/disease/{plant_name}", response_model=Disease)
52
+ async def get_disease(plant_name: str):
 
53
  # Fetch the disease from MongoDB
54
+ disease = await disease_collection.find_one({"plantName": plant_name})
55
+
56
  if disease is None:
57
  raise HTTPException(status_code=404, detail="Disease not found")
58
+
59
  # Convert MongoDB document to Pydantic model
60
  disease_data = Disease(
61
+ plantName=disease["plantName"],
62
+ botanicalName=disease["botanicalName"],
63
+ diseaseDesc=DiseaseDesc(
64
+ diseaseName=disease["diseaseDesc"]["diseaseName"],
65
+ symptoms=disease["diseaseDesc"]["symptoms"],
66
+ diseaseCauses=disease["diseaseDesc"]["diseaseCauses"]
67
+ ),
68
+ diseaseRemedyList=[DiseaseRemedy(
69
+ title=remedy["title"],
70
+ diseaseRemedyShortDesc=remedy["diseaseRemedyShortDesc"],
71
+ diseaseRemedy=remedy["diseaseRemedy"]
72
+ ) for remedy in disease["diseaseRemedyList"]]
73
  )
74
+
75
  return disease_data
76
 
77
  # Run the FastAPI server using Uvicorn