ranchopanda0 commited on
Commit
a0d5d1b
·
verified ·
1 Parent(s): 7561df6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -12
app.py CHANGED
@@ -6,11 +6,11 @@ import torch
6
  import numpy as np
7
  import requests
8
  import logging
9
- from dotenv import load_dotenv # Load .env file
10
 
11
- # Load environment variables
12
- load_dotenv()
13
  HUGGINGFACE_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
 
 
14
 
15
  # Configure Logging
16
  logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
@@ -33,12 +33,16 @@ def get_treatment_suggestions(disease_name):
33
 
34
  try:
35
  response = requests.post(url, headers=headers, json=data)
36
- if response.status_code == 200:
37
- return response.json()[0]["generated_text"]
 
 
38
  else:
39
- return f"API Error: {response.status_code}"
 
40
  except Exception as e:
41
- return "Error retrieving treatment details."
 
42
 
43
  # Define Prediction Function
44
  def predict(image):
@@ -50,11 +54,11 @@ def predict(image):
50
  logits = outputs.logits
51
  predicted_class_idx = logits.argmax(-1).item()
52
  predicted_label = model.config.id2label[predicted_class_idx]
53
-
54
  # Get AI-generated treatment suggestions
55
  treatment = get_treatment_suggestions(predicted_label)
56
-
57
- return f"Predicted Disease: {predicted_label}\nTreatment: {treatment}"
58
  except Exception as e:
59
  logging.error(f"Prediction failed: {str(e)}")
60
  return f"❌ Prediction failed: {str(e)}"
@@ -64,8 +68,8 @@ iface = gr.Interface(
64
  fn=predict,
65
  inputs=gr.Image(type="numpy", label="Upload or capture plant image"),
66
  outputs=gr.Textbox(label="Result"),
67
- title="AI-Powered Plant Disease Detector",
68
- description="Upload a plant leaf image to detect diseases and get AI-powered treatment suggestions.",
69
  allow_flagging="never",
70
  )
71
 
 
6
  import numpy as np
7
  import requests
8
  import logging
 
9
 
10
+ # Load Hugging Face API Key from Secrets
 
11
  HUGGINGFACE_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
12
+ if not HUGGINGFACE_API_KEY:
13
+ raise ValueError("❌ Missing Hugging Face API Key. Please set it in Hugging Face Secrets.")
14
 
15
  # Configure Logging
16
  logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
 
33
 
34
  try:
35
  response = requests.post(url, headers=headers, json=data)
36
+ response_data = response.json()
37
+
38
+ if response.status_code == 200 and isinstance(response_data, list):
39
+ return response_data[0].get("generated_text", "No treatment information found.")
40
  else:
41
+ logging.error(f"API Error {response.status_code}: {response_data}")
42
+ return f"API Error {response.status_code}: {response_data.get('error', 'Unknown error')}"
43
  except Exception as e:
44
+ logging.error(f"Error fetching treatment suggestions: {str(e)}")
45
+ return "❌ Error retrieving treatment details."
46
 
47
  # Define Prediction Function
48
  def predict(image):
 
54
  logits = outputs.logits
55
  predicted_class_idx = logits.argmax(-1).item()
56
  predicted_label = model.config.id2label[predicted_class_idx]
57
+
58
  # Get AI-generated treatment suggestions
59
  treatment = get_treatment_suggestions(predicted_label)
60
+
61
+ return f"🌱 **Predicted Disease:** {predicted_label}\n💊 **Treatment:** {treatment}"
62
  except Exception as e:
63
  logging.error(f"Prediction failed: {str(e)}")
64
  return f"❌ Prediction failed: {str(e)}"
 
68
  fn=predict,
69
  inputs=gr.Image(type="numpy", label="Upload or capture plant image"),
70
  outputs=gr.Textbox(label="Result"),
71
+ title="🌿 AI-Powered Plant Disease Detector",
72
+ description="📷 Upload a plant leaf image to detect diseases and get AI-powered treatment suggestions.",
73
  allow_flagging="never",
74
  )
75