File size: 1,372 Bytes
a52c03c
 
ab0db34
a52c03c
 
ab0db34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a52c03c
 
 
ab0db34
a52c03c
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import streamlit as st
from google.cloud import language_v1
from google.oauth2 import service_account

def print_result(annotations):
    score = annotations.document_sentiment.score
    magnitude = annotations.document_sentiment.magnitude

    for index, sentence in enumerate(annotations.sentences):
        sentence_sentiment = sentence.sentiment.score
        st.write(f"Sentence {index} has a sentiment score of {sentence_sentiment}")

    st.write(f"Overall Sentiment: score of {score} with magnitude of {magnitude}")

def analyze_sentiment(texts):
    # Load credentials directly from secrets
    credentials_info = st.secrets["GOOGLE_APPLICATION_CREDENTIALS"]
    credentials = service_account.Credentials.from_service_account_info(credentials_info)

    client = language_v1.LanguageServiceClient(credentials=credentials)

    document = language_v1.Document(content=texts, type_=language_v1.Document.Type.PLAIN_TEXT)
    annotations = client.analyze_sentiment(request={"document": document})

    return annotations

st.title("Sentiment Analysis App")
st.write("Enter some text to analyze its sentiment:")

text_input = st.text_area("Text to analyze", height=200)

if st.button("Analyze Sentiment"):
    if text_input:
        annotations = analyze_sentiment(text_input) 
        print_result(annotations)
    else:
        st.warning("Please enter some text.")