MojoHz commited on
Commit
2ce9573
Β·
verified Β·
1 Parent(s): b12e794

Upload gradio_huggingface.ipynb

Browse files
Files changed (1) hide show
  1. gradio_huggingface.ipynb +247 -0
gradio_huggingface.ipynb ADDED
@@ -0,0 +1,247 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": []
7
+ },
8
+ "kernelspec": {
9
+ "name": "python3",
10
+ "display_name": "Python 3"
11
+ },
12
+ "language_info": {
13
+ "name": "python"
14
+ }
15
+ },
16
+ "cells": [
17
+ {
18
+ "source": [
19
+ "!pip install gradio"
20
+ ],
21
+ "cell_type": "code",
22
+ "metadata": {
23
+ "colab": {
24
+ "base_uri": "https://localhost:8080/"
25
+ },
26
+ "id": "AG3LJPwN8_bz",
27
+ "outputId": "579e2d88-15d8-4500-a6e8-ea10ec00fabf"
28
+ },
29
+ "execution_count": 2,
30
+ "outputs": [
31
+ {
32
+ "output_type": "stream",
33
+ "name": "stdout",
34
+ "text": [
35
+ "Collecting gradio\n",
36
+ " Downloading gradio-4.41.0-py3-none-any.whl.metadata (15 kB)\n",
37
+ "Collecting aiofiles<24.0,>=22.0 (from gradio)\n",
38
+ " Downloading aiofiles-23.2.1-py3-none-any.whl.metadata (9.7 kB)\n",
39
+ "Requirement already satisfied: anyio<5.0,>=3.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.7.1)\n",
40
+ "Collecting fastapi (from gradio)\n",
41
+ " Downloading fastapi-0.112.1-py3-none-any.whl.metadata (27 kB)\n",
42
+ "Collecting ffmpy (from gradio)\n",
43
+ " Downloading ffmpy-0.4.0-py3-none-any.whl.metadata (2.9 kB)\n",
44
+ "Collecting gradio-client==1.3.0 (from gradio)\n",
45
+ " Downloading gradio_client-1.3.0-py3-none-any.whl.metadata (7.1 kB)\n",
46
+ "Collecting httpx>=0.24.1 (from gradio)\n",
47
+ " Downloading httpx-0.27.0-py3-none-any.whl.metadata (7.2 kB)\n",
48
+ "Requirement already satisfied: huggingface-hub>=0.19.3 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.23.5)\n",
49
+ "Requirement already satisfied: importlib-resources<7.0,>=1.3 in /usr/local/lib/python3.10/dist-packages (from gradio) (6.4.0)\n",
50
+ "Requirement already satisfied: jinja2<4.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.1.4)\n",
51
+ "Requirement already satisfied: markupsafe~=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.1.5)\n",
52
+ "Requirement already satisfied: matplotlib~=3.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.7.1)\n",
53
+ "Requirement already satisfied: numpy<3.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (1.26.4)\n",
54
+ "Collecting orjson~=3.0 (from gradio)\n",
55
+ " Downloading orjson-3.10.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (50 kB)\n",
56
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m50.4/50.4 kB\u001b[0m \u001b[31m3.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
57
+ "\u001b[?25hRequirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from gradio) (24.1)\n",
58
+ "Requirement already satisfied: pandas<3.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.1.4)\n",
59
+ "Requirement already satisfied: pillow<11.0,>=8.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (9.4.0)\n",
60
+ "Requirement already satisfied: pydantic>=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.8.2)\n",
61
+ "Collecting pydub (from gradio)\n",
62
+ " Downloading pydub-0.25.1-py2.py3-none-any.whl.metadata (1.4 kB)\n",
63
+ "Collecting python-multipart>=0.0.9 (from gradio)\n",
64
+ " Downloading python_multipart-0.0.9-py3-none-any.whl.metadata (2.5 kB)\n",
65
+ "Requirement already satisfied: pyyaml<7.0,>=5.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (6.0.2)\n",
66
+ "Collecting ruff>=0.2.2 (from gradio)\n",
67
+ " Downloading ruff-0.6.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (25 kB)\n",
68
+ "Collecting semantic-version~=2.0 (from gradio)\n",
69
+ " Downloading semantic_version-2.10.0-py2.py3-none-any.whl.metadata (9.7 kB)\n",
70
+ "Collecting tomlkit==0.12.0 (from gradio)\n",
71
+ " Downloading tomlkit-0.12.0-py3-none-any.whl.metadata (2.7 kB)\n",
72
+ "Requirement already satisfied: typer<1.0,>=0.12 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.12.3)\n",
73
+ "Requirement already satisfied: typing-extensions~=4.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (4.12.2)\n",
74
+ "Requirement already satisfied: urllib3~=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.0.7)\n",
75
+ "Collecting uvicorn>=0.14.0 (from gradio)\n",
76
+ " Downloading uvicorn-0.30.6-py3-none-any.whl.metadata (6.6 kB)\n",
77
+ "Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from gradio-client==1.3.0->gradio) (2024.6.1)\n",
78
+ "Collecting websockets<13.0,>=10.0 (from gradio-client==1.3.0->gradio)\n",
79
+ " Downloading websockets-12.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (6.6 kB)\n",
80
+ "Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.10/dist-packages (from anyio<5.0,>=3.0->gradio) (3.7)\n",
81
+ "Requirement already satisfied: sniffio>=1.1 in /usr/local/lib/python3.10/dist-packages (from anyio<5.0,>=3.0->gradio) (1.3.1)\n",
82
+ "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5.0,>=3.0->gradio) (1.2.2)\n",
83
+ "Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx>=0.24.1->gradio) (2024.7.4)\n",
84
+ "Collecting httpcore==1.* (from httpx>=0.24.1->gradio)\n",
85
+ " Downloading httpcore-1.0.5-py3-none-any.whl.metadata (20 kB)\n",
86
+ "Collecting h11<0.15,>=0.13 (from httpcore==1.*->httpx>=0.24.1->gradio)\n",
87
+ " Downloading h11-0.14.0-py3-none-any.whl.metadata (8.2 kB)\n",
88
+ "Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.3->gradio) (3.15.4)\n",
89
+ "Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.3->gradio) (2.32.3)\n",
90
+ "Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.3->gradio) (4.66.5)\n",
91
+ "Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (1.2.1)\n",
92
+ "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (0.12.1)\n",
93
+ "Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (4.53.1)\n",
94
+ "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (1.4.5)\n",
95
+ "Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (3.1.2)\n",
96
+ "Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (2.8.2)\n",
97
+ "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas<3.0,>=1.0->gradio) (2024.1)\n",
98
+ "Requirement already satisfied: tzdata>=2022.1 in /usr/local/lib/python3.10/dist-packages (from pandas<3.0,>=1.0->gradio) (2024.1)\n",
99
+ "Requirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic>=2.0->gradio) (0.7.0)\n",
100
+ "Requirement already satisfied: pydantic-core==2.20.1 in /usr/local/lib/python3.10/dist-packages (from pydantic>=2.0->gradio) (2.20.1)\n",
101
+ "Requirement already satisfied: click>=8.0.0 in /usr/local/lib/python3.10/dist-packages (from typer<1.0,>=0.12->gradio) (8.1.7)\n",
102
+ "Requirement already satisfied: shellingham>=1.3.0 in /usr/local/lib/python3.10/dist-packages (from typer<1.0,>=0.12->gradio) (1.5.4)\n",
103
+ "Requirement already satisfied: rich>=10.11.0 in /usr/local/lib/python3.10/dist-packages (from typer<1.0,>=0.12->gradio) (13.7.1)\n",
104
+ "Collecting starlette<0.39.0,>=0.37.2 (from fastapi->gradio)\n",
105
+ " Downloading starlette-0.38.2-py3-none-any.whl.metadata (5.9 kB)\n",
106
+ "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7->matplotlib~=3.0->gradio) (1.16.0)\n",
107
+ "Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.10/dist-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (3.0.0)\n",
108
+ "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.10/dist-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (2.16.1)\n",
109
+ "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.19.3->gradio) (3.3.2)\n",
110
+ "Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.10/dist-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->typer<1.0,>=0.12->gradio) (0.1.2)\n",
111
+ "Downloading gradio-4.41.0-py3-none-any.whl (12.6 MB)\n",
112
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m12.6/12.6 MB\u001b[0m \u001b[31m57.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
113
+ "\u001b[?25hDownloading gradio_client-1.3.0-py3-none-any.whl (318 kB)\n",
114
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m318.7/318.7 kB\u001b[0m \u001b[31m21.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
115
+ "\u001b[?25hDownloading tomlkit-0.12.0-py3-none-any.whl (37 kB)\n",
116
+ "Downloading aiofiles-23.2.1-py3-none-any.whl (15 kB)\n",
117
+ "Downloading httpx-0.27.0-py3-none-any.whl (75 kB)\n",
118
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m4.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
119
+ "\u001b[?25hDownloading httpcore-1.0.5-py3-none-any.whl (77 kB)\n",
120
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m5.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
121
+ "\u001b[?25hDownloading orjson-3.10.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (141 kB)\n",
122
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m141.9/141.9 kB\u001b[0m \u001b[31m10.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
123
+ "\u001b[?25hDownloading python_multipart-0.0.9-py3-none-any.whl (22 kB)\n",
124
+ "Downloading ruff-0.6.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.2 MB)\n",
125
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m10.2/10.2 MB\u001b[0m \u001b[31m60.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
126
+ "\u001b[?25hDownloading semantic_version-2.10.0-py2.py3-none-any.whl (15 kB)\n",
127
+ "Downloading uvicorn-0.30.6-py3-none-any.whl (62 kB)\n",
128
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m62.8/62.8 kB\u001b[0m \u001b[31m4.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
129
+ "\u001b[?25hDownloading fastapi-0.112.1-py3-none-any.whl (93 kB)\n",
130
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m93.2/93.2 kB\u001b[0m \u001b[31m7.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
131
+ "\u001b[?25hDownloading ffmpy-0.4.0-py3-none-any.whl (5.8 kB)\n",
132
+ "Downloading pydub-0.25.1-py2.py3-none-any.whl (32 kB)\n",
133
+ "Downloading h11-0.14.0-py3-none-any.whl (58 kB)\n",
134
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m3.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
135
+ "\u001b[?25hDownloading starlette-0.38.2-py3-none-any.whl (72 kB)\n",
136
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m72.0/72.0 kB\u001b[0m \u001b[31m5.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
137
+ "\u001b[?25hDownloading websockets-12.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (130 kB)\n",
138
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m130.2/130.2 kB\u001b[0m \u001b[31m10.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
139
+ "\u001b[?25hInstalling collected packages: pydub, websockets, tomlkit, semantic-version, ruff, python-multipart, orjson, h11, ffmpy, aiofiles, uvicorn, starlette, httpcore, httpx, fastapi, gradio-client, gradio\n",
140
+ " Attempting uninstall: tomlkit\n",
141
+ " Found existing installation: tomlkit 0.13.0\n",
142
+ " Uninstalling tomlkit-0.13.0:\n",
143
+ " Successfully uninstalled tomlkit-0.13.0\n",
144
+ "Successfully installed aiofiles-23.2.1 fastapi-0.112.1 ffmpy-0.4.0 gradio-4.41.0 gradio-client-1.3.0 h11-0.14.0 httpcore-1.0.5 httpx-0.27.0 orjson-3.10.7 pydub-0.25.1 python-multipart-0.0.9 ruff-0.6.1 semantic-version-2.10.0 starlette-0.38.2 tomlkit-0.12.0 uvicorn-0.30.6 websockets-12.0\n"
145
+ ]
146
+ }
147
+ ]
148
+ },
149
+ {
150
+ "cell_type": "code",
151
+ "execution_count": 3,
152
+ "metadata": {
153
+ "colab": {
154
+ "base_uri": "https://localhost:8080/",
155
+ "height": 646
156
+ },
157
+ "id": "sk8EestI8YRY",
158
+ "outputId": "d0434b66-0088-4611-88eb-e031e246e264"
159
+ },
160
+ "outputs": [
161
+ {
162
+ "output_type": "stream",
163
+ "name": "stdout",
164
+ "text": [
165
+ "Setting queue=True in a Colab notebook requires sharing enabled. Setting `share=True` (you can turn this off by setting `share=False` in `launch()` explicitly).\n",
166
+ "\n",
167
+ "Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n",
168
+ "Running on public URL: https://8f53fe8496666cb51a.gradio.live\n",
169
+ "\n",
170
+ "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n"
171
+ ]
172
+ },
173
+ {
174
+ "output_type": "display_data",
175
+ "data": {
176
+ "text/plain": [
177
+ "<IPython.core.display.HTML object>"
178
+ ],
179
+ "text/html": [
180
+ "<div><iframe src=\"https://8f53fe8496666cb51a.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
181
+ ]
182
+ },
183
+ "metadata": {}
184
+ },
185
+ {
186
+ "output_type": "execute_result",
187
+ "data": {
188
+ "text/plain": []
189
+ },
190
+ "metadata": {},
191
+ "execution_count": 3
192
+ }
193
+ ],
194
+ "source": [
195
+ "import gradio as gr\n",
196
+ "import pandas as pd\n",
197
+ "import numpy as np\n",
198
+ "import pickle\n",
199
+ "\n",
200
+ "# Load the trained model from the pickle file\n",
201
+ "with open('best_arima_models.pkl', 'rb') as f:\n",
202
+ " model = pickle.load(f)\n",
203
+ "\n",
204
+ "def predict_demand(mapped_code, num_months):\n",
205
+ " try:\n",
206
+ " # Retrieve the specific model for the mapped code\n",
207
+ " if mapped_code not in model:\n",
208
+ " return f\"No model found for Mapped Code: {mapped_code}\"\n",
209
+ "\n",
210
+ " model_for_code = model[mapped_code]\n",
211
+ "\n",
212
+ " # Generate a date range for the prediction period\n",
213
+ " dates = pd.date_range(start=pd.Timestamp.today(), periods=num_months, freq='M')\n",
214
+ "\n",
215
+ " # Make predictions\n",
216
+ " future_steps = len(dates)\n",
217
+ " forecast = model_for_code.forecast(steps=future_steps)\n",
218
+ "\n",
219
+ " # Prepare a DataFrame for display\n",
220
+ " df = pd.DataFrame({\n",
221
+ " 'Date': dates.strftime('%Y-%m'),\n",
222
+ " 'Predicted Demand': forecast\n",
223
+ " })\n",
224
+ "\n",
225
+ " return df\n",
226
+ "\n",
227
+ " except Exception as e:\n",
228
+ " print(f\"Error occurred: {e}\")\n",
229
+ " return f\"An error occurred: {str(e)}\"\n",
230
+ "\n",
231
+ "# Gradio Interface Definition\n",
232
+ "gr.Interface(\n",
233
+ " fn=predict_demand,\n",
234
+ " inputs=[\n",
235
+ " gr.Textbox(label=\"Mapped Code\", placeholder=\"Enter mapped code here\"),\n",
236
+ " gr.Slider(minimum=1, maximum=12, step=1, label=\"Number of Months\")\n",
237
+ " ],\n",
238
+ " outputs=[\n",
239
+ " gr.Dataframe(label=\"Predicted Demand\")\n",
240
+ " ],\n",
241
+ " title=\"Demand Forecasting\",\n",
242
+ " description=\"Enter the mapped code and the number of months to predict future demand.\"\n",
243
+ ").launch()\n"
244
+ ]
245
+ }
246
+ ]
247
+ }