abdullahmubeen10 commited on
Commit
6d79a95
1 Parent(s): 96b8e1a

Update Demo.py

Browse files
Files changed (1) hide show
  1. Demo.py +122 -123
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
- 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):
45
- imageAssembler = ImageAssembler() \
46
- .setInputCol("image") \
47
- .setOutputCol("image_assembler")
48
-
49
- imageCaptioning = VisionEncoderDecoderForImageCaptioning \
50
- .pretrained("image_captioning_vit_gpt2") \
51
- .setBeamSize(2) \
52
- .setDoSample(False) \
53
- .setInputCols(["image_assembler"]) \
54
- .setOutputCol("caption")
55
-
56
- pipeline = Pipeline(stages=[imageAssembler, imageCaptioning])
57
- return pipeline
58
-
59
- def fit_data(pipeline, data):
60
- empty_df = spark.createDataFrame([['']]).toDF('text')
61
- model = pipeline.fit(empty_df)
62
- light_pipeline = LightPipeline(model)
63
- annotations_result = light_pipeline.fullAnnotateImage(data)
64
- return annotations_result[0]['class'][0].result
65
-
66
- def save_uploadedfile(uploadedfile):
67
- filepath = os.path.join(IMAGE_FILE_PATH, uploadedfile.name)
68
- with open(filepath, "wb") as f:
69
- if hasattr(uploadedfile, 'getbuffer'):
70
- f.write(uploadedfile.getbuffer())
71
- else:
72
- f.write(uploadedfile.read())
73
-
74
- # Sidebar content
75
- model_list = ['image_captioning_vit_gpt2']
76
- model = st.sidebar.selectbox(
77
- "Choose the pretrained model",
78
- model_list,
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">VisionEncoderDecoder For Image Captioning</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://colab.research.google.com/github/JohnSnowLabs/spark-nlp/blob/master/examples/python/annotation/image/VisionEncoderDecoderForImageCaptioning.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 = f"inputs"
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
- st.subheader('Classified Image')
110
-
111
- image_size = st.slider('Image Size', 400, 1000, value=400, step = 100)
112
-
113
- try:
114
- st.image(f"{IMAGE_FILE_PATH}/{selected_image}", width=image_size)
115
- except:
116
- st.image(selected_image, width=image_size)
117
-
118
- st.subheader('Classification')
119
-
120
- spark = init_spark()
121
- Pipeline = create_pipeline(model)
122
- output = fit_data(Pipeline, selected_image)
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}**')