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
1123plant_disease_model.tfliteplant_disease_model.tflite.envapp2.py3 unmodified lines456789101011121314151617181913202122231724252021262728292630312829303233343517 unmodified lines535455565758596061626364656667686970717273747576777879808182838457585960616263858687888990919293949596979899100101102103104105106107108661093 unmodified linesimport numpy as npimport tensorflow as tfimport gradio as grimport requestsfrom huggingface_hub import hf_hub_download
# -----------------------------# Download TFLite model from Hugging Face model repo# 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 model repo repo_id="sidd-harth011/checkingPDRMod", # β
your repo filename="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# Load class indices (local file in 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))class_indices = json.load(open("class_indices.json"))
# -----------------------------# Preprocessing function17 unmodified lines predicted_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# -----------------------------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.")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 classifier with 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 Chatbot with 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__": interface.launch() demo.launch()123345tensorflownumpypillowpillowopenaianthropic.gitignore+3-1
app.py+60-17
requirements.txt+3-1