import streamlit as st import sparknlp import os import pandas as pd from sparknlp.base import * from sparknlp.annotator import * from pyspark.ml import Pipeline from sparknlp.pretrained import PretrainedPipeline from streamlit_tags import st_tags # Page configuration st.set_page_config( layout="wide", initial_sidebar_state="auto" ) # CSS for styling st.markdown(""" """, unsafe_allow_html=True) @st.cache_resource def init_spark(): return sparknlp.start() @st.cache_resource def create_pipeline(model): image_assembler = ImageAssembler()\ .setInputCol("image")\ .setOutputCol("image_assembler") imageClassifier = SwinForImageClassification.pretrained()\ .setInputCols("image_assembler")\ .setOutputCol("class") pipeline = Pipeline(stages=[image_assembler, imageClassifier]) return pipeline def fit_data(pipeline, data): empty_df = spark.createDataFrame([['']]).toDF('text') model = pipeline.fit(empty_df) light_pipeline = LightPipeline(model) annotations_result = light_pipeline.fullAnnotateImage(data) return annotations_result[0]['class'][0].result def save_uploadedfile(uploadedfile): filepath = os.path.join(IMAGE_FILE_PATH, uploadedfile.name) with open(filepath, "wb") as f: if hasattr(uploadedfile, 'getbuffer'): f.write(uploadedfile.getbuffer()) else: f.write(uploadedfile.read()) # Sidebar content model_list = ['image_classifier_swin_base_patch4_window7_224 ', 'image_classifier_swin_base_patch4_window12_384_in22k'] model = st.sidebar.selectbox( "Choose the pretrained model", model_list, help="For more info about the models visit: https://sparknlp.org/models" ) # Set up the page layout st.markdown(f'
{sub_title}