Update app.py
Browse files
app.py
CHANGED
@@ -1 +1,49 @@
|
|
1 |
-
st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from google.cloud import language_v1
|
3 |
+
from google.oauth2 import service_account
|
4 |
+
import json
|
5 |
+
|
6 |
+
# Load credentials from Hugging Face Secrets
|
7 |
+
def load_credentials():
|
8 |
+
credentials_info = json.loads(st.secrets["GOOGLE_APPLICATION_CREDENTIALS"])
|
9 |
+
return service_account.Credentials.from_service_account_info(credentials_info)
|
10 |
+
|
11 |
+
# Create a Language Service client with the provided credentials
|
12 |
+
def get_language_client():
|
13 |
+
credentials = load_credentials()
|
14 |
+
return language_v1.LanguageServiceClient(credentials=credentials)
|
15 |
+
|
16 |
+
# Perform sentiment analysis on the input text
|
17 |
+
def analyze_sentiment(texts):
|
18 |
+
client = get_language_client()
|
19 |
+
document = language_v1.Document(content=texts, type_=language_v1.Document.Type.PLAIN_TEXT)
|
20 |
+
return client.analyze_sentiment(request={"document": document})
|
21 |
+
|
22 |
+
# Display sentiment analysis results
|
23 |
+
def display_sentiment_results(annotations):
|
24 |
+
score = annotations.document_sentiment.score
|
25 |
+
magnitude = annotations.document_sentiment.magnitude
|
26 |
+
|
27 |
+
for index, sentence in enumerate(annotations.sentences):
|
28 |
+
sentence_sentiment = sentence.sentiment.score
|
29 |
+
st.write(f"Sentence {index} has a sentiment score of {sentence_sentiment}")
|
30 |
+
|
31 |
+
st.write(f"Overall Sentiment: score of {score} with magnitude of {magnitude}")
|
32 |
+
|
33 |
+
# Main function to run the app
|
34 |
+
def main():
|
35 |
+
st.title("Sentiment Analysis App")
|
36 |
+
st.write("Enter some text to analyze its sentiment:")
|
37 |
+
|
38 |
+
text_input = st.text_area("Text to analyze", height=200)
|
39 |
+
|
40 |
+
if st.button("Analyze Sentiment"):
|
41 |
+
if text_input:
|
42 |
+
annotations = analyze_sentiment(text_input)
|
43 |
+
display_sentiment_results(annotations)
|
44 |
+
else:
|
45 |
+
st.warning("Please enter some text.")
|
46 |
+
|
47 |
+
# Run the app
|
48 |
+
if __name__ == "__main__":
|
49 |
+
main()
|