File size: 2,510 Bytes
f44e85b
 
 
 
 
4686d63
 
f44e85b
4686d63
f44e85b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4686d63
f44e85b
4686d63
 
 
 
 
 
 
 
 
 
 
 
 
f44e85b
d971f44
 
 
 
 
4686d63
 
 
f44e85b
4686d63
 
f44e85b
 
4686d63
f44e85b
 
4686d63
 
 
 
 
 
 
 
 
 
 
 
f44e85b
4686d63
f44e85b
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from motor.motor_asyncio import AsyncIOMotorClient
from typing import List, Optional
from fastapi.middleware.cors import CORSMiddleware
import os

# MongoDB connection URI
MONGO_URI = os.getenv("MONGO_URI")  # Replace with your MongoDB URI if not using environment variables

# Initialize FastAPI app
app = FastAPI()

# Allow CORS for all origins
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Allows all origins, change this to more specific if needed
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# MongoDB client
client = AsyncIOMotorClient(MONGO_URI)
db = client.disease  # MongoDB database
disease_collection = db.diseases  # MongoDB collection

# Pydantic model to represent the Disease schema
class DiseaseDesc(BaseModel):
    diseaseName: str
    symptoms: str
    diseaseCauses: str

class DiseaseRemedy(BaseModel):
    title: str
    diseaseRemedyShortDesc: str
    diseaseRemedy: str

class Disease(BaseModel):
    plantName: str
    botanicalName: str
    diseaseDesc: DiseaseDesc
    diseaseRemedyList: List[DiseaseRemedy]

# Root route to show a welcome message
@app.get("/")
def welcome():
    return {"message": "Welcome to the Disease Information API! Visit /docs for API documentation."}

# API endpoint to fetch disease details by plant name
@app.get("/disease/{plant_name}", response_model=Disease)
async def get_disease(plant_name: str):
    # Fetch the disease from MongoDB
    disease = await disease_collection.find_one({"plantName": plant_name})

    if disease is None:
        raise HTTPException(status_code=404, detail="Disease not found")

    # Convert MongoDB document to Pydantic model
    disease_data = Disease(
        plantName=disease["plantName"],
        botanicalName=disease["botanicalName"],
        diseaseDesc=DiseaseDesc(
            diseaseName=disease["diseaseDesc"]["diseaseName"],
            symptoms=disease["diseaseDesc"]["symptoms"],
            diseaseCauses=disease["diseaseDesc"]["diseaseCauses"]
        ),
        diseaseRemedyList=[DiseaseRemedy(
            title=remedy["title"],
            diseaseRemedyShortDesc=remedy["diseaseRemedyShortDesc"],
            diseaseRemedy=remedy["diseaseRemedy"]
        ) for remedy in disease["diseaseRemedyList"]]
    )

    return disease_data

# Run the FastAPI server using Uvicorn
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=5000)