Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uploading example/demos #14175

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
streamlit
spark-nlp
pyspark
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
############ IMPORTING LIBRARIES ############

# Import streamlit, Spark NLP, pyspark

import streamlit as st
import sparknlp
import os

from sparknlp.base import *
from sparknlp.common import *
from sparknlp.annotator import *
from pyspark.ml import Pipeline
from sparknlp.pretrained import PretrainedPipeline

spark = sparknlp.start()

def create_pipeline(model, labels):

image_assembler = ImageAssembler() \
.setInputCol("image") \
.setOutputCol("image_assembler")

imageClassifier = CLIPForZeroShotClassification \
.pretrained() \
.setInputCols(["image_assembler"]) \
.setOutputCol("label") \
.setCandidateLabels(labels)

pipeline = Pipeline(stages=[
image_assembler,
imageClassifier,
])

return pipeline

def fit_data(pipeline, data):

model = pipeline.fit(data)
light_pipeline = LightPipeline(model)
annotations_result = light_pipeline.fullAnnotateImage(data)
pharsed_result = (lambda data: data[data.find("a photo of a"):].split(",")[0] if "a photo of a" in data else "Not found")(str(annotations_result[0]['label']))
return pharsed_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())

############ SETTING UP THE PAGE LAYOUT ############
jsl_logo_path = next(f'{root}/{name}' for root, dirs, files in os.walk('/content') for name in files if name == 'jsl_logo_icon.png')

st.set_page_config(
layout="wide", page_title="CLIPForZeroShotClassification for Zero Shot Classification", page_icon=jsl_logo_path, initial_sidebar_state="auto"
)

st.caption("")
st.title("CLIPForZeroShotClassification for Zero Shot Classification")

if not "valid_inputs_received" in st.session_state:
st.session_state["valid_inputs_received"] = False

### SIDEBAR CONTENT ###

st.sidebar.write("")

logo_path = next(f'{root}/{name}' for root, dirs, files in os.walk('/content') for name in files if name == 'logo.png')
logo = st.sidebar.image(logo_path, width=300)
model = st.sidebar.selectbox("Choose the pretrained model", ['CLIPForZeroShotClassification'], help="For more info about the models visit: https://sparknlp.org/models",)

# # Let's add the colab link for the notebook.

link = """<a href="https://github.com/JohnSnowLabs/spark-nlp/blob/master/examples/python/annotation/image/CLIPForZeroShotClassification.ipynb"><img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/></a>"""
st.sidebar.title('')
st.sidebar.markdown('Reference notebook:')
st.sidebar.markdown(link, unsafe_allow_html=True)

### MAIN CONTENT ###

IMAGE_FILE_PATH = "/content/CLIPForZeroShotClassification/input"
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'])

st.subheader("Summarize text to make it shorter while retaining meaning.")

img_options = st.selectbox("Select an image", image_files)
uploadedfile = st.file_uploader("Try it for yourself!")

if uploadedfile:
file_details = {"FileName":uploadedfile.name,"FileType":uploadedfile.type}
save_uploadedfile(uploadedfile)
selected_image = f"{IMAGE_FILE_PATH}/{uploadedfile.name}"
elif img_options:
selected_image = f"{IMAGE_FILE_PATH}/{img_options}"

candidateLabels = [
"a photo of a bird",
"a photo of a cat",
"a photo of a dog",
"a photo of a hen",
"a photo of a hippo",
"a photo of a room",
"a photo of a tractor",
"a photo of an ostrich",
"a photo of an ox"]

lables = st.multiselect("Select labels", candidateLabels, default=candidateLabels)

st.subheader('Classified Image')

image_size = st.slider('Image Size', 400, 1000, value=400, step = 100)

try:
st.image(f"{IMAGE_FILE_PATH}/{selected_image}", width=image_size)
except:
st.image(selected_image, width=image_size)

st.subheader('Classification')

Pipeline = create_pipeline(model, lables)
output = fit_data(Pipeline, selected_image)

st.markdown(f'This document has been classified as : **{output}**')
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
streamlit
spark-nlp
pyspark
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
############ IMPORTING LIBRARIES ############

# Import streamlit, Spark NLP, pyspark

import streamlit as st
import sparknlp
import os
import pandas as pd
import numpy as np

from sparknlp.base import *
from sparknlp.common import *
from sparknlp.annotator import *
from pyspark.ml import Pipeline
from sparknlp.pretrained import PretrainedPipeline

spark = sparknlp.start()

def create_pipeline(model):
document_assembler = DocumentAssembler()\
.setInputCol("text")\
.setOutputCol("documents")

sentence_detector = SentenceDetectorDLModel\
.pretrained("sentence_detector_dl", "en")\
.setInputCols(["documents"])\
.setOutputCol("questions")

t5 = T5Transformer()\
.pretrained("google_t5_small_ssm_nq")\
.setTask('trivia question:')\
.setInputCols(["questions"])\
.setOutputCol("answers")

qa_pp = Pipeline(stages=[document_assembler, sentence_detector, t5])

return qa_pp

def fit_data(pipeline, data):
empty_df = spark.createDataFrame([['']]).toDF('text')
pipeline_model = pipeline.fit(empty_df)
model = LightPipeline(pipeline_model)
results = model.fullAnnotate(data)[0]

return results['answers'][0].result

############ SETTING UP THE PAGE LAYOUT ############
jsl_logo_path = next(f'{root}/{name}' for root, dirs, files in os.walk('/content') for name in files if name == 'jsl_logo_icon.png')

st.set_page_config(
layout="wide", page_title="Automatically Answer Questions", page_icon=jsl_logo_path, initial_sidebar_state="auto"
)

st.title("Automatically Answer Questions (Closed Book)")

if not "valid_inputs_received" in st.session_state:
st.session_state["valid_inputs_received"] = False

### SIDEBAR CONTENT ###

st.sidebar.write("")

logo_path = next(f'{root}/{name}' for root, dirs, files in os.walk('/content') for name in files if name == 'logo.png')
logo = st.sidebar.image(logo_path, width=300)

model_list = ["google_t5_small_ssm_nq"]
model = st.sidebar.selectbox("Choose the pretrained model", model_list, help="For more info about the models visit: https://sparknlp.org/models",)

# # Let's add the colab link for the notebook.

link= """<a href="https://colab.research.google.com/github/JohnSnowLabs/spark-nlp-workshop/blob/master/tutorials/streamlit_notebooks/public/QUESTION_ANSWERING_CLOSED_BOOK.ipynb"><img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/></a>"""
st.sidebar.title('')
st.sidebar.markdown('Reference notebook:')
st.sidebar.markdown(link, unsafe_allow_html=True)

### MAIN CONTENT ###

examples = [
"Who is Clark Kent?",
"Which is the capital of Bulgaria ?",
"Which country tops the annual global democracy index compiled by the economist intelligence unit?",
"In which city is the Eiffel Tower located?",
"Who is the founder of Microsoft?"
]

st.subheader("Automatically generate answers to questions without context")

selected_text = st.selectbox('Select an Example:', examples)
custom_input = st.text_input('Try it yourself!')

if custom_input:
selected_text = custom_input
elif selected_text:
selected_text = selected_text

st.subheader('Selected Text')
st.write(selected_text)

Pipeline = create_pipeline(model)
output = fit_data(Pipeline, selected_text)

st.subheader('Prediction')
st.write(output)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
streamlit
spark-nlp
pyspark
104 changes: 104 additions & 0 deletions examples/demos/streamlit/Question Answering Open Book/streamlit_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
############ IMPORTING LIBRARIES ############

# Import streamlit, Spark NLP, pyspark

import streamlit as st
import sparknlp
import os
import pandas as pd
import numpy as np

from sparknlp.base import *
from sparknlp.common import *
from sparknlp.annotator import *
from pyspark.ml import Pipeline
from sparknlp.pretrained import PretrainedPipeline

spark = sparknlp.start()

def create_pipeline(model):
document_assembler = DocumentAssembler()\
.setInputCol("text")\
.setOutputCol("documents")

t5 = T5Transformer() \
.pretrained(model) \
.setTask("question:")\
.setMaxOutputLength(200)\
.setInputCols(["documents"]) \
.setOutputCol("answers")

t5_pp = Pipeline(stages=[ document_assembler,
t5])
return t5_pp

def fit_data(pipeline, data):
empty_df = spark.createDataFrame([['']]).toDF('text')
pipeline_model = pipeline.fit(empty_df)
model = LightPipeline(pipeline_model)
results = model.fullAnnotate(data)[0]

return results['answers'][0].result

############ SETTING UP THE PAGE LAYOUT ############
jsl_logo_path = next(f'{root}/{name}' for root, dirs, files in os.walk('/content') for name in files if name == 'jsl_logo_icon.png')

st.set_page_config(
layout="wide", page_title="Automatically Answer Questions", page_icon=jsl_logo_path, initial_sidebar_state="auto"
)

st.title("Automatically Answer Questions (Open Book)")

if not "valid_inputs_received" in st.session_state:
st.session_state["valid_inputs_received"] = False

### SIDEBAR CONTENT ###

st.sidebar.write("")

logo_path = next(f'{root}/{name}' for root, dirs, files in os.walk('/content') for name in files if name == 'logo.png')
logo = st.sidebar.image(logo_path, width=300)

model_list = ["t5_small"]
model = st.sidebar.selectbox("Choose the pretrained model", model_list, help="For more info about the models visit: https://sparknlp.org/models",)

# # Let's add the colab link for the notebook.

link= """<a href="https://colab.research.google.com/github/JohnSnowLabs/spark-nlp-workshop/blob/master/tutorials/streamlit_notebooks/public/QUESTION_ANSWERING_OPEN_BOOK.ipynb"><img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/></a>"""
st.sidebar.markdown('Reference notebook:')
st.sidebar.markdown(link, unsafe_allow_html=True)

### MAIN CONTENT ###

examples = {
"""What does increased oxygen concentrations in the patient’s lungs displace?""": """Hyperbaric (high-pressure) medicine uses special oxygen chambers to increase the partial pressure of O 2 around the patient and, when needed, the medical staff. Carbon monoxide poisoning, gas gangrene, and decompression sickness (the ’bends’) are sometimes treated using these devices. Increased O 2 concentration in the lungs helps to displace carbon monoxide from the heme group of hemoglobin. Oxygen gas is poisonous to the anaerobic bacteria that cause gas gangrene, so increasing its partial pressure helps kill them. Decompression sickness occurs in divers who decompress too quickly after a dive, resulting in bubbles of inert gas, mostly nitrogen and helium, forming in their blood. Increasing the pressure of O 2 as soon as possible is part of the treatment.""",
"""What category of game is Legend of Zelda: Twilight Princess?""": """The Legend of Zelda: Twilight Princess (Japanese: ゼルダの伝説 トワイライトプリンセス, Hepburn: Zeruda no Densetsu: Towairaito Purinsesu?) is an action-adventure game developed and published by Nintendo for the GameCube and Wii home video game consoles. It is the thirteenth installment in the The Legend of Zelda series. Originally planned for release on the GameCube in November 2005, Twilight Princess was delayed by Nintendo to allow its developers to refine the game, add more content, and port it to the Wii. The Wii version was released alongside the console in North America in November 2006, and in Japan, Europe, and Australia the following month. The GameCube version was released worldwide in December 2006.""",
"""Who is founder of Alibaba Group?""": """Alibaba Group founder Jack Ma has made his first appearance since Chinese regulators cracked down on his business empire. His absence had fuelled speculation over his whereabouts amid increasing official scrutiny of his businesses. The billionaire met 100 rural teachers in China via a video meeting on Wednesday, according to local government media. Alibaba shares surged 5% on Hong Kong's stock exchange on the news.""",
"""For what instrument did Frédéric write primarily for?""": """Frédéric François Chopin (/ˈʃoʊpæn/; French pronunciation: ​[fʁe.de.ʁik fʁɑ̃.swa ʃɔ.pɛ̃]; 22 February or 1 March 1810 – 17 October 1849), born Fryderyk Franciszek Chopin,[n 1] was a Polish and French (by citizenship and birth of father) composer and a virtuoso pianist of the Romantic era, who wrote primarily for the solo piano. He gained and has maintained renown worldwide as one of the leading musicians of his era, whose "poetic genius was based on a professional technique that was without equal in his generation." Chopin was born in what was then the Duchy of Warsaw, and grew up in Warsaw, which after 1815 became part of Congress Poland. A child prodigy, he completed his musical education and composed his earlier works in Warsaw before leaving Poland at the age of 20, less than a month before the outbreak of the November 1830 Uprising.""",
"""The most populated city in the United States is which city?""": """New York—often called New York City or the City of New York to distinguish it from the State of New York, of which it is a part—is the most populous city in the United States and the center of the New York metropolitan area, the premier gateway for legal immigration to the United States and one of the most populous urban agglomerations in the world. A global power city, New York exerts a significant impact upon commerce, finance, media, art, fashion, research, technology, education, and entertainment, its fast pace defining the term New York minute. Home to the headquarters of the United Nations, New York is an important center for international diplomacy and has been described as the cultural and financial capital of the world."""
}

selected_text = st.selectbox('Select an Example:', list(examples.keys()))
st.subheader('Try it yourself!')
custom_input_question = st.text_input('Create a question')
custom_input_context = st.text_input("Create it's context")

custom_examples = {}

st.subheader('Selected Text')

if custom_input_question and custom_input_context:
custom_examples[custom_input_question] = custom_input_context
selected_text = [f'"""{next(iter(custom_examples))}""" """context:{custom_examples[next(iter(custom_examples))]}"""']
st.markdown(f"**Text:** {custom_input_question}")
st.markdown(f"**Context:** {custom_input_context}")
elif selected_text:
st.markdown(f"**Text:** {selected_text}")
st.markdown(f"**Context:** {examples[selected_text]}")
selected_text = [f'"""{selected_text}""" """context:{examples[selected_text]}"""']

Pipeline = create_pipeline(model)
output = fit_data(Pipeline, selected_text)

st.subheader('Prediction')
st.write(output)
Loading