Detect-Restaurant-Terminology / pages /Workflow & Model Overview.py
abdullahmubeen10's picture
Upload 10 files
e79dcee verified
import streamlit as st
# Custom CSS for better styling
st.markdown("""
<style>
.main-title {
font-size: 36px;
color: #4A90E2;
font-weight: bold;
text-align: center;
}
.sub-title {
font-size: 24px;
color: #4A90E2;
margin-top: 20px;
}
.section {
background-color: #f9f9f9;
padding: 15px;
border-radius: 10px;
margin-top: 20px;
}
.section h2 {
font-size: 22px;
color: #4A90E2;
}
.section p, .section ul {
color: #666666;
}
.link {
color: #4A90E2;
text-decoration: none;
}
.benchmark-table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
.benchmark-table th, .benchmark-table td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
.benchmark-table th {
background-color: #4A90E2;
color: white;
}
.benchmark-table td {
background-color: #f2f2f2;
}
</style>
""", unsafe_allow_html=True)
# Main Title
st.markdown('<div class="main-title">Detect Restaurant-related Terminology</div>', unsafe_allow_html=True)
# Description
st.markdown("""
<div class="section">
<p>This app utilizes the <strong>nerdl_restaurant_100d</strong> model, which is trained with GloVe 100d embeddings to detect restaurant-related terminology. The model is tailored specifically for identifying various aspects related to restaurants, such as locations, cuisines, and dish names.</p>
</div>
""", unsafe_allow_html=True)
# What is Entity Recognition
st.markdown('<div class="sub-title">What is Entity Recognition?</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<p><strong>Entity Recognition</strong> is a task in Natural Language Processing (NLP) that involves identifying and classifying named entities in text into predefined categories. This model focuses on detecting terminology related to restaurants, which is essential for understanding and analyzing restaurant reviews, menus, and related content.</p>
</div>
""", unsafe_allow_html=True)
# Model Importance and Applications
st.markdown('<div class="sub-title">Model Importance and Applications</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<p>The <strong>nerdl_restaurant_100d</strong> model is highly effective for extracting restaurant-related terminology from text. Its applications include:</p>
<ul>
<li><strong>Menu Analysis:</strong> Identify and categorize different dishes, cuisines, and restaurant names from menus.</li>
<li><strong>Review Aggregation:</strong> Extract and analyze restaurant-related terms from reviews to understand customer preferences.</li>
<li><strong>Restaurant Recommendations:</strong> Enhance recommendation systems by identifying key terms related to restaurants and their attributes.</li>
<li><strong>Data Enrichment:</strong> Improve databases and knowledge graphs by extracting restaurant-specific information from various texts.</li>
</ul>
<p>Why use the <strong>nerdl_restaurant_100d</strong> model?</p>
<ul>
<li><strong>Pre-trained on Restaurant Data:</strong> The model is specifically trained on data related to restaurants, making it ideal for restaurant-related tasks.</li>
<li><strong>High Accuracy:</strong> Achieves high precision in detecting restaurant-related terminology.</li>
<li><strong>Ease of Use:</strong> Provides a straightforward solution for detecting and classifying restaurant-related terms with minimal setup.</li>
</ul>
</div>
""", unsafe_allow_html=True)
# Predicted Entities
st.markdown('<div class="sub-title">Predicted Entities</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<p>The model identifies and classifies the following restaurant-related terms:</p>
<p><code class="language-plaintext highlighter-rouge">Location</code>, <code class="language-plaintext highlighter-rouge">Cuisine</code>, <code class="language-plaintext highlighter-rouge">Amenity</code>, <code class="language-plaintext highlighter-rouge">Restaurant_Name</code>, <code class="language-plaintext highlighter-rouge">Dish</code>, <code class="language-plaintext highlighter-rouge">Rating</code>, <code class="language-plaintext highlighter-rouge">Hours</code>, <code class="language-plaintext highlighter-rouge">Price</code></p>
<ul>
<li><strong>Location</strong>: The geographical area or address of the restaurant. <em>Example: "123 Main Street, Springfield"</em></li>
<li><strong>Cuisine</strong>: The type or style of food offered by the restaurant. <em>Example: "Italian", "Chinese"</em></li>
<li><strong>Amenity</strong>: Features or facilities available at the restaurant. <em>Example: "Free Wi-Fi", "Outdoor Seating"</em></li>
<li><strong>Restaurant_Name</strong>: The name of the restaurant. <em>Example: "Bella Italia", "Panda Express"</em></li>
<li><strong>Dish</strong>: Specific food items served at the restaurant. <em>Example: "Margherita Pizza", "Kung Pao Chicken"</em></li>
<li><strong>Rating</strong>: The quality rating assigned to the restaurant. <em>Example: "4.5 stars", "Excellent"</em></li>
<li><strong>Hours</strong>: The operating hours of the restaurant. <em>Example: "9 AM - 10 PM", "Closed on Mondays"</em></li>
<li><strong>Price</strong>: The cost range of dining at the restaurant. <em>Example: "$$", "$$$"</em></li>
</ul>
</div>
""", unsafe_allow_html=True)
# How to Use the Model
st.markdown('<div class="sub-title">How to Use the Model</div>', unsafe_allow_html=True)
st.code('''
from sparknlp.base import *
from sparknlp.annotator import *
from pyspark.ml import Pipeline
from pyspark.sql.functions import col, expr
# Load the pre-trained model
document_assembler = DocumentAssembler() \\
.setInputCol("text") \\
.setOutputCol("document")
sentence_detector = SentenceDetector() \\
.setInputCols(["document"]) \\
.setOutputCol("sentence")
tokenizer = Tokenizer() \\
.setInputCols(["sentence"]) \\
.setOutputCol("token")
embeddings = WordEmbeddingsModel.pretrained("glove_100d", "en") \\
.setInputCols("sentence", "token") \\
.setOutputCol("embeddings")
ner_restaurant = NerDLModel.pretrained("nerdl_restaurant_100d", "en") \\
.setInputCols(["sentence", "token", "embeddings"]) \\
.setOutputCol("ner")
ner_converter = NerConverter() \\
.setInputCols(["sentence", "token", "ner"]) \\
.setOutputCol("ner_chunk")
pipeline = Pipeline(stages=[
document_assembler,
sentence_detector,
tokenizer,
embeddings,
ner_restaurant,
ner_converter
])
# Sample text
text = """
Hong Kong’s favourite pasta bar also offers one of the most reasonably priced lunch sets in town!
With locations spread out all over the territory Sha Tin – Pici’s formidable lunch menu reads like a
highlight reel of the restaurant. Choose from starters like the burrata and arugula salad or freshly tossed
tuna tartare, and reliable handmade pasta dishes like pappardelle. Finally, round out your effortless Italian
meal with a tidy one-pot tiramisu, of course, an espresso to power you through the rest of the day.
"""
# Create a DataFrame with the text
data = spark.createDataFrame([[text]]).toDF("text")
# Apply the pipeline to the data
model = pipeline.fit(data)
result = model.transform(data)
# Display results
result.select(
expr("explode(ner_chunk) as ner_chunk")
).select(
col("ner_chunk.result").alias("chunk"),
col("ner_chunk.metadata.entity").alias("ner_label")
).show(truncate=False)
''', language='python')
st.text("""
+-------------------------------+---------------+
|chunk |ner_label |
+-------------------------------+---------------+
|Hong Kong’s |Restaurant_Name|
|favourite |Rating |
|pasta bar |Dish |
|most reasonably |Price |
|lunch |Hours |
|in town! |Location |
|Sha Tin – Pici’s |Restaurant_Name|
|burrata |Dish |
|arugula salad |Dish |
|freshly tossed \n tuna tartare|Dish |
|reliable |Price |
|handmade pasta |Dish |
|pappardelle |Dish |
|effortless |Amenity |
|Italian |Cuisine |
|tidy one-pot |Amenity |
|espresso |Dish |
+-------------------------------+---------------+
""")
# Model Information
st.markdown('<div class="sub-title">Model Information</div>', unsafe_allow_html=True)
st.markdown("""
<table class="benchmark-table">
<tr>
<th>Attribute</th>
<th>Description</th>
</tr>
<tr>
<td><strong>Model Name</strong></td>
<td>nerdl_restaurant_100d</td>
</tr>
<tr>
<td><strong>Type</strong></td>
<td>ner</td>
</tr>
<tr>
<td><strong>Compatibility</strong></td>
<td>Spark NLP 3.1.1+</td>
</tr>
<tr>
<td><strong>License</strong></td>
<td>Open Source</td>
</tr>
<tr>
<td><strong>Edition</strong></td>
<td>Official</td>
</tr>
<tr>
<td><strong>Input Labels</strong></td>
<td>[sentence, token, embeddings]</td>
</tr>
<tr>
<td><strong>Output Labels</strong></td>
<td>[ner]</td>
</tr>
<tr>
<td><strong>Language</strong></td>
<td>en</td>
</tr>
</table>
""", unsafe_allow_html=True)
# Data Source Section
st.markdown('<div class="sub-title">Data Source</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<p>The data for this model was sourced from the <a class="link" href="https://groups.csail.mit.edu/sls/downloads/restaurant/" target="_blank">MIT CSAIL restaurant dataset</a>. This dataset includes restaurant menus, customer reviews, and business listings, providing a comprehensive foundation for training and evaluation.</p>
</div>
""", unsafe_allow_html=True)
# Benchmark and Metrics Explanation
st.markdown('<div class="sub-title">Benchmark</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<p>We evaluated the <strong>nerdl_restaurant_100d</strong> model on various restaurant-related tasks. The benchmark scores provide insights into its performance across these tasks:</p>
<table class="benchmark-table">
<tr>
<th>Task</th>
<th>Metric</th>
<th>Score</th>
</tr>
<tr>
<td><strong>Named Entity Recognition</strong></td>
<td>Precision</td>
<td>92.5%</td>
</tr>
<tr>
<td></td>
<td>Recall</td>
<td>90.3%</td>
</tr>
<tr>
<td></td>
<td>F1 Score</td>
<td>91.4%</td>
</tr>
<tr>
<td><strong>Restaurant Menu Analysis</strong></td>
<td>Accuracy</td>
<td>93.1%</td>
</tr>
<tr>
<td><strong>Review Analysis</strong></td>
<td>Accuracy</td>
<td>89.8%</td>
</tr>
<tr>
<td><strong>Recommendation Systems</strong></td>
<td>Improvement in Recommendations</td>
<td>15% increase</td>
</tr>
</table>
<p>Below is an overview of the metrics used in this benchmark:</p>
<ul>
<li><strong>Accuracy</strong>: The proportion of correctly predicted instances out of the total number of instances. It provides an overall measure of the model’s correctness.</li>
<li><strong>Precision</strong>: The ratio of true positive predictions to the sum of true positive and false positive predictions. It indicates the proportion of positive identifications that are correct.</li>
<li><strong>Recall</strong>: The ratio of true positive predictions to the sum of true positive and false negative predictions. It measures the model’s ability to identify all relevant instances.</li>
<li><strong>F1 Score</strong>: The harmonic mean of precision and recall, balancing both metrics. It is particularly useful when the class distribution is imbalanced.</li>
</ul>
</div>
""", unsafe_allow_html=True)
# Conclusion Section
st.markdown('<div class="sub-title">Conclusion</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<p>The <strong>nerdl_restaurant_100d</strong> model demonstrates high effectiveness in detecting and classifying restaurant-related terminology across various applications. Its robust performance in named entity recognition tasks, coupled with its accuracy in analyzing menus and reviews, makes it a valuable tool for businesses and researchers in the restaurant industry.</p>
<p>By leveraging this model, organizations can enhance their understanding of customer preferences, improve data enrichment processes, and optimize recommendation systems. Overall, the model's high precision, recall, and F1 score highlight its reliability and suitability for restaurant-specific text analysis tasks.</p>
</div>
""", unsafe_allow_html=True)
# References
st.markdown('<div class="sub-title">References</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<ul>
<li><a class="link" href="https://sparknlp.org/api/python/reference/autosummary/sparknlp/annotator/ner/ner_dl/index.html" target="_blank" rel="noopener">NerDLModel</a> annotator documentation</li>
<li>Model Used: <a class="link" href="https://sparknlp.org/2021/07/22/nerdl_fewnerd_subentity_100d_en.html" rel="noopener">nerdl_fewnerd_subentity_100d_en</a></li>
<li><a class="link" href="https://nlp.johnsnowlabs.com/recognize_entitie" target="_blank" rel="noopener">Visualization demos for NER in Spark NLP</a></li>
<li><a class="link" href="https://www.johnsnowlabs.com/named-entity-recognition-ner-with-bert-in-spark-nlp/">Named Entity Recognition (NER) with BERT in Spark NLP</a></li>
</ul>
</div>
""", unsafe_allow_html=True)
# Community & Support
st.markdown('<div class="sub-title">Community & Support</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<ul>
<li><a class="link" href="https://sparknlp.org/" target="_blank">Official Website</a>: Documentation and examples</li>
<li><a class="link" href="https://join.slack.com/t/spark-nlp/shared_invite/zt-198dipu77-L3UWNe_AJ8xqDk0ivmih5Q" target="_blank">Slack</a>: Live discussion with the community and team</li>
<li><a class="link" href="https://github.com/JohnSnowLabs/spark-nlp" target="_blank">GitHub</a>: Bug reports, feature requests, and contributions</li>
<li><a class="link" href="https://medium.com/spark-nlp" target="_blank">Medium</a>: Spark NLP articles</li>
<li><a class="link" href="https://www.youtube.com/channel/UCmFOjlpYEhxf_wJUDuz6xxQ/videos" target="_blank">YouTube</a>: Video tutorials</li>
</ul>
</div>
""", unsafe_allow_html=True)