Files changed (1) hide show
  1. app.py +7 -15
app.py CHANGED
@@ -105,7 +105,7 @@ def get_commercial_product_info(recommendation, disease_name):
105
  return indiamart_results + krishi_results
106
 
107
 
108
- def analyze_plant_image(image_path, plant_name):
109
  try:
110
  # Load the image
111
  image_parts = [
@@ -115,7 +115,7 @@ def analyze_plant_image(image_path, plant_name):
115
  }
116
  ]
117
 
118
- # Create prompt for Gemini API
119
  prompt = f"""
120
  Analyze this image of a {plant_name} plant and priortize the determine if it's healthy or has a disease or pest infestation.
121
 
@@ -147,6 +147,7 @@ def analyze_plant_image(image_path, plant_name):
147
 
148
  Only return the JSON data and nothing else. Ensure the JSON is valid and properly formatted.
149
  If the plant appears completely healthy, set is_healthy to true and include an empty results array.
 
150
  """
151
 
152
  # Send request to Gemini API
@@ -208,6 +209,7 @@ def analyze():
208
 
209
  file = request.files['plant_image']
210
  plant_name = request.form.get('plant_name', 'unknown')
 
211
 
212
  if file.filename == '':
213
  flash('No selected file')
@@ -222,12 +224,11 @@ def analyze():
222
  file.save(file_path)
223
 
224
  try:
225
- # Analyze the image
226
- analysis_result = analyze_plant_image(file_path, plant_name)
227
 
228
  if 'error' in analysis_result:
229
  flash(f"Error analyzing image: {analysis_result['error']}")
230
- # Remove the file if analysis failed
231
  if os.path.exists(file_path):
232
  os.remove(file_path)
233
  return redirect(url_for('index'))
@@ -240,16 +241,11 @@ def analyze():
240
  for result in analysis_result['results']:
241
  disease_name = result.get('name', '')
242
  if disease_name:
243
- # Get web information about the disease
244
  web_info[disease_name] = get_web_pesticide_info(disease_name, plant_name)
245
-
246
- # Get commercial product information for treatment
247
  treatment = result.get('treatment', '')
248
  if treatment:
249
  product_info[disease_name] = get_commercial_product_info(treatment, disease_name)
250
 
251
- # Now we need to handle the file - since we've already analyzed it,
252
- # we can delete it immediately after rendering the template
253
  response = render_template(
254
  'results.html',
255
  results=analysis_result,
@@ -259,9 +255,7 @@ def analyze():
259
  product_info=product_info
260
  )
261
 
262
- # Schedule the file for deletion (will be deleted after rendering)
263
- # We use a small delay to ensure the file is available for initial page load
264
- def delete_file_after_delay(path, delay=30): # Increased delay for Hugging Face
265
  time.sleep(delay)
266
  if os.path.exists(path):
267
  try:
@@ -270,7 +264,6 @@ def analyze():
270
  except Exception as e:
271
  print(f"Error deleting {path}: {e}")
272
 
273
- # Start a thread to delete the file after a brief delay
274
  threading.Thread(
275
  target=delete_file_after_delay,
276
  args=(file_path,),
@@ -281,7 +274,6 @@ def analyze():
281
 
282
  except Exception as e:
283
  flash(f"An error occurred: {str(e)}")
284
- # Remove the file if there's an error
285
  if os.path.exists(file_path):
286
  os.remove(file_path)
287
  return redirect(url_for('index'))
 
105
  return indiamart_results + krishi_results
106
 
107
 
108
+ def analyze_plant_image(image_path, plant_name, language):
109
  try:
110
  # Load the image
111
  image_parts = [
 
115
  }
116
  ]
117
 
118
+ # Create prompt for Gemini API with language instruction
119
  prompt = f"""
120
  Analyze this image of a {plant_name} plant and priortize the determine if it's healthy or has a disease or pest infestation.
121
 
 
147
 
148
  Only return the JSON data and nothing else. Ensure the JSON is valid and properly formatted.
149
  If the plant appears completely healthy, set is_healthy to true and include an empty results array.
150
+ Additionally, provide the response in {language} language.
151
  """
152
 
153
  # Send request to Gemini API
 
209
 
210
  file = request.files['plant_image']
211
  plant_name = request.form.get('plant_name', 'unknown')
212
+ language = request.form.get('language', 'English') # New field for response language
213
 
214
  if file.filename == '':
215
  flash('No selected file')
 
224
  file.save(file_path)
225
 
226
  try:
227
+ # Analyze the image with language parameter
228
+ analysis_result = analyze_plant_image(file_path, plant_name, language)
229
 
230
  if 'error' in analysis_result:
231
  flash(f"Error analyzing image: {analysis_result['error']}")
 
232
  if os.path.exists(file_path):
233
  os.remove(file_path)
234
  return redirect(url_for('index'))
 
241
  for result in analysis_result['results']:
242
  disease_name = result.get('name', '')
243
  if disease_name:
 
244
  web_info[disease_name] = get_web_pesticide_info(disease_name, plant_name)
 
 
245
  treatment = result.get('treatment', '')
246
  if treatment:
247
  product_info[disease_name] = get_commercial_product_info(treatment, disease_name)
248
 
 
 
249
  response = render_template(
250
  'results.html',
251
  results=analysis_result,
 
255
  product_info=product_info
256
  )
257
 
258
+ def delete_file_after_delay(path, delay=30):
 
 
259
  time.sleep(delay)
260
  if os.path.exists(path):
261
  try:
 
264
  except Exception as e:
265
  print(f"Error deleting {path}: {e}")
266
 
 
267
  threading.Thread(
268
  target=delete_file_after_delay,
269
  args=(file_path,),
 
274
 
275
  except Exception as e:
276
  flash(f"An error occurred: {str(e)}")
 
277
  if os.path.exists(file_path):
278
  os.remove(file_path)
279
  return redirect(url_for('index'))