yasirfaizahmed commited on
Commit
4020c78
1 Parent(s): 058e617

init commit

Browse files
Files changed (3) hide show
  1. model_uploader.ipynb +27 -0
  2. model_utilizer.ipynb +84 -0
  3. notes/mnist.ipynb +120 -0
model_uploader.ipynb ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "import huggingface_hub as hb\n",
10
+ "from tensorflow import keras\n",
11
+ "\n",
12
+ "# Load the model\n",
13
+ "model = keras.models.load_model('../../models/imsoumyaneel-sentiment_analysis_llama2.keras')\n",
14
+ "\n",
15
+ "# Upload the model to Hugging Face\n",
16
+ "hub.push_to_hub_keras(model, repo_id=\"yasirfaizahmed/sentiment-text-classifications\")"
17
+ ]
18
+ }
19
+ ],
20
+ "metadata": {
21
+ "language_info": {
22
+ "name": "python"
23
+ }
24
+ },
25
+ "nbformat": 4,
26
+ "nbformat_minor": 2
27
+ }
model_utilizer.ipynb ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 13,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "from tensorflow.keras.preprocessing.text import Tokenizer\n",
10
+ "from tensorflow.keras.preprocessing.sequence import pad_sequences\n",
11
+ "from tensorflow import keras\n",
12
+ "from pathlib import Path as pp\n",
13
+ "\n",
14
+ "import os\n",
15
+ "notebook_path = os.getcwd()"
16
+ ]
17
+ },
18
+ {
19
+ "cell_type": "code",
20
+ "execution_count": 19,
21
+ "metadata": {},
22
+ "outputs": [
23
+ {
24
+ "ename": "ValueError",
25
+ "evalue": "File not found: filepath=../imsoumyaneel-sentiment_analysis_llama2.keras. Please ensure the file is an accessible `.keras` zip file.",
26
+ "output_type": "error",
27
+ "traceback": [
28
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
29
+ "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
30
+ "Cell \u001b[0;32mIn[19], line 15\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m padded_text\n\u001b[1;32m 14\u001b[0m \u001b[38;5;66;03m# load model\u001b[39;00m\n\u001b[0;32m---> 15\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[43mkeras\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmodels\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mload_model\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m../imsoumyaneel-sentiment_analysis_llama2.keras\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# Replace \"your_model.h5\" with the path to your trained model\u001b[39;00m\n\u001b[1;32m 17\u001b[0m \u001b[38;5;66;03m# Preprocess the custom input text\u001b[39;00m\n\u001b[1;32m 18\u001b[0m preprocessed_text \u001b[38;5;241m=\u001b[39m preprocess_text(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mi love this thing!\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
31
+ "File \u001b[0;32m~/Documents/models/twitter_model/.venv/lib/python3.10/site-packages/keras/src/saving/saving_api.py:185\u001b[0m, in \u001b[0;36mload_model\u001b[0;34m(filepath, custom_objects, compile, safe_mode)\u001b[0m\n\u001b[1;32m 183\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m legacy_h5_format\u001b[38;5;241m.\u001b[39mload_model_from_hdf5(filepath)\n\u001b[1;32m 184\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28mstr\u001b[39m(filepath)\u001b[38;5;241m.\u001b[39mendswith(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m.keras\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[0;32m--> 185\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 186\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFile not found: filepath=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfilepath\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 187\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPlease ensure the file is an accessible `.keras` \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 188\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mzip file.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 189\u001b[0m )\n\u001b[1;32m 190\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 191\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 192\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFile format not supported: filepath=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfilepath\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 193\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mKeras 3 only supports V3 `.keras` files and \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 202\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmight have a different name).\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 203\u001b[0m )\n",
32
+ "\u001b[0;31mValueError\u001b[0m: File not found: filepath=../imsoumyaneel-sentiment_analysis_llama2.keras. Please ensure the file is an accessible `.keras` zip file."
33
+ ]
34
+ }
35
+ ],
36
+ "source": [
37
+ "tokenizer = Tokenizer()\n",
38
+ "max_length = 200\n",
39
+ "models_dir = pp(pp(notebook_path).parent, 'models')\n",
40
+ "\n",
41
+ "\n",
42
+ "# use model\n",
43
+ "def preprocess_text(text):\n",
44
+ " # Tokenize the text\n",
45
+ " tokenized_text = tokenizer.texts_to_sequences([text])\n",
46
+ " # Pad sequences to the same length as training data\n",
47
+ " padded_text = pad_sequences(tokenized_text, maxlen=max_length, padding='post')\n",
48
+ " return padded_text\n",
49
+ "\n",
50
+ "# load model\n",
51
+ "model = keras.models.load_model('../imsoumyaneel-sentiment_analysis_llama2.keras') # Replace \"your_model.h5\" with the path to your trained model\n",
52
+ "\n",
53
+ "# Preprocess the custom input text\n",
54
+ "preprocessed_text = preprocess_text(\"i love this thing!\")\n",
55
+ "\n",
56
+ "# Make predictions\n",
57
+ "predictions = model.predict(preprocessed_text)\n",
58
+ "\n",
59
+ "predictions[0][0]"
60
+ ]
61
+ }
62
+ ],
63
+ "metadata": {
64
+ "kernelspec": {
65
+ "display_name": ".venv",
66
+ "language": "python",
67
+ "name": "python3"
68
+ },
69
+ "language_info": {
70
+ "codemirror_mode": {
71
+ "name": "ipython",
72
+ "version": 3
73
+ },
74
+ "file_extension": ".py",
75
+ "mimetype": "text/x-python",
76
+ "name": "python",
77
+ "nbconvert_exporter": "python",
78
+ "pygments_lexer": "ipython3",
79
+ "version": "3.10.12"
80
+ }
81
+ },
82
+ "nbformat": 4,
83
+ "nbformat_minor": 2
84
+ }
notes/mnist.ipynb ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 14,
6
+ "id": "b2d6f096-6123-4dd0-ae4f-c9abf70889c4",
7
+ "metadata": {
8
+ "execution": {
9
+ "iopub.execute_input": "2024-04-03T17:21:13.044450Z",
10
+ "iopub.status.busy": "2024-04-03T17:21:13.043912Z",
11
+ "iopub.status.idle": "2024-04-03T17:21:13.374087Z",
12
+ "shell.execute_reply": "2024-04-03T17:21:13.373277Z",
13
+ "shell.execute_reply.started": "2024-04-03T17:21:13.044419Z"
14
+ }
15
+ },
16
+ "outputs": [
17
+ {
18
+ "name": "stderr",
19
+ "output_type": "stream",
20
+ "text": [
21
+ "Reusing dataset mnist (/root/.cache/huggingface/datasets/mnist/mnist/1.0.0/fda16c03c4ecfb13f165ba7e29cf38129ce035011519968cdaf74894ce91c9d4)\n"
22
+ ]
23
+ },
24
+ {
25
+ "data": {
26
+ "application/vnd.jupyter.widget-view+json": {
27
+ "model_id": "cd2dd3c8b22b49718b3c66585ca3559c",
28
+ "version_major": 2,
29
+ "version_minor": 0
30
+ },
31
+ "text/plain": [
32
+ " 0%| | 0/2 [00:00<?, ?it/s]"
33
+ ]
34
+ },
35
+ "metadata": {},
36
+ "output_type": "display_data"
37
+ }
38
+ ],
39
+ "source": [
40
+ "from datasets import load_dataset\n",
41
+ "\n",
42
+ "dataset = load_dataset(\"mnist\")"
43
+ ]
44
+ },
45
+ {
46
+ "cell_type": "code",
47
+ "execution_count": 16,
48
+ "id": "7c5ea43d-6a70-4deb-a90e-1d7758c961a3",
49
+ "metadata": {
50
+ "execution": {
51
+ "iopub.execute_input": "2024-04-03T17:21:27.628082Z",
52
+ "iopub.status.busy": "2024-04-03T17:21:27.627048Z",
53
+ "iopub.status.idle": "2024-04-03T17:21:27.632687Z",
54
+ "shell.execute_reply": "2024-04-03T17:21:27.632002Z",
55
+ "shell.execute_reply.started": "2024-04-03T17:21:27.628051Z"
56
+ }
57
+ },
58
+ "outputs": [],
59
+ "source": [
60
+ "import numpy as np\n",
61
+ "from PIL import Image\n",
62
+ "import io\n",
63
+ "\n",
64
+ "def convert(byte_like_str_image):\n",
65
+ " return np.asarray(Image.open(io.BytesIO(byte_like_str_image['bytes'])))\n"
66
+ ]
67
+ },
68
+ {
69
+ "cell_type": "code",
70
+ "execution_count": 18,
71
+ "id": "daafde17-8100-4f49-b27e-8aad43b129c6",
72
+ "metadata": {
73
+ "execution": {
74
+ "iopub.execute_input": "2024-04-03T17:22:20.343090Z",
75
+ "iopub.status.busy": "2024-04-03T17:22:20.341876Z",
76
+ "iopub.status.idle": "2024-04-03T17:22:27.888815Z",
77
+ "shell.execute_reply": "2024-04-03T17:22:27.887812Z",
78
+ "shell.execute_reply.started": "2024-04-03T17:22:20.343047Z"
79
+ }
80
+ },
81
+ "outputs": [],
82
+ "source": [
83
+ "dataset_train = dataset['train'].to_pandas()\n",
84
+ "dataset_train['image'] = dataset_train['image'].map(convert)\n",
85
+ "\n",
86
+ "dataset_test = dataset['test'].to_pandas()\n",
87
+ "dataset_test['image'] = dataset_test['image'].map(convert)\n"
88
+ ]
89
+ },
90
+ {
91
+ "cell_type": "code",
92
+ "execution_count": null,
93
+ "id": "72022fd2-000d-4d5c-88d5-9afc62c283d5",
94
+ "metadata": {},
95
+ "outputs": [],
96
+ "source": []
97
+ }
98
+ ],
99
+ "metadata": {
100
+ "kernelspec": {
101
+ "display_name": "Python 3 (ipykernel)",
102
+ "language": "python",
103
+ "name": "python3"
104
+ },
105
+ "language_info": {
106
+ "codemirror_mode": {
107
+ "name": "ipython",
108
+ "version": 3
109
+ },
110
+ "file_extension": ".py",
111
+ "mimetype": "text/x-python",
112
+ "name": "python",
113
+ "nbconvert_exporter": "python",
114
+ "pygments_lexer": "ipython3",
115
+ "version": "3.9.16"
116
+ }
117
+ },
118
+ "nbformat": 4,
119
+ "nbformat_minor": 5
120
+ }