Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,10 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from meta_ai_api import MetaAI
|
| 3 |
from urllib.parse import urlparse
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Initialize Meta AI API
|
| 6 |
ai = MetaAI()
|
|
@@ -35,6 +39,35 @@ def display_sources(sources):
|
|
| 35 |
else:
|
| 36 |
st.write("No sources available.")
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
# ---------------------------------------------------------------------------- #
|
| 39 |
# Main Function
|
| 40 |
# ---------------------------------------------------------------------------- #
|
|
@@ -62,8 +95,22 @@ def main():
|
|
| 62 |
if submit_button and user_query:
|
| 63 |
# Fetching response from Meta AI
|
| 64 |
response = fetch_response(user_query)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
st.write(response.get('message', 'No response message.'))
|
| 67 |
|
| 68 |
# Display the AI response in a collapsible section
|
| 69 |
with st.expander("Show Sources"):
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from meta_ai_api import MetaAI
|
| 3 |
from urllib.parse import urlparse
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import plotly.express as px
|
| 6 |
+
from nltk.sentiment.vader import SentimentIntensityAnalyzer
|
| 7 |
+
import nltk
|
| 8 |
|
| 9 |
# Initialize Meta AI API
|
| 10 |
ai = MetaAI()
|
|
|
|
| 39 |
else:
|
| 40 |
st.write("No sources available.")
|
| 41 |
|
| 42 |
+
# ---------------------------------------------------------------------------- #
|
| 43 |
+
# Sentiment Analysis Function
|
| 44 |
+
# ---------------------------------------------------------------------------- #
|
| 45 |
+
|
| 46 |
+
# Download the VADER lexicon for sentiment analysis
|
| 47 |
+
nltk.download('vader_lexicon')
|
| 48 |
+
|
| 49 |
+
# Initialize the Sentiment Intensity Analyzer
|
| 50 |
+
sid = SentimentIntensityAnalyzer()
|
| 51 |
+
|
| 52 |
+
def sentiment_analysis(text):
|
| 53 |
+
# Split the text into sentences
|
| 54 |
+
sentences = [sentence.strip() for sentence in text.split('.') if sentence]
|
| 55 |
+
|
| 56 |
+
# Create a DataFrame to hold the content and sentiment scores
|
| 57 |
+
df = pd.DataFrame(sentences, columns=['content'])
|
| 58 |
+
|
| 59 |
+
# Calculate sentiment scores for each sentence
|
| 60 |
+
df['sentiment_scores'] = df['content'].apply(lambda x: sid.polarity_scores(x))
|
| 61 |
+
|
| 62 |
+
# Split sentiment_scores into separate columns
|
| 63 |
+
df = pd.concat([df.drop(['sentiment_scores'], axis=1), df['sentiment_scores'].apply(pd.Series)], axis=1)
|
| 64 |
+
|
| 65 |
+
# Determine the dominant sentiment and its confidence
|
| 66 |
+
df['dominant_sentiment'] = df[['neg', 'neu', 'pos']].idxmax(axis=1)
|
| 67 |
+
df['confidence'] = df[['neg', 'neu', 'pos']].max(axis=1)
|
| 68 |
+
|
| 69 |
+
return df
|
| 70 |
+
|
| 71 |
# ---------------------------------------------------------------------------- #
|
| 72 |
# Main Function
|
| 73 |
# ---------------------------------------------------------------------------- #
|
|
|
|
| 95 |
if submit_button and user_query:
|
| 96 |
# Fetching response from Meta AI
|
| 97 |
response = fetch_response(user_query)
|
| 98 |
+
msg = response.get('message', 'No response message.')
|
| 99 |
+
# Write response
|
| 100 |
+
st.write(msg)
|
| 101 |
+
|
| 102 |
+
# Run sentiment analysis
|
| 103 |
+
df_sentiment = sentiment_analysis(msg)
|
| 104 |
+
|
| 105 |
+
# Display negative sentence locations
|
| 106 |
+
fig = px.scatter(df_sentiment, y='dominant_sentiment', color='dominant_sentiment', size='confidence',
|
| 107 |
+
hover_data=['content'],
|
| 108 |
+
color_discrete_map={"neg": "firebrick", "neu": "navajowhite", "pos": "darkgreen"},
|
| 109 |
+
labels={'dominant_sentiment': 'Sentiment'},
|
| 110 |
+
title='Sentiment Analysis of Sentences')
|
| 111 |
+
fig.update_layout(width=800, height=300)
|
| 112 |
+
st.plotly_chart(fig)
|
| 113 |
|
|
|
|
| 114 |
|
| 115 |
# Display the AI response in a collapsible section
|
| 116 |
with st.expander("Show Sources"):
|