Spaces:
Configuration error
Configuration error
File size: 1,465 Bytes
4646d15 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import gradio as gr\n",
"import tensorflow as tf\n",
"import numpy as np\n",
"\n",
"# Load the pre-trained model\n",
"model = tf.keras.models.load_model(\"\")\n",
"\n",
"# Define the prediction function\n",
"def classify_image(image):\n",
" # Preprocess the image\n",
" image = tf.image.resize(image, (224, 224))\n",
" image = tf.keras.applications.mobilenet_v2.preprocess_input(image)\n",
" image = np.expand_dims(image, axis=0)\n",
"\n",
" # Make predictions\n",
" predictions = model.predict(image)\n",
" label = np.argmax(predictions[0])\n",
" confidence = predictions[0][label]\n",
"\n",
" return label, confidence\n",
"\n",
"# Create the Gradio interface\n",
"inputs = gr.inputs.Image()\n",
"outputs = gr.outputs.Label(num_top_classes=3)\n",
"\n",
"# Launch the interface with a public link\n",
"gr.Interface(\n",
" fn=classify_image, \n",
" inputs=inputs, \n",
" outputs=outputs, \n",
" title=\"Micro-Guru\", \n",
" description=\"Upload an image and the model will predict its class along with the confidence level.\"\n",
").launch(share=True)"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|