Spaces:
Sleeping
Sleeping
Commit ยท
dd43f94
1
Parent(s): 52eb310
Updated
Browse files
app.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
import os
|
| 2 |
import logging
|
|
|
|
| 3 |
from fastapi import FastAPI, Request, Header, HTTPException, UploadFile, File
|
| 4 |
from fastapi.responses import JSONResponse
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from transformers import pipeline
|
| 7 |
from PIL import Image
|
| 8 |
-
import io
|
| 9 |
from vector import query_vector
|
| 10 |
|
| 11 |
# ==============================
|
|
@@ -66,13 +66,15 @@ class VectorRequest(BaseModel):
|
|
| 66 |
# ==============================
|
| 67 |
# HuggingFace Pipelines
|
| 68 |
# ==============================
|
| 69 |
-
|
| 70 |
-
chat_pipe = pipeline("conversational", model="
|
| 71 |
-
disaster_pipe = pipeline("conversational", model="
|
| 72 |
-
market_pipe = pipeline("conversational", model="
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
| 76 |
|
| 77 |
# ==============================
|
| 78 |
# Helper Functions
|
|
@@ -80,7 +82,6 @@ crop_pipe = pipeline("image-to-text", model="meta-llama/Llama-3.2-11B-Vision-Ins
|
|
| 80 |
def run_conversational(pipe, prompt: str):
|
| 81 |
try:
|
| 82 |
output = pipe(prompt)
|
| 83 |
-
# output is a list of dicts
|
| 84 |
if isinstance(output, list) and len(output) > 0:
|
| 85 |
return output[0].get("generated_text", str(output))
|
| 86 |
return str(output)
|
|
@@ -91,43 +92,53 @@ def run_conversational(pipe, prompt: str):
|
|
| 91 |
def run_crop_doctor(image_bytes: bytes, symptoms: str):
|
| 92 |
try:
|
| 93 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 94 |
-
prompt = f"Farmer reports: {symptoms}. Diagnose the crop disease and suggest treatment in simple
|
| 95 |
output = crop_pipe(image, prompt=prompt)
|
| 96 |
if isinstance(output, list) and len(output) > 0:
|
| 97 |
return output[0].get("generated_text", str(output))
|
| 98 |
return str(output)
|
| 99 |
except Exception as e:
|
| 100 |
logger.error(f"Crop Doctor pipeline error: {e}")
|
| 101 |
-
return f"โ ๏ธ Unexpected model error: {str(e)}"
|
| 102 |
|
| 103 |
# ==============================
|
| 104 |
# ENDPOINTS
|
| 105 |
# ==============================
|
|
|
|
|
|
|
| 106 |
@app.post("/crop-doctor")
|
| 107 |
-
async def crop_doctor(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
check_auth(authorization)
|
| 109 |
image_bytes = await image.read()
|
| 110 |
diagnosis = run_crop_doctor(image_bytes, symptoms)
|
| 111 |
return {"diagnosis": diagnosis}
|
| 112 |
|
|
|
|
| 113 |
@app.post("/multilingual-chat")
|
| 114 |
async def multilingual_chat(req: ChatRequest, authorization: str | None = Header(None)):
|
| 115 |
check_auth(authorization)
|
| 116 |
reply = run_conversational(chat_pipe, req.query)
|
| 117 |
return {"reply": reply}
|
| 118 |
|
|
|
|
| 119 |
@app.post("/disaster-summarizer")
|
| 120 |
async def disaster_summarizer(req: DisasterRequest, authorization: str | None = Header(None)):
|
| 121 |
check_auth(authorization)
|
| 122 |
summary = run_conversational(disaster_pipe, req.report)
|
| 123 |
return {"summary": summary}
|
| 124 |
|
|
|
|
| 125 |
@app.post("/marketplace")
|
| 126 |
async def marketplace(req: MarketRequest, authorization: str | None = Header(None)):
|
| 127 |
check_auth(authorization)
|
| 128 |
recommendation = run_conversational(market_pipe, req.product)
|
| 129 |
return {"recommendation": recommendation}
|
| 130 |
|
|
|
|
| 131 |
@app.post("/vector-search")
|
| 132 |
async def vector_search(req: VectorRequest, authorization: str | None = Header(None)):
|
| 133 |
check_auth(authorization)
|
|
@@ -135,4 +146,5 @@ async def vector_search(req: VectorRequest, authorization: str | None = Header(N
|
|
| 135 |
results = query_vector(req.query)
|
| 136 |
return {"results": results}
|
| 137 |
except Exception as e:
|
|
|
|
| 138 |
return {"error": f"Vector search error: {str(e)}"}
|
|
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
+
import io
|
| 4 |
from fastapi import FastAPI, Request, Header, HTTPException, UploadFile, File
|
| 5 |
from fastapi.responses import JSONResponse
|
| 6 |
from pydantic import BaseModel
|
| 7 |
from transformers import pipeline
|
| 8 |
from PIL import Image
|
|
|
|
| 9 |
from vector import query_vector
|
| 10 |
|
| 11 |
# ==============================
|
|
|
|
| 66 |
# ==============================
|
| 67 |
# HuggingFace Pipelines
|
| 68 |
# ==============================
|
| 69 |
+
try:
|
| 70 |
+
chat_pipe = pipeline("conversational", model="microsoft/DialoGPT-medium")
|
| 71 |
+
disaster_pipe = pipeline("conversational", model="microsoft/DialoGPT-medium")
|
| 72 |
+
market_pipe = pipeline("conversational", model="microsoft/DialoGPT-medium")
|
| 73 |
+
crop_pipe = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large")
|
| 74 |
+
logger.info("All pipelines loaded successfully.")
|
| 75 |
+
except Exception as e:
|
| 76 |
+
logger.error(f"Error loading pipelines: {e}")
|
| 77 |
+
raise RuntimeError("Pipelines failed to load. Check model availability.")
|
| 78 |
|
| 79 |
# ==============================
|
| 80 |
# Helper Functions
|
|
|
|
| 82 |
def run_conversational(pipe, prompt: str):
|
| 83 |
try:
|
| 84 |
output = pipe(prompt)
|
|
|
|
| 85 |
if isinstance(output, list) and len(output) > 0:
|
| 86 |
return output[0].get("generated_text", str(output))
|
| 87 |
return str(output)
|
|
|
|
| 92 |
def run_crop_doctor(image_bytes: bytes, symptoms: str):
|
| 93 |
try:
|
| 94 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 95 |
+
prompt = f"Farmer reports: {symptoms}. Diagnose the crop disease and suggest treatment in simple terms."
|
| 96 |
output = crop_pipe(image, prompt=prompt)
|
| 97 |
if isinstance(output, list) and len(output) > 0:
|
| 98 |
return output[0].get("generated_text", str(output))
|
| 99 |
return str(output)
|
| 100 |
except Exception as e:
|
| 101 |
logger.error(f"Crop Doctor pipeline error: {e}")
|
| 102 |
+
return f"โ ๏ธ Unexpected vision model error: {str(e)}"
|
| 103 |
|
| 104 |
# ==============================
|
| 105 |
# ENDPOINTS
|
| 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)
|
|
|
|
| 146 |
results = query_vector(req.query)
|
| 147 |
return {"results": results}
|
| 148 |
except Exception as e:
|
| 149 |
+
logger.error(f"Vector search error: {e}")
|
| 150 |
return {"error": f"Vector search error: {str(e)}"}
|