sarva-ai-back / main.py
navpan2's picture
Update main.py
ec7f7d1 verified
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from motor.motor_asyncio import AsyncIOMotorClient
from typing import List
from fastapi.middleware.cors import CORSMiddleware
import os
# MongoDB connection URI
MONGO_URI = os.getenv("MONGO_URI")
# 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):
key: str
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."}
@app.get("/check/{plant_name}")
async def get_disease(plant_name: str):
return {"plantName":plant_name}
# API endpoint to fetch disease details by plant name
@app.get("/disease/{plant_name}", response_model=Disease)
async def get_disease(plant_name: str):
disease = await disease_collection.find_one({"key": plant_name})
if disease is None:
raise HTTPException(status_code=404, detail="Disease not found")
disease_data = Disease(
key=disease["key"],
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
# New endpoint to fetch disease details by key
@app.get("/kotlinback/{key}", response_model=Disease)
async def get_disease_by_key(key: str):
disease = await disease_collection.find_one({"key": key})
if disease is None:
raise HTTPException(status_code=404, detail="Disease not found")
disease_data = Disease(
key=disease["key"],
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)