Update app.py
Browse files
app.py
CHANGED
@@ -3,39 +3,23 @@ from google.cloud import language_v1
|
|
3 |
from google.oauth2 import service_account
|
4 |
import json
|
5 |
|
6 |
-
|
7 |
-
# Load Google Cloud credentials
|
8 |
-
credentials = service_account.Credentials.from_service_account_file(json.loads(st.secrets["GOOGLE_APPLICATION_CREDENTIALS"]))
|
9 |
-
|
10 |
def print_result(annotations):
|
11 |
-
# Overall Sentiment
|
12 |
score = annotations.document_sentiment.score
|
13 |
magnitude = annotations.document_sentiment.magnitude
|
14 |
-
st.write("**Overall Sentiment:**")
|
15 |
-
st.write(f" * Score: {score}")
|
16 |
-
st.write(f" * Magnitude: {magnitude}")
|
17 |
|
18 |
-
# Sentence-Level Sentiment
|
19 |
-
st.write("**Sentence-Level Sentiment:**")
|
20 |
for index, sentence in enumerate(annotations.sentences):
|
21 |
-
sentence_text = sentence.text.content
|
22 |
sentence_sentiment = sentence.sentiment.score
|
23 |
-
st.write(f"Sentence {index}
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
st.write(f" * Magnitude: {entity.sentiment.magnitude}")
|
33 |
-
st.write(f" * Salience: {entity.salience}")
|
34 |
-
|
35 |
-
def analyze_sentiment(texts, credentials):
|
36 |
client = language_v1.LanguageServiceClient(credentials=credentials)
|
37 |
|
38 |
-
# Include options for entity analysis
|
39 |
document = language_v1.Document(content=texts, type_=language_v1.Document.Type.PLAIN_TEXT)
|
40 |
annotations = client.analyze_sentiment(request={"document": document})
|
41 |
|
@@ -48,7 +32,7 @@ text_input = st.text_area("Text to analyze", height=200)
|
|
48 |
|
49 |
if st.button("Analyze Sentiment"):
|
50 |
if text_input:
|
51 |
-
annotations = analyze_sentiment(text_input
|
52 |
print_result(annotations)
|
53 |
else:
|
54 |
st.warning("Please enter some text.")
|
|
|
3 |
from google.oauth2 import service_account
|
4 |
import json
|
5 |
|
|
|
|
|
|
|
|
|
6 |
def print_result(annotations):
|
|
|
7 |
score = annotations.document_sentiment.score
|
8 |
magnitude = annotations.document_sentiment.magnitude
|
|
|
|
|
|
|
9 |
|
|
|
|
|
10 |
for index, sentence in enumerate(annotations.sentences):
|
|
|
11 |
sentence_sentiment = sentence.sentiment.score
|
12 |
+
st.write(f"Sentence {index} has a sentiment score of {sentence_sentiment}")
|
13 |
+
|
14 |
+
st.write(f"Overall Sentiment: score of {score} with magnitude of {magnitude}")
|
15 |
+
|
16 |
+
def analyze_sentiment(texts):
|
17 |
+
# Load credentials directly from secrets (load as JSON)
|
18 |
+
credentials_info = json.loads(st.secrets["GOOGLE_APPLICATION_CREDENTIALS"])
|
19 |
+
credentials = service_account.Credentials.from_service_account_info(credentials_info)
|
20 |
+
|
|
|
|
|
|
|
|
|
21 |
client = language_v1.LanguageServiceClient(credentials=credentials)
|
22 |
|
|
|
23 |
document = language_v1.Document(content=texts, type_=language_v1.Document.Type.PLAIN_TEXT)
|
24 |
annotations = client.analyze_sentiment(request={"document": document})
|
25 |
|
|
|
32 |
|
33 |
if st.button("Analyze Sentiment"):
|
34 |
if text_input:
|
35 |
+
annotations = analyze_sentiment(text_input)
|
36 |
print_result(annotations)
|
37 |
else:
|
38 |
st.warning("Please enter some text.")
|