Spectral99 commited on
Commit
4720045
·
verified ·
1 Parent(s): f3c1663

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -7
app.py CHANGED
@@ -75,6 +75,8 @@ def translate_text(text, target_lang):
75
 
76
  # ---------------- LLM VIA HF API ----------------
77
  def generate_prevention_llm(plant, disease):
 
 
78
  if not HF_API_TOKEN:
79
  return "⚠️ Hugging Face API token not found."
80
 
@@ -125,22 +127,37 @@ Irrigation, drainage, sanitation, hygiene, storage.
125
  "parameters": {
126
  "max_new_tokens": 300,
127
  "temperature": 0.7,
128
- "top_p": 0.9
 
129
  }
130
  }
131
 
 
 
 
 
 
132
  try:
133
- response = requests.post(HF_API_URL, headers=HEADERS, json=payload, timeout=60)
134
- response.raise_for_status()
 
 
 
 
 
135
  output = response.json()
136
 
137
- if isinstance(output, list):
138
- return output[0]["generated_text"].replace(prompt, "").strip()
 
 
 
 
139
 
140
- return "No response from LLM."
141
 
142
  except Exception as e:
143
- return f"LLM Error: {e}"
144
 
145
  # ---------------- PREDICTION ----------------
146
  def predict(image_input, plant, language):
 
75
 
76
  # ---------------- LLM VIA HF API ----------------
77
  def generate_prevention_llm(plant, disease):
78
+ HF_API_TOKEN = os.environ.get("HF_API_TOKEN")
79
+
80
  if not HF_API_TOKEN:
81
  return "⚠️ Hugging Face API token not found."
82
 
 
127
  "parameters": {
128
  "max_new_tokens": 300,
129
  "temperature": 0.7,
130
+ "top_p": 0.9,
131
+ "return_full_text": False
132
  }
133
  }
134
 
135
+ headers = {
136
+ "Authorization": f"Bearer {HF_API_TOKEN}",
137
+ "Content-Type": "application/json"
138
+ }
139
+
140
  try:
141
+ response = requests.post(
142
+ "https://api-inference.huggingface.co/models/cropinailab/aksara_v1",
143
+ headers=headers,
144
+ json=payload,
145
+ timeout=(10, 60)
146
+ )
147
+
148
  output = response.json()
149
 
150
+ # Robust HF response handling
151
+ if isinstance(output, list) and len(output) > 0:
152
+ return output[0].get("generated_text", "").strip()
153
+
154
+ if isinstance(output, dict) and "error" in output:
155
+ return f"LLM Error: {output['error']}"
156
 
157
+ return "⚠️ No response from LLM."
158
 
159
  except Exception as e:
160
+ return f"LLM Error: {str(e)}"
161
 
162
  # ---------------- PREDICTION ----------------
163
  def predict(image_input, plant, language):