abdullahmubeen10 commited on
Commit
f6ed594
·
verified ·
1 Parent(s): 3eba281

Upload 15 files

Browse files
.streamlit/config.toml ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ [theme]
2
+ base="light"
3
+ primaryColor="#29B4E8"
Demo.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import sparknlp
3
+ import os
4
+ import pandas as pd
5
+
6
+ from sparknlp.base import *
7
+ from sparknlp.annotator import *
8
+ from pyspark.ml import Pipeline
9
+ from sparknlp.pretrained import PretrainedPipeline
10
+ from streamlit_tags import st_tags
11
+
12
+ # Page configuration
13
+ st.set_page_config(
14
+ layout="wide",
15
+ initial_sidebar_state="auto"
16
+ )
17
+
18
+ # CSS for styling
19
+ st.markdown("""
20
+ <style>
21
+ .main-title {
22
+ font-size: 36px;
23
+ color: #4A90E2;
24
+ font-weight: bold;
25
+ text-align: center;
26
+ }
27
+ .section {
28
+ background-color: #f9f9f9;
29
+ padding: 10px;
30
+ border-radius: 10px;
31
+ margin-top: 10px;
32
+ }
33
+ .section p, .section ul {
34
+ color: #666666;
35
+ }
36
+ </style>
37
+ """, unsafe_allow_html=True)
38
+
39
+ @st.cache_resource
40
+ def init_spark():
41
+ return sparknlp.start()
42
+
43
+ @st.cache_resource
44
+ def create_pipeline(model, labels):
45
+ image_assembler = ImageAssembler() \
46
+ .setInputCol("image") \
47
+ .setOutputCol("image_assembler")
48
+
49
+ imageClassifier = CLIPForZeroShotClassification \
50
+ .pretrained() \
51
+ .setInputCols(["image_assembler"]) \
52
+ .setOutputCol("label") \
53
+ .setCandidateLabels(labels)
54
+
55
+ pipeline = Pipeline(stages=[
56
+ image_assembler,
57
+ imageClassifier,
58
+ ])
59
+ return pipeline
60
+
61
+ def fit_data(pipeline, data):
62
+ model = pipeline.fit(data)
63
+ light_pipeline = LightPipeline(model)
64
+ annotations_result = light_pipeline.fullAnnotateImage(data)
65
+ return annotations_result[0]['label'][0].result
66
+
67
+ def save_uploadedfile(uploadedfile):
68
+ filepath = os.path.join(IMAGE_FILE_PATH, uploadedfile.name)
69
+ with open(filepath, "wb") as f:
70
+ if hasattr(uploadedfile, 'getbuffer'):
71
+ f.write(uploadedfile.getbuffer())
72
+ else:
73
+ f.write(uploadedfile.read())
74
+
75
+ # Sidebar content
76
+ model = st.sidebar.selectbox(
77
+ "Choose the pretrained model",
78
+ ["CLIPForZeroShotClassification"],
79
+ help="For more info about the models visit: https://sparknlp.org/models"
80
+ )
81
+
82
+ # Set up the page layout
83
+ st.markdown(f'<div class="main-title">CLIPForZeroShotClassification</div>', unsafe_allow_html=True)
84
+ # st.markdown(f'<div class="section"><p>{sub_title}</p></div>', unsafe_allow_html=True)
85
+
86
+ # Reference notebook link in sidebar
87
+ link = """
88
+ <a href="https://github.com/JohnSnowLabs/spark-nlp/blob/master/examples/python/annotation/image/CLIPForZeroShotClassification.ipynb">
89
+ <img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/>
90
+ </a>
91
+ """
92
+ st.sidebar.markdown('Reference notebook:')
93
+ st.sidebar.markdown(link, unsafe_allow_html=True)
94
+
95
+ # Load examples
96
+ IMAGE_FILE_PATH = "/content/sparknlp CLIPForZeroShotClassification/input"
97
+ image_files = sorted([file for file in os.listdir(IMAGE_FILE_PATH) if file.split('.')[-1]=='png' or file.split('.')[-1]=='jpg' or file.split('.')[-1]=='JPEG' or file.split('.')[-1]=='jpeg'])
98
+
99
+ img_options = st.selectbox("Select an image", image_files)
100
+ uploadedfile = st.file_uploader("Try it for yourself!")
101
+
102
+ if uploadedfile:
103
+ file_details = {"FileName":uploadedfile.name,"FileType":uploadedfile.type}
104
+ save_uploadedfile(uploadedfile)
105
+ selected_image = f"{IMAGE_FILE_PATH}/{uploadedfile.name}"
106
+ elif img_options:
107
+ selected_image = f"{IMAGE_FILE_PATH}/{img_options}"
108
+
109
+ candidateLabels = [
110
+ "a photo of a bird",
111
+ "a photo of a cat",
112
+ "a photo of a dog",
113
+ "a photo of a hen",
114
+ "a photo of a hippo",
115
+ "a photo of a room",
116
+ "a photo of a tractor",
117
+ "a photo of an ostrich",
118
+ "a photo of an ox"]
119
+
120
+ lables = st_tags(
121
+ label='Select labels',
122
+ text='Press enter to add more',
123
+ value=candidateLabels,
124
+ maxtags = -1)
125
+
126
+ st.subheader('Classified Image')
127
+
128
+ image_size = st.slider('Image Size', 400, 1000, value=400, step = 100)
129
+
130
+ try:
131
+ st.image(f"{IMAGE_FILE_PATH}/{selected_image}", width=image_size)
132
+ except:
133
+ st.image(selected_image, width=image_size)
134
+
135
+ st.subheader('Classification')
136
+
137
+ init_spark()
138
+ Pipeline = create_pipeline(model, lables)
139
+ output = fit_data(Pipeline, selected_image)
140
+
141
+ st.markdown(f'This document has been classified as : **{output}**')
Dockerfile ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Download base image ubuntu 18.04
2
+ FROM ubuntu:18.04
3
+
4
+ # Set environment variables
5
+ ENV NB_USER jovyan
6
+ ENV NB_UID 1000
7
+ ENV HOME /home/${NB_USER}
8
+
9
+ # Install required packages
10
+ RUN apt-get update && apt-get install -y \
11
+ tar \
12
+ wget \
13
+ bash \
14
+ rsync \
15
+ gcc \
16
+ libfreetype6-dev \
17
+ libhdf5-serial-dev \
18
+ libpng-dev \
19
+ libzmq3-dev \
20
+ python3 \
21
+ python3-dev \
22
+ python3-pip \
23
+ unzip \
24
+ pkg-config \
25
+ software-properties-common \
26
+ graphviz \
27
+ openjdk-8-jdk \
28
+ ant \
29
+ ca-certificates-java \
30
+ && apt-get clean \
31
+ && update-ca-certificates -f;
32
+
33
+ # Install Python 3.8 and pip
34
+ RUN add-apt-repository ppa:deadsnakes/ppa \
35
+ && apt-get update \
36
+ && apt-get install -y python3.8 python3-pip \
37
+ && apt-get clean;
38
+
39
+ # Set up JAVA_HOME
40
+ ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
41
+ RUN mkdir -p ${HOME} \
42
+ && echo "export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/" >> ${HOME}/.bashrc \
43
+ && chown -R ${NB_UID}:${NB_UID} ${HOME}
44
+
45
+ # Create a new user named "jovyan" with user ID 1000
46
+ RUN useradd -m -u ${NB_UID} ${NB_USER}
47
+
48
+ # Switch to the "jovyan" user
49
+ USER ${NB_USER}
50
+
51
+ # Set home and path variables for the user
52
+ ENV HOME=/home/${NB_USER} \
53
+ PATH=/home/${NB_USER}/.local/bin:$PATH
54
+
55
+ # Set the working directory to the user's home directory
56
+ WORKDIR ${HOME}
57
+
58
+ # Upgrade pip and install Python dependencies
59
+ RUN python3.8 -m pip install --upgrade pip
60
+ COPY requirements.txt /tmp/requirements.txt
61
+ RUN python3.8 -m pip install -r /tmp/requirements.txt
62
+
63
+ # Copy the application code into the container at /home/jovyan
64
+ COPY --chown=${NB_USER}:${NB_USER} . ${HOME}
65
+
66
+ # Expose port for Streamlit
67
+ EXPOSE 7860
68
+
69
+ # Define the entry point for the container
70
+ ENTRYPOINT ["streamlit", "run", "Demo.py", "--server.port=7860", "--server.address=0.0.0.0"]
input/bluetick.jpg ADDED
input/chihuahua.jpg ADDED
input/egyptian_cat.jpeg ADDED
input/hen.JPEG ADDED
input/hippopotamus.JPEG ADDED
input/junco.JPEG ADDED
input/ostrich.JPEG ADDED
input/ox.JPEG ADDED
input/palace.JPEG ADDED
input/tractor.JPEG ADDED
pages/Workflow & Model Overview.py ADDED
@@ -0,0 +1,246 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ # Custom CSS for better styling
4
+ st.markdown("""
5
+ <style>
6
+ .main-title {
7
+ font-size: 36px;
8
+ color: #4A90E2;
9
+ font-weight: bold;
10
+ text-align: center;
11
+ }
12
+ .sub-title {
13
+ font-size: 24px;
14
+ color: #4A90E2;
15
+ margin-top: 20px;
16
+ }
17
+ .section {
18
+ background-color: #f9f9f9;
19
+ padding: 15px;
20
+ border-radius: 10px;
21
+ margin-top: 20px;
22
+ }
23
+ .section h2 {
24
+ font-size: 22px;
25
+ color: #4A90E2;
26
+ }
27
+ .section p, .section ul {
28
+ color: #666666;
29
+ }
30
+ .link {
31
+ color: #4A90E2;
32
+ text-decoration: none;
33
+ }
34
+ .benchmark-table {
35
+ width: 100%;
36
+ border-collapse: collapse;
37
+ margin-top: 20px;
38
+ }
39
+ .benchmark-table th, .benchmark-table td {
40
+ border: 1px solid #ddd;
41
+ padding: 8px;
42
+ text-align: left;
43
+ }
44
+ .benchmark-table th {
45
+ background-color: #4A90E2;
46
+ color: white;
47
+ }
48
+ .benchmark-table td {
49
+ background-color: #f2f2f2;
50
+ }
51
+ </style>
52
+ """, unsafe_allow_html=True)
53
+
54
+ # Main Title
55
+ st.markdown('<div class="main-title">Image Zero Shot Classification with CLIP</div>', unsafe_allow_html=True)
56
+
57
+ # Description
58
+ st.markdown("""
59
+ <div class="section">
60
+ <p><strong>CLIP (Contrastive Language-Image Pre-Training)</strong> is a neural network trained on image and text pairs. It has the capability to classify images without requiring hard-coded labels, making it highly flexible. Labels can be provided during inference, similar to the zero-shot capabilities of GPT-2 and GPT-3 models.</p>
61
+ <p>This model was imported from Hugging Face Transformers: <a class="link" href="https://huggingface.co/openai/clip-vit-base-patch32" target="_blank">CLIP Model on Hugging Face</a></p>
62
+ </div>
63
+ """, unsafe_allow_html=True)
64
+
65
+ # How to Use
66
+ st.markdown('<div class="sub-title">How to Use the Model</div>', unsafe_allow_html=True)
67
+ st.code('''
68
+ import sparknlp
69
+ from sparknlp.base import *
70
+ from sparknlp.annotator import *
71
+ from pyspark.ml import Pipeline
72
+
73
+ # Load image data
74
+ imageDF = spark.read \\
75
+ .format("image") \\
76
+ .option("dropInvalid", value = True) \\
77
+ .load("src/test/resources/image/")
78
+
79
+ # Define Image Assembler
80
+ imageAssembler: ImageAssembler = ImageAssembler() \\
81
+ .setInputCol("image") \\
82
+ .setOutputCol("image_assembler")
83
+
84
+ # Define candidate labels
85
+ candidateLabels = [
86
+ "a photo of a bird",
87
+ "a photo of a cat",
88
+ "a photo of a dog",
89
+ "a photo of a hen",
90
+ "a photo of a hippo",
91
+ "a photo of a room",
92
+ "a photo of a tractor",
93
+ "a photo of an ostrich",
94
+ "a photo of an ox"]
95
+
96
+ # Define CLIP classifier
97
+ imageClassifier = CLIPForZeroShotClassification \\
98
+ .pretrained() \\
99
+ .setInputCols(["image_assembler"]) \\
100
+ .setOutputCol("label") \\
101
+ .setCandidateLabels(candidateLabels)
102
+
103
+ # Create pipeline
104
+ pipeline = Pipeline().setStages([imageAssembler, imageClassifier])
105
+
106
+ # Apply pipeline to image data
107
+ pipelineDF = pipeline.fit(imageDF).transform(imageDF)
108
+
109
+ # Show results
110
+ pipelineDF \\
111
+ .selectExpr("reverse(split(image.origin, '/'))[0] as image_name", "label.result") \\
112
+ .show(truncate=False)
113
+ ''', language='python')
114
+
115
+ # Results
116
+ st.markdown('<div class="sub-title">Results</div>', unsafe_allow_html=True)
117
+ st.markdown("""
118
+ <div class="section">
119
+ <table class="benchmark-table">
120
+ <tr>
121
+ <th>Image Name</th>
122
+ <th>Result</th>
123
+ </tr>
124
+ <tr>
125
+ <td>palace.JPEG</td>
126
+ <td>[a photo of a room]</td>
127
+ </tr>
128
+ <tr>
129
+ <td>egyptian_cat.jpeg</td>
130
+ <td>[a photo of a cat]</td>
131
+ </tr>
132
+ <tr>
133
+ <td>hippopotamus.JPEG</td>
134
+ <td>[a photo of a hippo]</td>
135
+ </tr>
136
+ <tr>
137
+ <td>hen.JPEG</td>
138
+ <td>[a photo of a hen]</td>
139
+ </tr>
140
+ <tr>
141
+ <td>ostrich.JPEG</td>
142
+ <td>[a photo of an ostrich]</td>
143
+ </tr>
144
+ <tr>
145
+ <td>junco.JPEG</td>
146
+ <td>[a photo of a bird]</td>
147
+ </tr>
148
+ <tr>
149
+ <td>bluetick.jpg</td>
150
+ <td>[a photo of a dog]</td>
151
+ </tr>
152
+ <tr>
153
+ <td>chihuahua.jpg</td>
154
+ <td>[a photo of a dog]</td>
155
+ </tr>
156
+ <tr>
157
+ <td>tractor.JPEG</td>
158
+ <td>[a photo of a tractor]</td>
159
+ </tr>
160
+ <tr>
161
+ <td>ox.JPEG</td>
162
+ <td>[a photo of an ox]</td>
163
+ </tr>
164
+ </table>
165
+ </div>
166
+ """, unsafe_allow_html=True)
167
+
168
+ # Model Information
169
+ st.markdown('<div class="sub-title">Model Information</div>', unsafe_allow_html=True)
170
+ st.markdown("""
171
+ <div class="section">
172
+ <table class="benchmark-table">
173
+ <tr>
174
+ <th>Attribute</th>
175
+ <th>Description</th>
176
+ </tr>
177
+ <tr>
178
+ <td><strong>Model Name</strong></td>
179
+ <td>zero_shot_classifier_clip_vit_base_patch32</td>
180
+ </tr>
181
+ <tr>
182
+ <td><strong>Compatibility</strong></td>
183
+ <td>Spark NLP 5.2.0+</td>
184
+ </tr>
185
+ <tr>
186
+ <td><strong>License</strong></td>
187
+ <td>Open Source</td>
188
+ </tr>
189
+ <tr>
190
+ <td><strong>Edition</strong></td>
191
+ <td>Official</td>
192
+ </tr>
193
+ <tr>
194
+ <td><strong>Input Labels</strong></td>
195
+ <td>[image_assembler]</td>
196
+ </tr>
197
+ <tr>
198
+ <td><strong>Output Labels</strong></td>
199
+ <td>[classification]</td>
200
+ </tr>
201
+ <tr>
202
+ <td><strong>Language</strong></td>
203
+ <td>en</td>
204
+ </tr>
205
+ <tr>
206
+ <td><strong>Size</strong></td>
207
+ <td>392.8 MB</td>
208
+ </tr>
209
+ </table>
210
+ </div>
211
+ """, unsafe_allow_html=True)
212
+
213
+ # Data Source Section
214
+ st.markdown('<div class="sub-title">Data Source</div>', unsafe_allow_html=True)
215
+ st.markdown("""
216
+ <div class="section">
217
+ <p>The CLIP model is available on <a class="link" href="https://huggingface.co/openai/clip-vit-base-patch32" target="_blank">Hugging Face</a>. This model was trained on image-text pairs and can be used for zero-shot image classification.</p>
218
+ </div>
219
+ """, unsafe_allow_html=True)
220
+
221
+ # References
222
+ st.markdown('<div class="sub-title">References</div>', unsafe_allow_html=True)
223
+ st.markdown("""
224
+ <div class="section">
225
+ <ul>
226
+ <li><a class="link" href="https://sparknlp.org/2023/12/02/zero_shot_classifier_clip_vit_base_patch32_en.html" target="_blank" rel="noopener">CLIP Model on Spark NLP</a></li>
227
+ <li><a class="link" href="https://huggingface.co/openai/clip-vit-base-patch32" target="_blank" rel="noopener">CLIP Model on Hugging Face</a></li>
228
+ <li><a class="link" href="https://github.com/openai/CLIP" target="_blank" rel="noopener">CLIP GitHub Repository</a></li>
229
+ <li><a class="link" href="https://arxiv.org/abs/2103.00020" target="_blank" rel="noopener">CLIP Paper</a></li>
230
+ </ul>
231
+ </div>
232
+ """, unsafe_allow_html=True)
233
+
234
+ # Community & Support
235
+ st.markdown('<div class="sub-title">Community & Support</div>', unsafe_allow_html=True)
236
+ st.markdown("""
237
+ <div class="section">
238
+ <ul>
239
+ <li><a class="link" href="https://sparknlp.org/" target="_blank">Official Website</a>: Documentation and examples</li>
240
+ <li><a class="link" href="https://join.slack.com/t/spark-nlp/shared_invite/zt-198dipu77-L3UWNe_AJ8xqDk0ivmih5Q" target="_blank">Slack</a>: Live discussion with the community and team</li>
241
+ <li><a class="link" href="https://github.com/JohnSnowLabs/spark-nlp" target="_blank">GitHub</a>: Bug reports, feature requests, and contributions</li>
242
+ <li><a class="link" href="https://medium.com/spark-nlp" target="_blank">Medium</a>: Spark NLP articles</li>
243
+ <li><a class="link" href="https://www.youtube.com/channel/UCmFOjlpYEhxf_wJUDuz6xxQ/videos" target="_blank">YouTube</a>: Video tutorials</li>
244
+ </ul>
245
+ </div>
246
+ """, unsafe_allow_html=True)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ streamlit
2
+ streamlit-tags
3
+ pandas
4
+ numpy
5
+ spark-nlp
6
+ pyspark