Making It look more asthetic
Browse files
main.py
CHANGED
|
@@ -3,6 +3,8 @@ import json
|
|
| 3 |
import torch
|
| 4 |
from transformers import AutoTokenizer
|
| 5 |
from modelling_cnn import CNNForNER, SentimentCNNModel
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Load the Yoruba NER model
|
| 8 |
ner_model_name = "./my_model/pytorch_model.bin"
|
|
@@ -70,22 +72,71 @@ def analyze_text(text):
|
|
| 70 |
return ner_labels, sentiment
|
| 71 |
|
| 72 |
def main():
|
|
|
|
|
|
|
| 73 |
st.title("YorubaCNN Models for NER and Sentiment Analysis")
|
| 74 |
-
|
| 75 |
# Input text
|
| 76 |
text = st.text_area("Enter Yoruba text", "")
|
| 77 |
-
|
| 78 |
if st.button("Analyze"):
|
| 79 |
if text:
|
| 80 |
-
ner_labels,
|
| 81 |
-
|
| 82 |
# Display Named Entities
|
| 83 |
-
st.
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
# Display Sentiment Analysis
|
| 87 |
-
st.
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|
| 91 |
-
main()
|
|
|
|
| 3 |
import torch
|
| 4 |
from transformers import AutoTokenizer
|
| 5 |
from modelling_cnn import CNNForNER, SentimentCNNModel
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import altair as alt
|
| 8 |
|
| 9 |
# Load the Yoruba NER model
|
| 10 |
ner_model_name = "./my_model/pytorch_model.bin"
|
|
|
|
| 72 |
return ner_labels, sentiment
|
| 73 |
|
| 74 |
def main():
|
| 75 |
+
st.set_page_config(page_title="YorubaCNN for NER and Sentiment Analysis", layout="wide")
|
| 76 |
+
|
| 77 |
st.title("YorubaCNN Models for NER and Sentiment Analysis")
|
| 78 |
+
|
| 79 |
# Input text
|
| 80 |
text = st.text_area("Enter Yoruba text", "")
|
| 81 |
+
|
| 82 |
if st.button("Analyze"):
|
| 83 |
if text:
|
| 84 |
+
ner_labels, sentiment = analyze_text(text)
|
| 85 |
+
|
| 86 |
# Display Named Entities
|
| 87 |
+
st.header("Named Entities")
|
| 88 |
+
|
| 89 |
+
# Convert NER results to DataFrame
|
| 90 |
+
ner_df = pd.DataFrame([label.split(': ') for label in ner_labels], columns=['Token', 'Entity'])
|
| 91 |
+
|
| 92 |
+
# Display NER results in a styled table
|
| 93 |
+
st.dataframe(ner_df.style.highlight_max(axis=0, color='lightblue'))
|
| 94 |
+
|
| 95 |
# Display Sentiment Analysis
|
| 96 |
+
st.header("Sentiment Analysis")
|
| 97 |
+
|
| 98 |
+
# Create a sentiment score (you may need to adjust this based on your model's output)
|
| 99 |
+
sentiment_score = 0.8 if sentiment == "positive" else -0.8 if sentiment == "negative" else 0
|
| 100 |
+
|
| 101 |
+
# Create a chart for sentiment visualization
|
| 102 |
+
sentiment_df = pd.DataFrame({'sentiment': [sentiment_score]})
|
| 103 |
+
chart = alt.Chart(sentiment_df).mark_bar().encode(
|
| 104 |
+
x=alt.X('sentiment', scale=alt.Scale(domain=(-1, 1))),
|
| 105 |
+
color=alt.condition(
|
| 106 |
+
alt.datum.sentiment > 0,
|
| 107 |
+
alt.value("green"),
|
| 108 |
+
alt.value("red")
|
| 109 |
+
)
|
| 110 |
+
).properties(width=600, height=100)
|
| 111 |
+
|
| 112 |
+
st.altair_chart(chart)
|
| 113 |
+
st.write(f"Sentiment: {sentiment.capitalize()}")
|
| 114 |
+
|
| 115 |
+
# Explanatory section
|
| 116 |
+
with st.expander("About this analysis"):
|
| 117 |
+
st.write("""
|
| 118 |
+
This tool uses YorubaCNN models to perform two types of analysis on Yoruba text:
|
| 119 |
+
|
| 120 |
+
1. **Named Entity Recognition (NER)**: Identifies and classifies named entities (e.g., person names, organizations) in the text.
|
| 121 |
+
2. **Sentiment Analysis**: Determines the overall emotional tone of the text (positive, negative, or neutral).
|
| 122 |
+
|
| 123 |
+
The models used are based on Convolutional Neural Networks (CNN) and are specifically trained for the Yoruba language.
|
| 124 |
+
""")
|
| 125 |
+
|
| 126 |
+
# Styling
|
| 127 |
+
st.markdown("""
|
| 128 |
+
<style>
|
| 129 |
+
.stAlert > div {
|
| 130 |
+
padding-top: 20px;
|
| 131 |
+
padding-bottom: 20px;
|
| 132 |
+
}
|
| 133 |
+
.stDataFrame {
|
| 134 |
+
padding: 10px;
|
| 135 |
+
border-radius: 5px;
|
| 136 |
+
background-color: #f0f2f6;
|
| 137 |
+
}
|
| 138 |
+
</style>
|
| 139 |
+
""", unsafe_allow_html=True)
|
| 140 |
|
| 141 |
if __name__ == "__main__":
|
| 142 |
+
main()
|