vishwak1 commited on
Commit
c0ea153
·
verified ·
1 Parent(s): fb17207

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py CHANGED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from tensorflow.keras.models import load_model
3
+ from tensorflow.keras.preprocessing.image import img_to_array
4
+ from PIL import Image
5
+ import numpy as np
6
+ import requests
7
+
8
+ # Load your trained .h5 model
9
+ model = load_model("model.h5")
10
+
11
+ # Hugging Face LLM (e.g., Mistral)
12
+ HF_TOKEN = "hf_your_token_here"
13
+ LLM_API =
14
+ "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-In
15
+ struct-v0.1"
16
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"}
17
+
18
+ # Prediction and explanation function
19
+ def classify_and_explain(image):
20
+ image = image.resize((225, 225))
21
+ img_array = img_to_array(image) / 255.0
22
+ img_array = np.expand_dims(img_array, axis=0)
23
+
24
+ prediction = model.predict(img_array)
25
+ predicted_class = int(np.argmax(prediction, axis=1)[0])
26
+ class_label = f"Class_{predicted_class}" # Or use actual class
27
+ names if available
28
+
29
+ # Get explanation from LLM
30
+ prompt = f"Explain the plant disease {class_label} and how to
31
+ treat it."
32
+ response = requests.post(LLM_API, headers=headers,
33
+ json={"inputs": prompt})
34
+ llm_text = response.json()[0]['generated_text'] if
35
+ isinstance(response.json(), list) else response.json()
36
+
37
+ return class_label, llm_text
38
+
39
+ # Gradio UI
40
+ interface = gr.Interface(
41
+ fn=classify_and_explain,
42
+ inputs=gr.Image(type="pil"),
43
+ outputs=["text", "text"],
44
+ title="Plant Disease Predictor & Explainer",
45
+ description="Upload a leaf image to detect plant disease and get
46
+ treatment advice using LLM"
47
+ )
48
+ interface.launch()