alaselababatunde commited on
Commit
ff2254b
·
1 Parent(s): c560358
Files changed (1) hide show
  1. app.py +34 -7
app.py CHANGED
@@ -64,17 +64,39 @@ class VectorRequest(BaseModel):
64
  query: str
65
 
66
  # ==============================
67
- # HuggingFace Pipelines
68
  # ==============================
 
 
 
 
 
 
 
69
  # Conversational endpoints use text-generation
70
- chat_pipe = pipeline("text-generation", model="meta-llama/Llama-3.1-8B-Instruct")
71
- disaster_pipe = pipeline("text-generation", model="meta-llama/Llama-3.1-8B-Instruct")
72
- market_pipe = pipeline("text-generation", model="meta-llama/Llama-3.1-8B-Instruct")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
  # Crop Doctor uses Meta Vision-Instruct model
75
  crop_pipe = pipeline(
76
  "image-text-to-text",
77
  model="meta-llama/Llama-3.2-11B-Vision-Instruct",
 
78
  )
79
 
80
  # ==============================
@@ -95,12 +117,12 @@ def run_crop_doctor(image_bytes: bytes, symptoms: str):
95
  """
96
  Diagnose crop issues using Meta's multimodal LLaMA Vision model.
97
  The model sees the crop image and reads the farmer's description,
98
- then explains the likely disease, simple treatment steps and future prevention steps.
99
  """
100
  try:
101
  image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
102
  prompt = (
103
- f"The AI farmer reports: {symptoms}. "
104
  "Analyze the plant image and diagnose the likely crop disease. "
105
  "Then provide a simple explanation and possible treatment steps."
106
  )
@@ -116,7 +138,11 @@ def run_crop_doctor(image_bytes: bytes, symptoms: str):
116
  # ENDPOINTS
117
  # ==============================
118
  @app.post("/crop-doctor")
119
- async def crop_doctor(symptoms: str = Header(...), image: UploadFile = File(...), authorization: str | None = Header(None)):
 
 
 
 
120
  check_auth(authorization)
121
  image_bytes = await image.read()
122
  diagnosis = run_crop_doctor(image_bytes, symptoms)
@@ -147,4 +173,5 @@ async def vector_search(req: VectorRequest, authorization: str | None = Header(N
147
  results = query_vector(req.query)
148
  return {"results": results}
149
  except Exception as e:
 
150
  return {"error": f"Vector search error: {str(e)}"}
 
64
  query: str
65
 
66
  # ==============================
67
+ # HuggingFace Pipelines (with token)
68
  # ==============================
69
+ HF_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
70
+
71
+ if not HF_TOKEN:
72
+ logger.warning("⚠️ No Hugging Face token found in environment. Gated models may fail to load.")
73
+ else:
74
+ logger.info("✅ Hugging Face token detected and ready.")
75
+
76
  # Conversational endpoints use text-generation
77
+ chat_pipe = pipeline(
78
+ "text-generation",
79
+ model="meta-llama/Llama-3.1-8B-Instruct",
80
+ token=HF_TOKEN
81
+ )
82
+
83
+ disaster_pipe = pipeline(
84
+ "text-generation",
85
+ model="meta-llama/Llama-3.1-8B-Instruct",
86
+ token=HF_TOKEN
87
+ )
88
+
89
+ market_pipe = pipeline(
90
+ "text-generation",
91
+ model="meta-llama/Llama-3.1-8B-Instruct",
92
+ token=HF_TOKEN
93
+ )
94
 
95
  # Crop Doctor uses Meta Vision-Instruct model
96
  crop_pipe = pipeline(
97
  "image-text-to-text",
98
  model="meta-llama/Llama-3.2-11B-Vision-Instruct",
99
+ token=HF_TOKEN
100
  )
101
 
102
  # ==============================
 
117
  """
118
  Diagnose crop issues using Meta's multimodal LLaMA Vision model.
119
  The model sees the crop image and reads the farmer's description,
120
+ then explains the likely disease, simple treatment steps, and prevention tips.
121
  """
122
  try:
123
  image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
124
  prompt = (
125
+ f"The farmer reports: {symptoms}. "
126
  "Analyze the plant image and diagnose the likely crop disease. "
127
  "Then provide a simple explanation and possible treatment steps."
128
  )
 
138
  # ENDPOINTS
139
  # ==============================
140
  @app.post("/crop-doctor")
141
+ async def crop_doctor(
142
+ symptoms: str = Header(...),
143
+ image: UploadFile = File(...),
144
+ authorization: str | None = Header(None)
145
+ ):
146
  check_auth(authorization)
147
  image_bytes = await image.read()
148
  diagnosis = run_crop_doctor(image_bytes, symptoms)
 
173
  results = query_vector(req.query)
174
  return {"results": results}
175
  except Exception as e:
176
+ logger.error(f"Vector search error: {e}")
177
  return {"error": f"Vector search error: {str(e)}"}