Spaces:
Sleeping
Sleeping
Commit Β·
fc33bcb
1
Parent(s): 4ca82bd
placed
Browse filesFiles changed (3)
- .gitignore+3-1
- app.py+60-17
- requirements.txt+3-1
1plant_disease_model.tflite123plant_disease_model.tflite.envapp2.py
3 unmodified lines45678910111213141516171819202122232425262728293031323317 unmodified lines515253545556575859606162636465663 unmodified linesimport numpy as npimport tensorflow as tfimport gradio as grfrom huggingface_hub import hf_hub_download# -----------------------------# Download TFLite model from Hugging Face model repo# -----------------------------model_path = hf_hub_download(repo_id="sidd-harth011/checkingPDRMod", # your model repofilename="plant_disease_model.tflite")# Load TFLite modelinterpreter = tf.lite.Interpreter(model_path=model_path)interpreter.allocate_tensors()# Get input and output detailsinput_details = interpreter.get_input_details()output_details = interpreter.get_output_details()# -----------------------------# Load class indices locally from project repo# -----------------------------working_dir = os.path.dirname(os.path.abspath(__file__))class_indices_path = os.path.join(working_dir, "class_indices.json")class_indices = json.load(open(class_indices_path))# -----------------------------# Preprocessing function17 unmodified linespredicted_class_name = class_indices[str(predicted_class_index)]return f"Prediction: {predicted_class_name}"# -----------------------------# Gradio Interface# -----------------------------interface = gr.Interface(fn=predict_image_class,inputs=gr.Image(type="pil", label="Upload an Image"),outputs=gr.Textbox(label="Prediction"),title="π± Plant Disease Classifier (TFLite)",description="Upload a plant leaf image to classify its disease using a compressed TFLite model hosted on Hugging Face.")if __name__ == "__main__":interface.launch()3 unmodified lines456789101112131415161718192021222324252627282930313233343517 unmodified lines53545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081093 unmodified linesimport numpy as npimport tensorflow as tfimport gradio as grimport requestsfrom huggingface_hub import hf_hub_download# -----------------------------# Load API key from Hugging Face Secrets# -----------------------------OPENAI_KEY = os.getenv("OPENAI_API_KEY")OPENAI_URL = "https://api.openai.com/v1/chat/completions"# -----------------------------# Load TFLite model from Hugging Face Hub# -----------------------------model_path = hf_hub_download(repo_id="sidd-harth011/checkingPDRMod", # β your repofilename="plant_disease_model.tflite")interpreter = tf.lite.Interpreter(model_path=model_path)interpreter.allocate_tensors()input_details = interpreter.get_input_details()output_details = interpreter.get_output_details()# -----------------------------# Load class indices (local file in repo)# -----------------------------class_indices = json.load(open("class_indices.json"))# -----------------------------# Preprocessing function17 unmodified linespredicted_class_name = class_indices[str(predicted_class_index)]return f"Prediction: {predicted_class_name}"# -----------------------------# OpenAI Chatbot (single-turn, no history)# -----------------------------def openai_chatbot(user_message):payload = {"model": "gpt-4o-mini", # β lightweight, works in Spaces"messages": [{"role": "user", "content": user_message}],"temperature": 0.7,"max_tokens": 500}headers = {"Authorization": f"Bearer {OPENAI_KEY}","Content-Type": "application/json"}response = requests.post(OPENAI_URL, headers=headers, json=payload)if response.status_code == 200:bot_message = response.json()["choices"][0]["message"]["content"]else:print("Error:", response.status_code, response.text)bot_message = "β οΈ Sorry, I couldn't process that. Try again!"return bot_message# -----------------------------# Gradio Interface# -----------------------------with gr.Blocks(title="π± Plant Disease Classifier & AI Chatbot (OpenAI)") as demo:gr.Markdown("## π± Plant Disease Classifier with AI Assistant (OpenAI)")with gr.Row():# Left: Plant classifierwith gr.Column(scale=1):gr.Markdown("### Upload Image")image_input = gr.Image(type="pil", label="Upload a Plant Leaf Image")predict_button = gr.Button("Classify")prediction_output = gr.Textbox(label="Prediction")predict_button.click(fn=predict_image_class, inputs=image_input, outputs=prediction_output)# Right: AI Chatbotwith gr.Column(scale=1):gr.Markdown("### π€ AI Chatbot")msg = gr.Textbox(label="Type your message")response_box = gr.Textbox(label="Bot Response", lines=5)send_btn = gr.Button("Send")send_btn.click(openai_chatbot, inputs=msg, outputs=response_box)if __name__ == "__main__":demo.launch()
123tensorflownumpypillow12345tensorflownumpypillowopenaianthropic
.gitignore+3-1
app.py+60-17
requirements.txt+3-1