Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -66,12 +66,9 @@ TRANSLATIONS = {
|
|
| 66 |
|
| 67 |
def translate_text(text):
|
| 68 |
if st.session_state.get("language", "English") == "Marathi":
|
| 69 |
-
if text == "Krushi Mitra":
|
| 70 |
-
return "कृषी मित्र"
|
| 71 |
return TRANSLATIONS.get(text, text)
|
| 72 |
return text
|
| 73 |
|
| 74 |
-
|
| 75 |
# Custom CSS for styling
|
| 76 |
# Update custom CSS
|
| 77 |
custom_css = """
|
|
@@ -194,7 +191,6 @@ def get_plant_info(disease, plant_type="Unknown"):
|
|
| 194 |
- Treatment & Prevention
|
| 195 |
"""
|
| 196 |
try:
|
| 197 |
-
# API_URL = "http://192.168.56.1:1234/v1/chat/completions"
|
| 198 |
API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-3.2-1B-Instruct/v1/chat/completions"
|
| 199 |
headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_TOKEN')}"}
|
| 200 |
try:
|
|
@@ -204,8 +200,7 @@ def get_plant_info(disease, plant_type="Unknown"):
|
|
| 204 |
{"role": "user", "content": prompt},
|
| 205 |
]
|
| 206 |
}
|
| 207 |
-
|
| 208 |
-
response = requests.post(API_URL, json=data)
|
| 209 |
response.raise_for_status()
|
| 210 |
print("Response:", data)
|
| 211 |
except requests.exceptions.HTTPError as http_err:
|
|
@@ -225,8 +220,8 @@ def get_web_pesticide_info(disease, plant_type="Unknown"):
|
|
| 225 |
query = f"site:agrowon.esakal.com {disease} in {plant_type}"
|
| 226 |
url = "https://www.googleapis.com/customsearch/v1"
|
| 227 |
params = {
|
| 228 |
-
"key": "
|
| 229 |
-
"cx": "
|
| 230 |
"q": query,
|
| 231 |
"num": 3
|
| 232 |
}
|
|
@@ -247,8 +242,8 @@ def get_web_pesticide_info(disease, plant_type="Unknown"):
|
|
| 247 |
def get_more_web_info(query):
|
| 248 |
url = "https://www.googleapis.com/customsearch/v1"
|
| 249 |
params = {
|
| 250 |
-
"key": "
|
| 251 |
-
"cx": "
|
| 252 |
"q": query,
|
| 253 |
"num": 3
|
| 254 |
}
|
|
@@ -293,25 +288,16 @@ def translate_text(text):
|
|
| 293 |
|
| 294 |
|
| 295 |
def main():
|
| 296 |
-
# Initialize language in session state if not present
|
| 297 |
-
if "language" not in st.session_state:
|
| 298 |
-
st.session_state.language = "English"
|
| 299 |
-
|
| 300 |
st.sidebar.title(translate_text("Settings"))
|
| 301 |
st.sidebar.info(translate_text("Choose language and plant type, then upload an image to classify the disease."))
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
new_language = st.sidebar.radio(translate_text("Language"), options=["English", "Marathi"], index=["English", "Marathi"].index(st.session_state.language))
|
| 305 |
-
if new_language != st.session_state.language:
|
| 306 |
-
st.session_state.language = new_language
|
| 307 |
-
|
| 308 |
plant_type = st.sidebar.selectbox(translate_text("Select Plant Type"), options=['sugarcane', 'maize', 'cotton', 'rice', 'wheat'])
|
| 309 |
uploaded_file = st.sidebar.file_uploader(
|
| 310 |
translate_text("Upload a plant image..."),
|
| 311 |
type=["jpg", "jpeg", "png"],
|
| 312 |
help=translate_text("Select an image of your plant to detect diseases")
|
| 313 |
)
|
| 314 |
-
|
| 315 |
st.title(translate_text("Krushi Mitra"))
|
| 316 |
st.write(translate_text("Plant Disease Classification and Pesticide Recommendation.\n\nUpload an image, select plant type, and click on Classify."))
|
| 317 |
|
|
@@ -338,8 +324,8 @@ def main():
|
|
| 338 |
web_recommendation = get_web_pesticide_info(predicted_class, plant_type)
|
| 339 |
if web_recommendation:
|
| 340 |
st.markdown(translate_text("#### Additional Pesticide Recommendations"))
|
| 341 |
-
st.markdown(f"
|
| 342 |
-
st.markdown(f"
|
| 343 |
if web_recommendation['link']:
|
| 344 |
st.markdown(f"[{translate_text('Read More')}]({web_recommendation['link']})")
|
| 345 |
else:
|
|
@@ -349,8 +335,8 @@ def main():
|
|
| 349 |
commercial_products = get_commercial_product_info(pesticide)
|
| 350 |
if commercial_products:
|
| 351 |
for item in commercial_products:
|
| 352 |
-
st.markdown(f"
|
| 353 |
-
st.markdown(f"
|
| 354 |
if item['link']:
|
| 355 |
st.markdown(f"[{translate_text('Read More')}]({item['link']})")
|
| 356 |
st.markdown("---")
|
|
@@ -361,8 +347,8 @@ def main():
|
|
| 361 |
more_info = get_more_web_info(f"{predicted_class} in {plant_type}")
|
| 362 |
if more_info:
|
| 363 |
for item in more_info:
|
| 364 |
-
st.markdown(f"
|
| 365 |
-
st.markdown(f"
|
| 366 |
if item['link']:
|
| 367 |
st.markdown(f"[{translate_text('Read More')}]({item['link']})")
|
| 368 |
st.markdown("---")
|
|
@@ -374,4 +360,4 @@ def main():
|
|
| 374 |
st.info(translate_text("Please upload an image from the sidebar to get started."))
|
| 375 |
|
| 376 |
if __name__ == "__main__":
|
| 377 |
-
main()
|
|
|
|
| 66 |
|
| 67 |
def translate_text(text):
|
| 68 |
if st.session_state.get("language", "English") == "Marathi":
|
|
|
|
|
|
|
| 69 |
return TRANSLATIONS.get(text, text)
|
| 70 |
return text
|
| 71 |
|
|
|
|
| 72 |
# Custom CSS for styling
|
| 73 |
# Update custom CSS
|
| 74 |
custom_css = """
|
|
|
|
| 191 |
- Treatment & Prevention
|
| 192 |
"""
|
| 193 |
try:
|
|
|
|
| 194 |
API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-3.2-1B-Instruct/v1/chat/completions"
|
| 195 |
headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_TOKEN')}"}
|
| 196 |
try:
|
|
|
|
| 200 |
{"role": "user", "content": prompt},
|
| 201 |
]
|
| 202 |
}
|
| 203 |
+
response = requests.post(API_URL, headers=headers, json=data)
|
|
|
|
| 204 |
response.raise_for_status()
|
| 205 |
print("Response:", data)
|
| 206 |
except requests.exceptions.HTTPError as http_err:
|
|
|
|
| 220 |
query = f"site:agrowon.esakal.com {disease} in {plant_type}"
|
| 221 |
url = "https://www.googleapis.com/customsearch/v1"
|
| 222 |
params = {
|
| 223 |
+
"key": os.getenv("GOOGLE_API_KEY"),
|
| 224 |
+
"cx": os.getenv("GOOGLE_CX"),
|
| 225 |
"q": query,
|
| 226 |
"num": 3
|
| 227 |
}
|
|
|
|
| 242 |
def get_more_web_info(query):
|
| 243 |
url = "https://www.googleapis.com/customsearch/v1"
|
| 244 |
params = {
|
| 245 |
+
"key": os.getenv("GOOGLE_API_KEY"),
|
| 246 |
+
"cx": os.getenv("GOOGLE_CX"),
|
| 247 |
"q": query,
|
| 248 |
"num": 3
|
| 249 |
}
|
|
|
|
| 288 |
|
| 289 |
|
| 290 |
def main():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
st.sidebar.title(translate_text("Settings"))
|
| 292 |
st.sidebar.info(translate_text("Choose language and plant type, then upload an image to classify the disease."))
|
| 293 |
+
language_option = st.sidebar.radio(translate_text("Language"), options=["English", "Marathi"], index=0)
|
| 294 |
+
st.session_state.language = language_option
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
plant_type = st.sidebar.selectbox(translate_text("Select Plant Type"), options=['sugarcane', 'maize', 'cotton', 'rice', 'wheat'])
|
| 296 |
uploaded_file = st.sidebar.file_uploader(
|
| 297 |
translate_text("Upload a plant image..."),
|
| 298 |
type=["jpg", "jpeg", "png"],
|
| 299 |
help=translate_text("Select an image of your plant to detect diseases")
|
| 300 |
)
|
|
|
|
| 301 |
st.title(translate_text("Krushi Mitra"))
|
| 302 |
st.write(translate_text("Plant Disease Classification and Pesticide Recommendation.\n\nUpload an image, select plant type, and click on Classify."))
|
| 303 |
|
|
|
|
| 324 |
web_recommendation = get_web_pesticide_info(predicted_class, plant_type)
|
| 325 |
if web_recommendation:
|
| 326 |
st.markdown(translate_text("#### Additional Pesticide Recommendations"))
|
| 327 |
+
st.markdown(f"{translate_text('Title')}:** {translate_text(web_recommendation['title'])}")
|
| 328 |
+
st.markdown(f"{translate_text('Summary')}:** {translate_text(web_recommendation['summary'])}")
|
| 329 |
if web_recommendation['link']:
|
| 330 |
st.markdown(f"[{translate_text('Read More')}]({web_recommendation['link']})")
|
| 331 |
else:
|
|
|
|
| 335 |
commercial_products = get_commercial_product_info(pesticide)
|
| 336 |
if commercial_products:
|
| 337 |
for item in commercial_products:
|
| 338 |
+
st.markdown(f"{translate_text('Title')}:** {translate_text(item['title'])}")
|
| 339 |
+
st.markdown(f"{translate_text('Snippet')}:** {translate_text(item['snippet'])}")
|
| 340 |
if item['link']:
|
| 341 |
st.markdown(f"[{translate_text('Read More')}]({item['link']})")
|
| 342 |
st.markdown("---")
|
|
|
|
| 347 |
more_info = get_more_web_info(f"{predicted_class} in {plant_type}")
|
| 348 |
if more_info:
|
| 349 |
for item in more_info:
|
| 350 |
+
st.markdown(f"{translate_text('Title')}:** {translate_text(item['title'])}")
|
| 351 |
+
st.markdown(f"{translate_text('Snippet')}:** {translate_text(item['snippet'])}")
|
| 352 |
if item['link']:
|
| 353 |
st.markdown(f"[{translate_text('Read More')}]({item['link']})")
|
| 354 |
st.markdown("---")
|
|
|
|
| 360 |
st.info(translate_text("Please upload an image from the sidebar to get started."))
|
| 361 |
|
| 362 |
if __name__ == "__main__":
|
| 363 |
+
main()
|