Spaces:
Sleeping
Sleeping
Delete Demo.py
Browse files
Demo.py
DELETED
@@ -1,121 +0,0 @@
|
|
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 |
-
|
11 |
-
# Page configuration
|
12 |
-
st.set_page_config(
|
13 |
-
layout="wide",
|
14 |
-
page_title="Spark NLP Demos App",
|
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 p, .section ul {
|
28 |
-
color: #666666;
|
29 |
-
}
|
30 |
-
</style>
|
31 |
-
""", unsafe_allow_html=True)
|
32 |
-
|
33 |
-
@st.cache_resource
|
34 |
-
def init_spark():
|
35 |
-
return sparknlp.start()
|
36 |
-
|
37 |
-
@st.cache_resource
|
38 |
-
def create_pipeline():
|
39 |
-
document = DocumentAssembler() \
|
40 |
-
.setInputCol("text") \
|
41 |
-
.setOutputCol("document")
|
42 |
-
|
43 |
-
# Step 2: Sentence Detection
|
44 |
-
sentenceDetector = SentenceDetector() \
|
45 |
-
.setInputCols("document") \
|
46 |
-
.setOutputCol("sentences")
|
47 |
-
|
48 |
-
# Step 3: Tokenization
|
49 |
-
token = Tokenizer() \
|
50 |
-
.setInputCols("sentences") \
|
51 |
-
.setOutputCol("tokens") \
|
52 |
-
.setContextChars(["(", ")", "?", "!", ".", ","])
|
53 |
-
|
54 |
-
# Step 4: Coreference Resolution
|
55 |
-
corefResolution = SpanBertCorefModel().pretrained("spanbert_base_coref") \
|
56 |
-
.setInputCols(["sentences", "tokens"]) \
|
57 |
-
.setOutputCol("corefs") \
|
58 |
-
.setCaseSensitive(False)
|
59 |
-
|
60 |
-
# Define the pipeline
|
61 |
-
pipeline = Pipeline(stages=[document, sentenceDetector, token, corefResolution])
|
62 |
-
|
63 |
-
def fit_data(pipeline, data):
|
64 |
-
empty_df = spark.createDataFrame([['']]).toDF('text')
|
65 |
-
pipeline_model = pipeline.fit(empty_df)
|
66 |
-
model = LightPipeline(pipeline_model)
|
67 |
-
results = model.fullAnnotate(data)
|
68 |
-
return results
|
69 |
-
|
70 |
-
# Set up the page layout
|
71 |
-
st.markdown('<div class="main-title">State-of-the-Art Coreference Resolution in Spark NLP</div>', unsafe_allow_html=True)
|
72 |
-
|
73 |
-
# Sidebar content
|
74 |
-
model_name = st.sidebar.selectbox(
|
75 |
-
"Choose the pretrained model",
|
76 |
-
['spanbert_base_coref'],
|
77 |
-
help="For more info about the models visit: https://sparknlp.org/models"
|
78 |
-
)
|
79 |
-
|
80 |
-
# Reference notebook link in sidebar
|
81 |
-
link = """
|
82 |
-
<a href="https://github.com/JohnSnowLabs/spark-nlp/blob/master/examples/python/annotation/text/english/coreference-resolution/Coreference_Resolution_SpanBertCorefModel.ipynb#L117">
|
83 |
-
<img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/>
|
84 |
-
</a>
|
85 |
-
"""
|
86 |
-
st.sidebar.markdown('Reference notebook:')
|
87 |
-
st.sidebar.markdown(link, unsafe_allow_html=True)
|
88 |
-
|
89 |
-
# Load examples
|
90 |
-
examples = [
|
91 |
-
"Alice went to the market. She bought some fresh vegetables there. The tomatoes she purchased were particularly ripe.",
|
92 |
-
"Dr. Smith is a renowned surgeon. He has performed over a thousand successful operations. His colleagues respect him a lot.",
|
93 |
-
"The company announced a new product launch. It is expected to revolutionize the industry. The CEO was very excited about it.",
|
94 |
-
"Jennifer enjoys hiking. She goes to the mountains every weekend. Her favorite spot is the Blue Ridge Mountains.",
|
95 |
-
"The team won the championship. They celebrated their victory with a huge party. Their coach praised their hard work and dedication.",
|
96 |
-
"Michael is studying computer science. He finds artificial intelligence fascinating. His dream is to work at a leading tech company.",
|
97 |
-
"The book was well-received by critics. It was praised for its intricate plot and well-developed characters. The author felt proud of his work.",
|
98 |
-
"Sarah adopted a kitten. She named it Whiskers. Whiskers loves to play with her and often follows her around the house.",
|
99 |
-
"The project was completed ahead of schedule. It was a collaborative effort. The team members were rewarded for their contribution.",
|
100 |
-
"Tom is a skilled guitarist. He plays in a local band. His performances are always energetic and captivating."
|
101 |
-
]
|
102 |
-
|
103 |
-
# st.subheader("Automatically detect phrases expressing dates and normalize them with respect to a reference date.")
|
104 |
-
selected_text = st.selectbox("Select an example", examples)
|
105 |
-
custom_input = st.text_input("Try it with your own Sentence!")
|
106 |
-
|
107 |
-
text_to_analyze = custom_input if custom_input else selected_text
|
108 |
-
|
109 |
-
st.subheader('Full example text')
|
110 |
-
st.write(text_to_analyze)
|
111 |
-
|
112 |
-
# Initialize Spark and create pipeline
|
113 |
-
spark = init_spark()
|
114 |
-
pipeline = create_pipeline()
|
115 |
-
output = fit_data(pipeline, text_to_analyze)
|
116 |
-
|
117 |
-
# Display matched sentence
|
118 |
-
st.subheader("Processed output:")
|
119 |
-
df = extract_to_dataframe(output)
|
120 |
-
df.index += 1
|
121 |
-
st.dataframe(df)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|