abdullahmubeen10
commited on
Commit
•
6d79a95
1
Parent(s):
96b8e1a
Update Demo.py
Browse files
Demo.py
CHANGED
@@ -1,124 +1,123 @@
|
|
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 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
.
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
.
|
51 |
-
.
|
52 |
-
.
|
53 |
-
.
|
54 |
-
|
55 |
-
|
56 |
-
pipeline
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
annotations_result
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
st.markdown(f'<div class="
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
<
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
st.sidebar.markdown(
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
st.markdown(f'This document has been classified as : **{output}**')
|
|
|
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 |
+
|
11 |
+
# Page configuration
|
12 |
+
st.set_page_config(
|
13 |
+
layout="wide",
|
14 |
+
initial_sidebar_state="auto"
|
15 |
+
)
|
16 |
+
|
17 |
+
# CSS for styling
|
18 |
+
st.markdown("""
|
19 |
+
<style>
|
20 |
+
.main-title {
|
21 |
+
font-size: 36px;
|
22 |
+
color: #4A90E2;
|
23 |
+
font-weight: bold;
|
24 |
+
text-align: center;
|
25 |
+
}
|
26 |
+
.section {
|
27 |
+
background-color: #f9f9f9;
|
28 |
+
padding: 10px;
|
29 |
+
border-radius: 10px;
|
30 |
+
margin-top: 10px;
|
31 |
+
}
|
32 |
+
.section p, .section ul {
|
33 |
+
color: #666666;
|
34 |
+
}
|
35 |
+
</style>
|
36 |
+
""", unsafe_allow_html=True)
|
37 |
+
|
38 |
+
@st.cache_resource
|
39 |
+
def init_spark():
|
40 |
+
return sparknlp.start()
|
41 |
+
|
42 |
+
@st.cache_resource
|
43 |
+
def create_pipeline(model):
|
44 |
+
imageAssembler = ImageAssembler() \
|
45 |
+
.setInputCol("image") \
|
46 |
+
.setOutputCol("image_assembler")
|
47 |
+
|
48 |
+
imageCaptioning = VisionEncoderDecoderForImageCaptioning \
|
49 |
+
.pretrained("image_captioning_vit_gpt2") \
|
50 |
+
.setBeamSize(2) \
|
51 |
+
.setDoSample(False) \
|
52 |
+
.setInputCols(["image_assembler"]) \
|
53 |
+
.setOutputCol("caption")
|
54 |
+
|
55 |
+
pipeline = Pipeline(stages=[imageAssembler, imageCaptioning])
|
56 |
+
return pipeline
|
57 |
+
|
58 |
+
def fit_data(pipeline, data):
|
59 |
+
empty_df = spark.createDataFrame([['']]).toDF('text')
|
60 |
+
model = pipeline.fit(empty_df)
|
61 |
+
light_pipeline = LightPipeline(model)
|
62 |
+
annotations_result = light_pipeline.fullAnnotateImage(data)
|
63 |
+
return annotations_result[0]['caption'][0].result
|
64 |
+
|
65 |
+
def save_uploadedfile(uploadedfile):
|
66 |
+
filepath = os.path.join(IMAGE_FILE_PATH, uploadedfile.name)
|
67 |
+
with open(filepath, "wb") as f:
|
68 |
+
if hasattr(uploadedfile, 'getbuffer'):
|
69 |
+
f.write(uploadedfile.getbuffer())
|
70 |
+
else:
|
71 |
+
f.write(uploadedfile.read())
|
72 |
+
|
73 |
+
# Sidebar content
|
74 |
+
model_list = ['image_captioning_vit_gpt2']
|
75 |
+
model = st.sidebar.selectbox(
|
76 |
+
"Choose the pretrained model",
|
77 |
+
model_list,
|
78 |
+
help="For more info about the models visit: https://sparknlp.org/models"
|
79 |
+
)
|
80 |
+
|
81 |
+
# Set up the page layout
|
82 |
+
st.markdown(f'<div class="main-title">VisionEncoderDecoder For Image Captioning</div>', unsafe_allow_html=True)
|
83 |
+
# st.markdown(f'<div class="section"><p>{sub_title}</p></div>', unsafe_allow_html=True)
|
84 |
+
|
85 |
+
# Reference notebook link in sidebar
|
86 |
+
link = """
|
87 |
+
<a href="https://colab.research.google.com/github/JohnSnowLabs/spark-nlp/blob/master/examples/python/annotation/image/VisionEncoderDecoderForImageCaptioning.ipynb">
|
88 |
+
<img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/>
|
89 |
+
</a>
|
90 |
+
"""
|
91 |
+
st.sidebar.markdown('Reference notebook:')
|
92 |
+
st.sidebar.markdown(link, unsafe_allow_html=True)
|
93 |
+
|
94 |
+
# Load examples
|
95 |
+
IMAGE_FILE_PATH = f"inputs"
|
96 |
+
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'])
|
97 |
+
|
98 |
+
img_options = st.selectbox("Select an image", image_files)
|
99 |
+
uploadedfile = st.file_uploader("Try it for yourself!")
|
100 |
+
|
101 |
+
if uploadedfile:
|
102 |
+
file_details = {"FileName":uploadedfile.name,"FileType":uploadedfile.type}
|
103 |
+
save_uploadedfile(uploadedfile)
|
104 |
+
selected_image = f"{IMAGE_FILE_PATH}/{uploadedfile.name}"
|
105 |
+
elif img_options:
|
106 |
+
selected_image = f"{IMAGE_FILE_PATH}/{img_options}"
|
107 |
+
|
108 |
+
st.subheader('Classified Image')
|
109 |
+
|
110 |
+
image_size = st.slider('Image Size', 400, 1000, value=400, step = 100)
|
111 |
+
|
112 |
+
try:
|
113 |
+
st.image(f"{IMAGE_FILE_PATH}/{selected_image}", width=image_size)
|
114 |
+
except:
|
115 |
+
st.image(selected_image, width=image_size)
|
116 |
+
|
117 |
+
st.subheader('Classification')
|
118 |
+
|
119 |
+
spark = init_spark()
|
120 |
+
Pipeline = create_pipeline(model)
|
121 |
+
output = fit_data(Pipeline, selected_image)
|
122 |
+
|
|
|
123 |
st.markdown(f'This document has been classified as : **{output}**')
|