TIF_E41200999/app.ipynb

241 lines
6.2 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Buah Alpukat',\n",
" 'Buah Apel',\n",
" 'Buah Belimbing',\n",
" 'Buah Naga',\n",
" 'Buah Lemon',\n",
" 'Buah Nanas',\n",
" 'Buah Pir',\n",
" 'Buah Pisang',\n",
" 'Buah Salak',\n",
" 'Buah Stroberi']"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from fastai.vision.all import *\n",
"\n",
"learn = load_learner('model.pkl')\n",
"\n",
"\n",
"def format_name(name):\n",
" if name != 'buah naga':\n",
" return f'Buah {name.title()}'\n",
" \n",
" return name.title()\n",
"\n",
"\n",
"names = list(map(format_name, learn.dls.vocab))\n",
"names\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import firebase_admin\n",
"from firebase_admin import credentials\n",
"from firebase_admin import firestore\n",
"\n",
"cred = credentials.Certificate('firebase_key.json')\n",
"app = firebase_admin.initialize_app(cred)\n",
"db = firestore.client()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"doc_ref = db.collection(u'preds').add({\n",
" u'image': u'Lovelace',\n",
" u'prediction': 1815,\n",
" u'time_added': firestore.SERVER_TIMESTAMP\n",
"})"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"preds_ref = db.collection(u'preds')\n",
"docs = preds_ref.stream()\n",
"\n",
"for doc in docs:\n",
" data = base64.b64decode(doc.to_dict()['image'])\n",
" buff = BytesIO(data)\n",
" img = Image.open(buff)\n",
" img.show()"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running on local URL: http://127.0.0.1:7860\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7860/\" width=\"900\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(<gradio.routes.App at 0x7f3279fb6af0>, 'http://127.0.0.1:7860/', None)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"<style>\n",
" /* Turns off some styling */\n",
" progress {\n",
" /* gets rid of default border in Firefox and Opera. */\n",
" border: none;\n",
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
" background-size: auto;\n",
" }\n",
" progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
" background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
" }\n",
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
" background: #F44336;\n",
" }\n",
"</style>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import gradio as gr\n",
"from fastai.vision.all import *\n",
"import firebase_admin\n",
"from firebase_admin import credentials\n",
"from firebase_admin import firestore\n",
"import base64\n",
"from io import BytesIO\n",
"\n",
"cred = credentials.Certificate('firebase_key.json')\n",
"app = firebase_admin.initialize_app(cred)\n",
"db = firestore.client()\n",
"\n",
"learn = load_learner('model.pkl')\n",
"names = list(map(lambda name: name.title(), learn.dls.vocab))\n",
"\n",
"\n",
"def classify(image):\n",
" buffered = BytesIO()\n",
" image.save(buffered, format=\"JPEG\")\n",
" img_str = base64.b64encode(buffered.getvalue())\n",
"\n",
" pred, idx, probs = learn.predict(np.asarray(image))\n",
"\n",
" db.collection(u'preds').add({\n",
" u'image': img_str,\n",
" u'prediction': pred.title(),\n",
" u'time_added': firestore.SERVER_TIMESTAMP\n",
" })\n",
"\n",
" return dict(zip(names, map(float, probs)))\n",
"\n",
"\n",
"with gr.Blocks() as demo:\n",
" with gr.Row():\n",
" with gr.Column():\n",
" image_input = gr.Image(label=\"Gambar\", shape=(200, 200), type='pil')\n",
" predict_btn = gr.Button(\"Klasifikasi\", variant='primary')\n",
" with gr.Column():\n",
" chart = gr.Label(label=\"Hasil\")\n",
"\n",
" predict_btn.click(fn=classify, inputs=image_input,\n",
" outputs=chart, api_name='classify_image')\n",
"\n",
"demo.launch()\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.12 ('base')",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "fdff80770fe959a274e11f3f42c90636d375da6f3b1d0ebe5804fc741631cbf3"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}