importbase64importstreamlitasstfromPILimportImagefrompinferencia.frontend.templates.baseimportBaseTemplatefrompinferencia.frontend.templates.utilsimportdisplay_text_predictionclassSumMnistTemplate(BaseTemplate):title=('<span style="color:salmon;">Sum</span> ''<span style="color:slategray;">MNIST</span> ')defrender(self):super().render()col1,col2=st.columns(2)# (1)withcol1.form("First Image",clear_on_submit=True):first_number=col1.file_uploader("Choose an image...",type=["jpg","png","jpeg"],key="1")withcol2.form("Second Image",clear_on_submit=True):second_number=col2.file_uploader("Choose an image...",type=["jpg","png","jpeg"],key="2")st.markdown("##### Sum of the two digit:")images=[]iffirst_numberisnotNone:# (2)image1=Image.open(first_number)col1.image(image1,use_column_width=True)images.append(base64.b64encode(first_number.getvalue()).decode())ifsecond_numberisnotNone:# (3)image1=Image.open(second_number)col2.image(image1,use_column_width=True)images.append(base64.b64encode(second_number.getvalue()).decode())iffirst_numberandsecond_number:# (4)withst.spinner("Waiting for result"):prediction=self.predict(images)display_text_prediction(prediction,component=st)
Here we split the content panel into two columns, each accepts a single MNIST image.
Once the image is uploaded, append it to the image array for later prediction.
Once the image is uploaded, append it to the image array for later prediction.
If both images are uploaded, send them to backend to predict.
importbase64importstreamlitasstfromPILimportImagefrompinferencia.frontend.appimportServerfrompinferencia.frontend.templates.baseimportBaseTemplatefrompinferencia.frontend.templates.utilsimportdisplay_text_predictionclassSumMnistTemplate(BaseTemplate):title=('<span style="color:salmon;">Sum</span> ''<span style="color:slategray;">MNIST</span> ')defrender(self):super().render()col1,col2=st.columns(2)withcol1.form("First Image",clear_on_submit=True):first_number=col1.file_uploader("Choose an image...",type=["jpg","png","jpeg"],key="1")withcol2.form("Second Image",clear_on_submit=True):second_number=col2.file_uploader("Choose an image...",type=["jpg","png","jpeg"],key="2")st.markdown("##### Sum of the two digits:")images=[]iffirst_numberisnotNone:image1=Image.open(first_number)col1.image(image1,use_column_width=True)images.append(base64.b64encode(first_number.getvalue()).decode())ifsecond_numberisnotNone:image1=Image.open(second_number)col2.image(image1,use_column_width=True)images.append(base64.b64encode(second_number.getvalue()).decode())iffirst_numberandsecond_number:withst.spinner("Waiting for result"):prediction=self.predict(images)display_text_prediction(prediction,component=st)backend_address="http://127.0.0.1:8000"service=Server(backend_server=f"{backend_address}",custom_templates={"Sum Mnist":SumMnistTemplate},)