{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from fastai.vision.all import *\n", "\n", "learn = load_learner('model.pkl')" ] }, { "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", "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()" ] }, { "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": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "(, 'http://127.0.0.1:7860/', None)" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "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.13 64-bit", "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.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)]" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "b671c20432fcd147198c92e7f072af9e705f087eb990bee22b07f08caab9f630" } } }, "nbformat": 4, "nbformat_minor": 2 }