billu_gunda_op / app.py
billusanda007's picture
Update app.py
5d71004 verified
import nltk
import matplotlib.pyplot as plt
from nltk.sentiment import SentimentIntensityAnalyzer
import streamlit as st
import os
import joblib
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
import re
from urllib.parse import urlparse
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords as nltk_stopwords # Rename the variable to avoid conflicts
from nltk.stem import WordNetLemmatizer
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
import plotly.graph_objects as go
import openai
import pprint
#import google.generativeai as palm
import nltk
nltk.download('vader_lexicon')
nltk.download('omw-1.4')
nltk.download('wordnet')
nltk.download('wordnet2022')
nltk.download('punkt')
nltk.download('stopwords')
stop_words = set(nltk_stopwords.words("english"))
lemmatizer = WordNetLemmatizer()
#api_key = os.getenv("OPENAI_API_KEY")
#llm = OpenAI(temperature=0.6, api_key=api_key)
#api_key = os.getenv("PALM_API_KEY")
#palm.configure(api_key=api_key)
import google.generativeai as genai
import os
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
ml_model = joblib.load('Stress identification NLP')
tfidf_vectorizer = joblib.load('tfidf_vectorizer.joblib')
def textProcess(sent):
try:
if sent is None:
return ""
sent = re.sub('[][)(]', ' ', sent)
sent = [word for word in sent.split() if not urlparse(word).scheme]
sent = ' '.join(sent)
sent = re.sub(r'\@\w+', '', sent)
sent = re.sub(re.compile("<.*?>"), '', sent)
sent = re.sub("[^A-Za-z0-9]", ' ', sent)
sent = sent.lower()
sent = [word.strip() for word in sent.split()]
sent = ' '.join(sent)
tokens = word_tokenize(sent)
for word in tokens.copy():
if word in stop_words:
tokens.remove(word)
sent = [lemmatizer.lemmatize(word) for word in tokens]
sent = ' '.join(sent)
return sent
except Exception as ex:
return ""
def score_text(text):
model = genai.GenerativeModel("gemini-1.5-flash")
prompt ="""Score the following transcript :\n """+text+""" \nScore it out of 5 which can take any float value from 0 to 5 under the following classes in the list:
['Depressiveness', 'Suicidal Thoughts', 'Self Esteem', 'Supressiveness', 'Tiredness']
each separated by commas and give the response in one single line without any leading spaces or new line
changes directly starting with scores and only write the scores no extra information and if any score
is an integer value end it with .0"""
response = model.generate_content(
prompt,
generation_config=genai.types.GenerationConfig(
# Only one candidate for now.
candidate_count=1,
stop_sequences=["x"],
max_output_tokens=100,
temperature=0,
),
)
#print(response.text)
#scores = completion.result
scores = response.text
return scores
def create_radar_chart(scores):
numbers = re.findall(r'\d+\.\d+', scores)
number_list = [float(num) for num in numbers]
categories = ['Depressiveness', 'Suicidal Thoughts', 'Self Esteem', 'Supressiveness', 'Tiredness']
x = number_list
num_categories = len(categories)
max_value = 5.0
data = [(value / max_value) * 5 for value in x]
angles = np.linspace(0, 2 * np.pi, num_categories, endpoint=False).tolist()
angles += angles[:1]
data += data[:1]
fig = go.Figure()
fig.add_trace(go.Scatterpolar(
r=data,
theta=categories,
fill='toself',
text=number_list,#
textfont=dict(color='deepskyblue'),#
name='Mental Health Radar Chart'
))
return fig
def get_sentiment_percentage(text):
sia = SentimentIntensityAnalyzer()
sentiment_scores = sia.polarity_scores(text)
positive_percentage = sentiment_scores['pos'] * 100
neutral_percentage = sentiment_scores['neu'] * 100
negative_percentage = sentiment_scores['neg'] * 100
return positive_percentage, neutral_percentage, negative_percentage
def main():
st.title("Stress Visualizer")
st.write("Enter some text to predict if the person is in stress or not.")
user_input = st.text_area("Enter text here:")
if st.button("Predict"):
if user_input:
processed_text = textProcess(user_input)
positive_percentage, neutral_percentage, negative_percentage = get_sentiment_percentage(processed_text)
st.write(f"Positive: {positive_percentage:.2f}%")
st.write(f"Neutral: {neutral_percentage:.2f}%")
st.write(f"Negative: {negative_percentage:.2f}%")
tfidf_text = tfidf_vectorizer.transform([processed_text])
prediction = ml_model.predict(tfidf_text)[0]
if prediction == 1:
result = "**This person is in stress.**"
else:
result = "**This person is not in stress.**"
if result == "**This person is not in stress.**" and max(positive_percentage,negative_percentage)==negative_percentage:
result="**This person is in stress.**"
st.markdown(result)
scores = score_text(user_input)
fig = go.Figure(data=[go.Bar(
x=['Positive', 'Neutral', 'Negative'],
y=[positive_percentage, neutral_percentage, negative_percentage],
marker_color=['green', 'gray', 'red']
)])
fig.update_layout(
yaxis_title='Percentage',
title='Sentiment Percentages'
)
radar_chart = create_radar_chart(scores)
st.plotly_chart(fig)
st.plotly_chart(radar_chart)
if __name__ == '__main__':
main()