File size: 3,576 Bytes
5da5ab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import streamlit as st
import sparknlp

from sparknlp.base import *
from sparknlp.annotator import *
from pyspark.ml import Pipeline

# Page configuration
st.set_page_config(
    layout="wide", 
    initial_sidebar_state="auto"
)

# CSS for styling
st.markdown("""

    <style>

        .main-title {

            font-size: 36px;

            color: #4A90E2;

            font-weight: bold;

            text-align: center;

        }

        .section {

            background-color: #f9f9f9;

            padding: 10px;

            border-radius: 10px;

            margin-top: 10px;

        }

        .section p, .section ul {

            color: #666666;

        }

    </style>

""", unsafe_allow_html=True)

@st.cache_resource
def init_spark():
    return sparknlp.start()

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

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

  t5 = T5Transformer()\
    .pretrained("google_t5_small_ssm_nq")\
    .setInputCols(["questions"])\
    .setOutputCol("answers")\

  pipeline = Pipeline().setStages([document_assembler, sentence_detector, t5])
  return pipeline

def fit_data(pipeline, data):
    df = spark.createDataFrame([[data]]).toDF("text")
    result = pipeline.fit(df).transform(df)
    return result.select('answers.result').collect()

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

# Set up the page layout
title, sub_title = (
    'Automatically Answer Questions (CLOSED BOOK)',
    'Automatically generate answers to questions without context.'
)

st.markdown(f'<div class="main-title">{title}</div>', unsafe_allow_html=True)
st.write(sub_title)

# Reference notebook link in sidebar
link = """

<a href="https://colab.research.google.com/github/JohnSnowLabs/spark-nlp-workshop/blob/master/tutorials/streamlit_notebooks/public/QUESTION_ANSWERING_CLOSED_BOOK.ipynb#scrollTo=LEW2ZjZj7T1Q">

    <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)

# Load examples
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?"
]

selected_text = st.selectbox("Select an example", examples)
custom_input = st.text_input("Try it with your own Sentence!")

text_to_analyze = custom_input if custom_input else selected_text

st.write('Question:')
HTML_WRAPPER = """<div class="scroll entities" style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem; white-space:pre-wrap">{}</div>"""
st.markdown(HTML_WRAPPER.format(text_to_analyze), unsafe_allow_html=True)

# Initialize Spark and create pipeline
spark = init_spark()
pipeline = create_pipeline(model)
output = fit_data(pipeline, text_to_analyze)

# Display matched sentence
st.write("Answer:")

output_text = "".join(output[0][0])
st.markdown(HTML_WRAPPER.format(output_text.title()), unsafe_allow_html=True)