Spaces:
Sleeping
Sleeping
add api auth
Browse files- .gitignore +3 -1
- Dockerfile +0 -1
- main.py +55 -9
.gitignore
CHANGED
|
@@ -2,4 +2,6 @@ __pycache__/
|
|
| 2 |
.venv/
|
| 3 |
|
| 4 |
weights/
|
| 5 |
-
.env
|
|
|
|
|
|
|
|
|
| 2 |
.venv/
|
| 3 |
|
| 4 |
weights/
|
| 5 |
+
.env
|
| 6 |
+
|
| 7 |
+
*test*
|
Dockerfile
CHANGED
|
@@ -9,7 +9,6 @@ ENV PATH="$VIRTUAL_ENV/bin:$PATH"
|
|
| 9 |
|
| 10 |
COPY requirements.txt .
|
| 11 |
RUN uv pip install --no-cache -r requirements.txt
|
| 12 |
-
# RUN uv pip install numpy --upgrade
|
| 13 |
|
| 14 |
COPY . .
|
| 15 |
|
|
|
|
| 9 |
|
| 10 |
COPY requirements.txt .
|
| 11 |
RUN uv pip install --no-cache -r requirements.txt
|
|
|
|
| 12 |
|
| 13 |
COPY . .
|
| 14 |
|
main.py
CHANGED
|
@@ -1,15 +1,23 @@
|
|
| 1 |
import os
|
| 2 |
import atexit
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import tensorflow as tf
|
| 6 |
from pathlib import Path
|
| 7 |
from pydantic import BaseModel
|
| 8 |
-
from fastapi import FastAPI, UploadFile
|
| 9 |
from starlette.responses import RedirectResponse
|
| 10 |
from database import update_data_field, cleanup
|
|
|
|
|
|
|
| 11 |
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
class Item(BaseModel):
|
| 14 |
uuid: str
|
| 15 |
time: str
|
|
@@ -18,10 +26,19 @@ class Item(BaseModel):
|
|
| 18 |
soil_moisture: str
|
| 19 |
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
mongo_string = os.environ.get("MONGO_STRING")
|
| 22 |
app = FastAPI(title="Dristhi Backend", description="APIs for Dristhi", version="1.0")
|
| 23 |
|
| 24 |
-
|
| 25 |
def load_model():
|
| 26 |
model_path = (
|
| 27 |
Path(__file__).resolve().parent.parent
|
|
@@ -29,17 +46,18 @@ def load_model():
|
|
| 29 |
/ "plant_disease_classifier.h5"
|
| 30 |
)
|
| 31 |
model = tf.keras.models.load_model(model_path)
|
| 32 |
-
|
| 33 |
return model
|
| 34 |
|
| 35 |
-
|
| 36 |
@app.get("/")
|
| 37 |
-
def display() -> str:
|
| 38 |
response = RedirectResponse(url='https://github.com/epicshi')
|
| 39 |
return response
|
| 40 |
|
| 41 |
@app.post("/predict")
|
| 42 |
-
def predict(
|
|
|
|
|
|
|
|
|
|
| 43 |
model = load_model()
|
| 44 |
|
| 45 |
original_image = Image.open(file.file).convert("RGB")
|
|
@@ -48,14 +66,16 @@ def predict(file: UploadFile) -> str:
|
|
| 48 |
preds = model.predict(np.expand_dims(preprocessed_image, axis=0))
|
| 49 |
|
| 50 |
labels = ["Healthy", "Powdery", "Rust"]
|
| 51 |
-
|
| 52 |
preds_class = np.argmax(preds)
|
| 53 |
preds_label = labels[preds_class]
|
| 54 |
|
| 55 |
return preds_label
|
| 56 |
|
| 57 |
@app.post("/update-data")
|
| 58 |
-
def update_data(
|
|
|
|
|
|
|
|
|
|
| 59 |
if (
|
| 60 |
item.humidity.lower() == "nan"
|
| 61 |
or item.temperature.lower() == "nan"
|
|
@@ -73,11 +93,37 @@ def update_data(item: Item) -> str:
|
|
| 73 |
},
|
| 74 |
)
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|
| 78 |
import uvicorn
|
| 79 |
try:
|
| 80 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 81 |
finally:
|
| 82 |
cleanup()
|
| 83 |
atexit.register(cleanup)
|
|
|
|
| 1 |
import os
|
| 2 |
import atexit
|
| 3 |
import numpy as np
|
| 4 |
+
import json, requests
|
| 5 |
from PIL import Image
|
| 6 |
import tensorflow as tf
|
| 7 |
from pathlib import Path
|
| 8 |
from pydantic import BaseModel
|
|
|
|
| 9 |
from starlette.responses import RedirectResponse
|
| 10 |
from database import update_data_field, cleanup
|
| 11 |
+
from fastapi import FastAPI, UploadFile, HTTPException, Security, Depends
|
| 12 |
+
from fastapi.security import APIKeyHeader
|
| 13 |
|
| 14 |
|
| 15 |
+
DRISHTI_AUTH_KEY = os.environ.get("DRISHTI_AUTH_KEY")
|
| 16 |
+
DRISHTI_AUTH_KEY_NAME = os.environ.get("DRISHTI_AUTH_KEY_NAME")
|
| 17 |
+
|
| 18 |
+
if not DRISHTI_AUTH_KEY:
|
| 19 |
+
raise ValueError("DRISHTI_AUTH_KEY environment variable must be set")
|
| 20 |
+
|
| 21 |
class Item(BaseModel):
|
| 22 |
uuid: str
|
| 23 |
time: str
|
|
|
|
| 26 |
soil_moisture: str
|
| 27 |
|
| 28 |
|
| 29 |
+
api_key_header = APIKeyHeader(name=DRISHTI_AUTH_KEY_NAME, auto_error=True)
|
| 30 |
+
|
| 31 |
+
async def verify_api_key(api_key: str = Security(api_key_header)):
|
| 32 |
+
if api_key != DRISHTI_AUTH_KEY:
|
| 33 |
+
raise HTTPException(
|
| 34 |
+
status_code=403,
|
| 35 |
+
detail="Invalid API key",
|
| 36 |
+
)
|
| 37 |
+
return api_key
|
| 38 |
+
|
| 39 |
mongo_string = os.environ.get("MONGO_STRING")
|
| 40 |
app = FastAPI(title="Dristhi Backend", description="APIs for Dristhi", version="1.0")
|
| 41 |
|
|
|
|
| 42 |
def load_model():
|
| 43 |
model_path = (
|
| 44 |
Path(__file__).resolve().parent.parent
|
|
|
|
| 46 |
/ "plant_disease_classifier.h5"
|
| 47 |
)
|
| 48 |
model = tf.keras.models.load_model(model_path)
|
|
|
|
| 49 |
return model
|
| 50 |
|
|
|
|
| 51 |
@app.get("/")
|
| 52 |
+
async def display() -> str:
|
| 53 |
response = RedirectResponse(url='https://github.com/epicshi')
|
| 54 |
return response
|
| 55 |
|
| 56 |
@app.post("/predict")
|
| 57 |
+
async def predict(
|
| 58 |
+
file: UploadFile,
|
| 59 |
+
api_key: str = Depends(verify_api_key)
|
| 60 |
+
) -> str:
|
| 61 |
model = load_model()
|
| 62 |
|
| 63 |
original_image = Image.open(file.file).convert("RGB")
|
|
|
|
| 66 |
preds = model.predict(np.expand_dims(preprocessed_image, axis=0))
|
| 67 |
|
| 68 |
labels = ["Healthy", "Powdery", "Rust"]
|
|
|
|
| 69 |
preds_class = np.argmax(preds)
|
| 70 |
preds_label = labels[preds_class]
|
| 71 |
|
| 72 |
return preds_label
|
| 73 |
|
| 74 |
@app.post("/update-data")
|
| 75 |
+
async def update_data(
|
| 76 |
+
item: Item,
|
| 77 |
+
api_key: str = Depends(verify_api_key)
|
| 78 |
+
) -> str:
|
| 79 |
if (
|
| 80 |
item.humidity.lower() == "nan"
|
| 81 |
or item.temperature.lower() == "nan"
|
|
|
|
| 93 |
},
|
| 94 |
)
|
| 95 |
|
| 96 |
+
@app.get("/fetch-news")
|
| 97 |
+
async def fetch_news(
|
| 98 |
+
api_key: str = Depends(verify_api_key)
|
| 99 |
+
):
|
| 100 |
+
news_api_key = os.environ.get("NEWS_API_KEY")
|
| 101 |
+
if not news_api_key:
|
| 102 |
+
raise HTTPException(
|
| 103 |
+
status_code=500,
|
| 104 |
+
detail="News API key not configured"
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
response = requests.get(
|
| 108 |
+
'https://newsapi.org/v2/everything',
|
| 109 |
+
params={
|
| 110 |
+
'q': '(farmer OR agriculture OR "rural development" OR "farm laws" OR kisaan OR kisan) AND (India OR Maharashtra OR Punjab OR Karnataka OR "Uttar Pradesh")',
|
| 111 |
+
'language': 'en',
|
| 112 |
+
'sortBy': 'publishedAt',
|
| 113 |
+
'apiKey': news_api_key
|
| 114 |
+
}
|
| 115 |
+
)
|
| 116 |
+
if response.status_code != 200:
|
| 117 |
+
raise HTTPException(
|
| 118 |
+
status_code=500,
|
| 119 |
+
detail="News API request failed"
|
| 120 |
+
)
|
| 121 |
+
return json.loads(response.content)
|
| 122 |
|
| 123 |
if __name__ == "__main__":
|
| 124 |
import uvicorn
|
| 125 |
try:
|
| 126 |
+
uvicorn.run("main:app", host="0.0.0.0", port=7860)
|
| 127 |
finally:
|
| 128 |
cleanup()
|
| 129 |
atexit.register(cleanup)
|