Spaces:
Sleeping
Sleeping
Commit
ยท
fb015b4
1
Parent(s):
dd43f94
Updated
Browse files
app.py
CHANGED
|
@@ -9,7 +9,7 @@ from PIL import Image
|
|
| 9 |
from vector import query_vector
|
| 10 |
|
| 11 |
# ==============================
|
| 12 |
-
#
|
| 13 |
# ==============================
|
| 14 |
logging.basicConfig(level=logging.INFO)
|
| 15 |
logger = logging.getLogger("AgriCopilot")
|
|
@@ -24,7 +24,7 @@ async def root():
|
|
| 24 |
return {"status": "AgriCopilot AI Backend is working perfectly"}
|
| 25 |
|
| 26 |
# ==============================
|
| 27 |
-
#
|
| 28 |
# ==============================
|
| 29 |
PROJECT_API_KEY = os.getenv("PROJECT_API_KEY", "agricopilot404")
|
| 30 |
|
|
@@ -66,15 +66,29 @@ class VectorRequest(BaseModel):
|
|
| 66 |
# ==============================
|
| 67 |
# HuggingFace Pipelines
|
| 68 |
# ==============================
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
# ==============================
|
| 80 |
# Helper Functions
|
|
@@ -102,43 +116,33 @@ def run_crop_doctor(image_bytes: bytes, symptoms: str):
|
|
| 102 |
return f"โ ๏ธ Unexpected vision model error: {str(e)}"
|
| 103 |
|
| 104 |
# ==============================
|
| 105 |
-
#
|
| 106 |
# ==============================
|
| 107 |
-
|
| 108 |
-
# ๐ฑ Crop Doctor
|
| 109 |
@app.post("/crop-doctor")
|
| 110 |
-
async def crop_doctor(
|
| 111 |
-
symptoms: str = Header(...),
|
| 112 |
-
image: UploadFile = File(...),
|
| 113 |
-
authorization: str | None = Header(None)
|
| 114 |
-
):
|
| 115 |
check_auth(authorization)
|
| 116 |
image_bytes = await image.read()
|
| 117 |
diagnosis = run_crop_doctor(image_bytes, symptoms)
|
| 118 |
return {"diagnosis": diagnosis}
|
| 119 |
|
| 120 |
-
# ๐ฃ Multilingual Chat
|
| 121 |
@app.post("/multilingual-chat")
|
| 122 |
async def multilingual_chat(req: ChatRequest, authorization: str | None = Header(None)):
|
| 123 |
check_auth(authorization)
|
| 124 |
reply = run_conversational(chat_pipe, req.query)
|
| 125 |
return {"reply": reply}
|
| 126 |
|
| 127 |
-
# ๐ช Disaster Summarizer
|
| 128 |
@app.post("/disaster-summarizer")
|
| 129 |
async def disaster_summarizer(req: DisasterRequest, authorization: str | None = Header(None)):
|
| 130 |
check_auth(authorization)
|
| 131 |
summary = run_conversational(disaster_pipe, req.report)
|
| 132 |
return {"summary": summary}
|
| 133 |
|
| 134 |
-
# ๐ Marketplace Recommendation
|
| 135 |
@app.post("/marketplace")
|
| 136 |
async def marketplace(req: MarketRequest, authorization: str | None = Header(None)):
|
| 137 |
check_auth(authorization)
|
| 138 |
recommendation = run_conversational(market_pipe, req.product)
|
| 139 |
return {"recommendation": recommendation}
|
| 140 |
|
| 141 |
-
# ๐ Vector Search
|
| 142 |
@app.post("/vector-search")
|
| 143 |
async def vector_search(req: VectorRequest, authorization: str | None = Header(None)):
|
| 144 |
check_auth(authorization)
|
|
|
|
| 9 |
from vector import query_vector
|
| 10 |
|
| 11 |
# ==============================
|
| 12 |
+
# Logging
|
| 13 |
# ==============================
|
| 14 |
logging.basicConfig(level=logging.INFO)
|
| 15 |
logger = logging.getLogger("AgriCopilot")
|
|
|
|
| 24 |
return {"status": "AgriCopilot AI Backend is working perfectly"}
|
| 25 |
|
| 26 |
# ==============================
|
| 27 |
+
# Auth
|
| 28 |
# ==============================
|
| 29 |
PROJECT_API_KEY = os.getenv("PROJECT_API_KEY", "agricopilot404")
|
| 30 |
|
|
|
|
| 66 |
# ==============================
|
| 67 |
# HuggingFace Pipelines
|
| 68 |
# ==============================
|
| 69 |
+
def load_pipeline(task, model_meta, model_fallback):
|
| 70 |
+
"""Try Meta model, fallback to public."""
|
| 71 |
+
try:
|
| 72 |
+
return pipeline(task, model=model_meta)
|
| 73 |
+
except Exception as e:
|
| 74 |
+
logger.warning(f"Meta model {model_meta} unavailable. Using fallback {model_fallback}.")
|
| 75 |
+
return pipeline(task, model=model_fallback)
|
| 76 |
+
|
| 77 |
+
chat_pipe = load_pipeline("conversational",
|
| 78 |
+
model_meta="meta-llama/Llama-3.1-8B-Instruct",
|
| 79 |
+
model_fallback="microsoft/DialoGPT-medium")
|
| 80 |
+
|
| 81 |
+
disaster_pipe = load_pipeline("conversational",
|
| 82 |
+
model_meta="meta-llama/Llama-3.1-8B-Instruct",
|
| 83 |
+
model_fallback="microsoft/DialoGPT-medium")
|
| 84 |
+
|
| 85 |
+
market_pipe = load_pipeline("conversational",
|
| 86 |
+
model_meta="meta-llama/Llama-3.1-8B-Instruct",
|
| 87 |
+
model_fallback="microsoft/DialoGPT-medium")
|
| 88 |
+
|
| 89 |
+
crop_pipe = load_pipeline("image-to-text",
|
| 90 |
+
model_meta="meta-llama/Llama-3.2-11B-Vision-Instruct",
|
| 91 |
+
model_fallback="Salesforce/blip-image-captioning-large")
|
| 92 |
|
| 93 |
# ==============================
|
| 94 |
# Helper Functions
|
|
|
|
| 116 |
return f"โ ๏ธ Unexpected vision model error: {str(e)}"
|
| 117 |
|
| 118 |
# ==============================
|
| 119 |
+
# Endpoints
|
| 120 |
# ==============================
|
|
|
|
|
|
|
| 121 |
@app.post("/crop-doctor")
|
| 122 |
+
async def crop_doctor(symptoms: str = Header(...), image: UploadFile = File(...), authorization: str | None = Header(None)):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
check_auth(authorization)
|
| 124 |
image_bytes = await image.read()
|
| 125 |
diagnosis = run_crop_doctor(image_bytes, symptoms)
|
| 126 |
return {"diagnosis": diagnosis}
|
| 127 |
|
|
|
|
| 128 |
@app.post("/multilingual-chat")
|
| 129 |
async def multilingual_chat(req: ChatRequest, authorization: str | None = Header(None)):
|
| 130 |
check_auth(authorization)
|
| 131 |
reply = run_conversational(chat_pipe, req.query)
|
| 132 |
return {"reply": reply}
|
| 133 |
|
|
|
|
| 134 |
@app.post("/disaster-summarizer")
|
| 135 |
async def disaster_summarizer(req: DisasterRequest, authorization: str | None = Header(None)):
|
| 136 |
check_auth(authorization)
|
| 137 |
summary = run_conversational(disaster_pipe, req.report)
|
| 138 |
return {"summary": summary}
|
| 139 |
|
|
|
|
| 140 |
@app.post("/marketplace")
|
| 141 |
async def marketplace(req: MarketRequest, authorization: str | None = Header(None)):
|
| 142 |
check_auth(authorization)
|
| 143 |
recommendation = run_conversational(market_pipe, req.product)
|
| 144 |
return {"recommendation": recommendation}
|
| 145 |
|
|
|
|
| 146 |
@app.post("/vector-search")
|
| 147 |
async def vector_search(req: VectorRequest, authorization: str | None = Header(None)):
|
| 148 |
check_auth(authorization)
|