blazingbunny commited on
Commit
04aa1d7
·
verified ·
1 Parent(s): 4133829

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -7
app.py CHANGED
@@ -3,23 +3,39 @@ from google.cloud import language_v1
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
 
@@ -35,4 +51,4 @@ if st.button("Analyze Sentiment"):
35
  annotations = analyze_sentiment(text_input)
36
  print_result(annotations)
37
  else:
38
- st.warning("Please enter some text.")
 
3
  from google.oauth2 import service_account
4
  import json
5
 
6
+ # ... (Your authentication code remains the same)
7
+
8
  def print_result(annotations):
9
+ # Overall Sentiment
10
  score = annotations.document_sentiment.score
11
  magnitude = annotations.document_sentiment.magnitude
12
+ st.write("**Overall Sentiment:**")
13
+ st.write(f" * Score: {score}")
14
+ st.write(f" * Magnitude: {magnitude}")
15
 
16
+ # Sentence-Level Sentiment
17
+ st.write("**Sentence-Level Sentiment:**")
18
  for index, sentence in enumerate(annotations.sentences):
19
+ sentence_text = sentence.text.content
20
  sentence_sentiment = sentence.sentiment.score
21
+ st.write(f"Sentence {index}: {sentence_text}")
22
+ st.write(f" * Sentiment score: {sentence_sentiment}")
23
+
24
+ # Entity-Level Sentiment (If Applicable)
25
+ if annotations.entities:
26
+ st.write("**Entity-Level Sentiment:**")
27
+ for entity in annotations.entities:
28
+ st.write(f"Entity: {entity.name} ({entity.type})")
29
+ st.write(f" * Sentiment Score: {entity.sentiment.score}")
30
+ st.write(f" * Magnitude: {entity.sentiment.magnitude}")
31
+ st.write(f" * Salience: {entity.salience}")
32
 
33
  def analyze_sentiment(texts):
34
+ # ... (Your authentication code)
 
 
35
 
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
 
 
51
  annotations = analyze_sentiment(text_input)
52
  print_result(annotations)
53
  else:
54
+ st.warning("Please enter some text.")