alaselababatunde commited on
Commit
fb015b4
ยท
1 Parent(s): dd43f94
Files changed (1) hide show
  1. app.py +27 -23
app.py CHANGED
@@ -9,7 +9,7 @@ from PIL import Image
9
  from vector import query_vector
10
 
11
  # ==============================
12
- # Setup Logging
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
- # AUTH CONFIG
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
- 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
@@ -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
- # 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)
 
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)