Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,10 +3,8 @@ from transformers import AutoImageProcessor, AutoModelForImageClassification
|
|
3 |
from PIL import Image
|
4 |
import torch
|
5 |
import numpy as np
|
6 |
-
import json
|
7 |
import logging
|
8 |
import requests
|
9 |
-
import os
|
10 |
|
11 |
# Configure Logging
|
12 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
@@ -21,22 +19,33 @@ except Exception as e:
|
|
21 |
logging.error(f"❌ Failed to load model: {str(e)}")
|
22 |
raise RuntimeError("Failed to load the model. Please check the logs for details.")
|
23 |
|
24 |
-
# Gemini API Key
|
25 |
-
GEMINI_API_KEY =
|
26 |
|
27 |
# Function to Get AI-Powered Treatment Suggestions
|
28 |
def get_treatment_suggestions(disease_name):
|
29 |
prompt = f"Provide detailed organic and chemical treatment options, including dosage and preventive care, for {disease_name} in crops."
|
30 |
-
url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateText"
|
|
|
31 |
headers = {"Content-Type": "application/json"}
|
32 |
-
data = {
|
33 |
-
|
|
|
|
|
|
|
|
|
34 |
try:
|
35 |
-
response = requests.post(url, headers=headers, json=data
|
36 |
if response.status_code == 200:
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
38 |
else:
|
39 |
-
|
|
|
40 |
except Exception as e:
|
41 |
logging.error(f"Error fetching treatment suggestions: {str(e)}")
|
42 |
return "Error retrieving treatment details."
|
@@ -65,7 +74,7 @@ iface = gr.Interface(
|
|
65 |
fn=predict,
|
66 |
inputs=gr.Image(type="numpy", label="Upload or capture plant image"),
|
67 |
outputs=gr.Textbox(label="Result"),
|
68 |
-
title="AI-Powered Plant Disease Detector",
|
69 |
description="Upload a plant leaf image to detect diseases and get AI-powered treatment suggestions.",
|
70 |
allow_flagging="never",
|
71 |
)
|
|
|
3 |
from PIL import Image
|
4 |
import torch
|
5 |
import numpy as np
|
|
|
6 |
import logging
|
7 |
import requests
|
|
|
8 |
|
9 |
# Configure Logging
|
10 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
|
|
19 |
logging.error(f"❌ Failed to load model: {str(e)}")
|
20 |
raise RuntimeError("Failed to load the model. Please check the logs for details.")
|
21 |
|
22 |
+
# Gemini API Key (Replace 'xxxxxxx' with your actual API key)
|
23 |
+
GEMINI_API_KEY = "xxxxxxx"
|
24 |
|
25 |
# Function to Get AI-Powered Treatment Suggestions
|
26 |
def get_treatment_suggestions(disease_name):
|
27 |
prompt = f"Provide detailed organic and chemical treatment options, including dosage and preventive care, for {disease_name} in crops."
|
28 |
+
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateText?key={GEMINI_API_KEY}"
|
29 |
+
|
30 |
headers = {"Content-Type": "application/json"}
|
31 |
+
data = {
|
32 |
+
"contents": [{"parts": [{"text": prompt}]}], # Correct API request format
|
33 |
+
"temperature": 0.7,
|
34 |
+
"maxOutputTokens": 250 # Correct parameter name
|
35 |
+
}
|
36 |
+
|
37 |
try:
|
38 |
+
response = requests.post(url, headers=headers, json=data)
|
39 |
if response.status_code == 200:
|
40 |
+
json_response = response.json()
|
41 |
+
candidates = json_response.get("candidates", [])
|
42 |
+
if candidates:
|
43 |
+
return candidates[0].get("output", "No treatment suggestions found.")
|
44 |
+
else:
|
45 |
+
return "No treatment suggestions found."
|
46 |
else:
|
47 |
+
logging.error(f"API Error: {response.status_code} - {response.text}")
|
48 |
+
return f"API Error: {response.status_code}"
|
49 |
except Exception as e:
|
50 |
logging.error(f"Error fetching treatment suggestions: {str(e)}")
|
51 |
return "Error retrieving treatment details."
|
|
|
74 |
fn=predict,
|
75 |
inputs=gr.Image(type="numpy", label="Upload or capture plant image"),
|
76 |
outputs=gr.Textbox(label="Result"),
|
77 |
+
title="🌿 AI-Powered Plant Disease Detector",
|
78 |
description="Upload a plant leaf image to detect diseases and get AI-powered treatment suggestions.",
|
79 |
allow_flagging="never",
|
80 |
)
|