ranchopanda0 commited on
Commit
2fb3db3
·
verified ·
1 Parent(s): 71e1d4e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -7
app.py CHANGED
@@ -3,9 +3,9 @@ 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
 
10
  # Configure Logging
11
  logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
@@ -21,26 +21,27 @@ except Exception as e:
21
  raise RuntimeError("Failed to load the model. Please check the logs for details.")
22
 
23
  # Gemini API Key (Replace with your actual key)
24
- GEMINI_API_KEY = "AIzaSyCiRL0ES-zsJGJYsY03xmpwqcggDGcL2Fk" # <-- Replace with your actual API key
25
 
26
  # Function to Get AI-Powered Treatment Suggestions
27
  def get_treatment_suggestions(disease_name):
28
  url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateText?key={GEMINI_API_KEY}"
29
  headers = {"Content-Type": "application/json"}
30
  data = {
31
- "prompt": f"Provide detailed organic and chemical treatment options, including dosage and preventive care, for {disease_name} in crops.",
32
  "temperature": 0.7,
33
- "max_tokens": 250
34
  }
35
-
36
  try:
37
  response = requests.post(url, headers=headers, json=data)
38
  if response.status_code == 200:
39
  result = response.json()
40
- treatment = result.get("candidates", [{}])[0].get("output", "No treatment suggestions found.")
41
  return treatment
42
  else:
43
- return f"API Error: {response.status_code} - {response.text}"
 
44
  except Exception as e:
45
  logging.error(f"Error fetching treatment suggestions: {str(e)}")
46
  return "Error retrieving treatment details."
 
3
  from PIL import Image
4
  import torch
5
  import numpy as np
 
6
  import logging
7
  import requests
8
+ import os
9
 
10
  # Configure Logging
11
  logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
 
21
  raise RuntimeError("Failed to load the model. Please check the logs for details.")
22
 
23
  # Gemini API Key (Replace with your actual key)
24
+ GEMINI_API_KEY = os.getenv("AIzaSyCiRL0ES-zsJGJYsY03xmpwqcggDGcL2Fk", "AIzaSyCiRL0ES-zsJGJYsY03xmpwqcggDGcL2Fk")
25
 
26
  # Function to Get AI-Powered Treatment Suggestions
27
  def get_treatment_suggestions(disease_name):
28
  url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateText?key={GEMINI_API_KEY}"
29
  headers = {"Content-Type": "application/json"}
30
  data = {
31
+ "prompt": { "text": f"Provide detailed organic and chemical treatment options, including dosage and preventive care, for {disease_name} in crops." },
32
  "temperature": 0.7,
33
+ "candidate_count": 1
34
  }
35
+
36
  try:
37
  response = requests.post(url, headers=headers, json=data)
38
  if response.status_code == 200:
39
  result = response.json()
40
+ treatment = result.get("candidates", [{}])[0].get("content", "No treatment suggestions found.")
41
  return treatment
42
  else:
43
+ logging.error(f"API Error: {response.status_code} - {response.text}")
44
+ return f"API Error: {response.status_code}"
45
  except Exception as e:
46
  logging.error(f"Error fetching treatment suggestions: {str(e)}")
47
  return "Error retrieving treatment details."