abdullahmubeen10 commited on
Commit
377aeec
1 Parent(s): 6b6b259

Update Demo.py

Browse files
Files changed (1) hide show
  1. Demo.py +121 -121
Demo.py CHANGED
@@ -1,122 +1,122 @@
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
- imageClassifier = ConvNextForImageClassification \
50
- .pretrained("image_classifier_convnext_tiny_224_local", "en") \
51
- .setInputCols(["image_assembler"]) \
52
- .setOutputCol("class")
53
-
54
- pipeline = Pipeline(stages=[image_assembler, imageClassifier])
55
- return pipeline
56
-
57
- def fit_data(pipeline, data):
58
- empty_df = spark.createDataFrame([['']]).toDF('text')
59
- model = pipeline.fit(empty_df)
60
- light_pipeline = LightPipeline(model)
61
- annotations_result = light_pipeline.fullAnnotateImage(data)
62
- return annotations_result[0]['class'][0].result
63
-
64
- def save_uploadedfile(uploadedfile):
65
- filepath = os.path.join(IMAGE_FILE_PATH, uploadedfile.name)
66
- with open(filepath, "wb") as f:
67
- if hasattr(uploadedfile, 'getbuffer'):
68
- f.write(uploadedfile.getbuffer())
69
- else:
70
- f.write(uploadedfile.read())
71
-
72
- # Sidebar content
73
- model_list = ['image_classifier_convnext_tiny_224_local']
74
- model = st.sidebar.selectbox(
75
- "Choose the pretrained model",
76
- model_list,
77
- help="For more info about the models visit: https://sparknlp.org/models"
78
- )
79
-
80
- # Set up the page layout
81
- st.markdown(f'<div class="main-title">ConvNext For Image Classification</div>', unsafe_allow_html=True)
82
- # st.markdown(f'<div class="section"><p>{sub_title}</p></div>', unsafe_allow_html=True)
83
-
84
- # Reference notebook link in sidebar
85
- link = """
86
- <a href="https://github.com/JohnSnowLabs/spark-nlp/blob/master/examples/python/annotation/image/ConvNextForImageClassification.ipynb">
87
- <img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/>
88
- </a>
89
- """
90
- st.sidebar.markdown('Reference notebook:')
91
- st.sidebar.markdown(link, unsafe_allow_html=True)
92
-
93
- # Load examples
94
- IMAGE_FILE_PATH = f"inputs"
95
- 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'])
96
-
97
- img_options = st.selectbox("Select an image", image_files)
98
- uploadedfile = st.file_uploader("Try it for yourself!")
99
-
100
- if uploadedfile:
101
- file_details = {"FileName":uploadedfile.name,"FileType":uploadedfile.type}
102
- save_uploadedfile(uploadedfile)
103
- selected_image = f"{IMAGE_FILE_PATH}/{uploadedfile.name}"
104
- elif img_options:
105
- selected_image = f"{IMAGE_FILE_PATH}/{img_options}"
106
-
107
- st.subheader('Classified Image')
108
-
109
- image_size = st.slider('Image Size', 400, 1000, value=400, step = 100)
110
-
111
- try:
112
- st.image(f"{IMAGE_FILE_PATH}/{selected_image}", width=image_size)
113
- except:
114
- st.image(selected_image, width=image_size)
115
-
116
- st.subheader('Classification')
117
-
118
- spark = init_spark()
119
- Pipeline = create_pipeline(model)
120
- output = fit_data(Pipeline, selected_image)
121
-
122
  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
+ 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
+ imageClassifier = ConvNextForImageClassification \
50
+ .pretrained("image_classifier_convnext_tiny_224_local", "en") \
51
+ .setInputCols(["image_assembler"]) \
52
+ .setOutputCol("class")
53
+
54
+ pipeline = Pipeline(stages=[imageAssembler, imageClassifier])
55
+ return pipeline
56
+
57
+ def fit_data(pipeline, data):
58
+ empty_df = spark.createDataFrame([['']]).toDF('text')
59
+ model = pipeline.fit(empty_df)
60
+ light_pipeline = LightPipeline(model)
61
+ annotations_result = light_pipeline.fullAnnotateImage(data)
62
+ return annotations_result[0]['class'][0].result
63
+
64
+ def save_uploadedfile(uploadedfile):
65
+ filepath = os.path.join(IMAGE_FILE_PATH, uploadedfile.name)
66
+ with open(filepath, "wb") as f:
67
+ if hasattr(uploadedfile, 'getbuffer'):
68
+ f.write(uploadedfile.getbuffer())
69
+ else:
70
+ f.write(uploadedfile.read())
71
+
72
+ # Sidebar content
73
+ model_list = ['image_classifier_convnext_tiny_224_local']
74
+ model = st.sidebar.selectbox(
75
+ "Choose the pretrained model",
76
+ model_list,
77
+ help="For more info about the models visit: https://sparknlp.org/models"
78
+ )
79
+
80
+ # Set up the page layout
81
+ st.markdown(f'<div class="main-title">ConvNext For Image Classification</div>', unsafe_allow_html=True)
82
+ # st.markdown(f'<div class="section"><p>{sub_title}</p></div>', unsafe_allow_html=True)
83
+
84
+ # Reference notebook link in sidebar
85
+ link = """
86
+ <a href="https://github.com/JohnSnowLabs/spark-nlp/blob/master/examples/python/annotation/image/ConvNextForImageClassification.ipynb">
87
+ <img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/>
88
+ </a>
89
+ """
90
+ st.sidebar.markdown('Reference notebook:')
91
+ st.sidebar.markdown(link, unsafe_allow_html=True)
92
+
93
+ # Load examples
94
+ IMAGE_FILE_PATH = f"inputs"
95
+ 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'])
96
+
97
+ img_options = st.selectbox("Select an image", image_files)
98
+ uploadedfile = st.file_uploader("Try it for yourself!")
99
+
100
+ if uploadedfile:
101
+ file_details = {"FileName":uploadedfile.name,"FileType":uploadedfile.type}
102
+ save_uploadedfile(uploadedfile)
103
+ selected_image = f"{IMAGE_FILE_PATH}/{uploadedfile.name}"
104
+ elif img_options:
105
+ selected_image = f"{IMAGE_FILE_PATH}/{img_options}"
106
+
107
+ st.subheader('Classified Image')
108
+
109
+ image_size = st.slider('Image Size', 400, 1000, value=400, step = 100)
110
+
111
+ try:
112
+ st.image(f"{IMAGE_FILE_PATH}/{selected_image}", width=image_size)
113
+ except:
114
+ st.image(selected_image, width=image_size)
115
+
116
+ st.subheader('Classification')
117
+
118
+ spark = init_spark()
119
+ Pipeline = create_pipeline(model)
120
+ output = fit_data(Pipeline, selected_image)
121
+
122
  st.markdown(f'This document has been classified as : **{output}**')