Spaces:
Runtime error
Runtime error
SudhanshuBlaze
commited on
Commit
•
bee76d7
0
Parent(s):
add files
Browse files- .gitignore +2 -0
- EDxHuggingface.py +128 -0
- README.md +32 -0
- requirements.txt +4 -0
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
.env
|
2 |
+
.DS_Store
|
EDxHuggingface.py
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import plotly.graph_objects as go
|
2 |
+
from plotly.subplots import make_subplots
|
3 |
+
import streamlit as st
|
4 |
+
import requests
|
5 |
+
import json
|
6 |
+
import os
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
# AI model code
|
11 |
+
HF_API_KEY = os.getenv("HF_API_KEY")
|
12 |
+
|
13 |
+
API_URL = "https://api-inference.huggingface.co/models/bhadresh-savani/bert-base-go-emotion"
|
14 |
+
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
|
15 |
+
|
16 |
+
# Set page title
|
17 |
+
st.title("GoEmotions Dashboard - Analyzing Emotions in Text")
|
18 |
+
|
19 |
+
# Add page description
|
20 |
+
description = "The GoEmotions Dashboard is a web-based user interface for analyzing emotions in text. The dashboard is powered by a pre-trained natural language processing model that can detect emotions in text input. Users can input any text and the dashboard will display the detected emotions in a set of gauges, with each gauge representing the intensity of a specific emotion category. The gauge colors are based on a predefined color map for each emotion category. This dashboard is useful for anyone who wants to understand the emotional content of a text, including content creators, marketers, and researchers."
|
21 |
+
st.markdown(description)
|
22 |
+
|
23 |
+
def query(payload):
|
24 |
+
data = json.dumps(payload)
|
25 |
+
response = requests.request("POST", API_URL, headers=headers, data=data)
|
26 |
+
return json.loads(response.content.decode("utf-8"))
|
27 |
+
|
28 |
+
# Define color map for each emotion category
|
29 |
+
color_map = {
|
30 |
+
'admiration': ['#1f77b4', '#aec7e8', '#ff7f0e', '#d62728'],
|
31 |
+
'amusement': ['#ff7f0e', '#ffbb78', '#2ca02c', '#d62728'],
|
32 |
+
'anger': ['#d62728', '#ff9896', '#2ca02c', '#bcbd22'],
|
33 |
+
'annoyance': ['#d62728', '#ff9896', '#2ca02c', '#bcbd22'],
|
34 |
+
'approval': ['#1f77b4', '#aec7e8', '#ff7f0e', '#d62728'],
|
35 |
+
'caring': ['#1f77b4', '#aec7e8', '#ff7f0e', '#d62728'],
|
36 |
+
'confusion': ['#9467bd', '#c5b0d5', '#ff7f0e', '#d62728'],
|
37 |
+
'curiosity': ['#9467bd', '#c5b0d5', '#ff7f0e', '#d62728'],
|
38 |
+
'desire': ['#ff7f0e', '#ffbb78', '#2ca02c', '#d62728'],
|
39 |
+
'disappointment': ['#d62728', '#ff9896', '#2ca02c', '#bcbd22'],
|
40 |
+
'disapproval': ['#d62728', '#ff9896', '#2ca02c', '#bcbd22'],
|
41 |
+
'disgust': ['#d62728', '#ff9896', '#2ca02c', '#bcbd22'],
|
42 |
+
'embarrassment': ['#9467bd', '#c5b0d5', '#ff7f0e', '#d62728'],
|
43 |
+
'excitement': ['#ff7f0e', '#ffbb78', '#2ca02c', '#d62728'],
|
44 |
+
'fear': ['#d62728', '#ff9896', '#2ca02c', '#bcbd22'],
|
45 |
+
'gratitude': ['#1f77b4', '#aec7e8', '#ff7f0e', '#d62728'],
|
46 |
+
'grief': ['#d62728', '#ff9896', '#2ca02c', '#bcbd22'],
|
47 |
+
'joy': ['#ff7f0e', '#ffbb78', '#2ca02c', '#d62728'],
|
48 |
+
'love': ['#1f77b4', '#aec7e8', '#ff7f0e', '#d62728'],
|
49 |
+
'nervousness': ['#9467bd', '#c5b0d5', '#ff7f0e', '#d62728'],
|
50 |
+
'optimism': ['#1f77b4', '#aec7e8', '#ff7f0e', '#d62728'],
|
51 |
+
'pride': ['#1f77b4', '#aec7e8', '#ff7f0e', '#d62728'],
|
52 |
+
'realization': ['#9467bd', '#c5b0d5', '#ff7f0e', '#d62728'],
|
53 |
+
'relief': ['#1f77b4', '#aec7e8', '#ff7f0e', '#d62728'],
|
54 |
+
'remorse': ['#d62728', '#ff9896', '#2ca02c', '#bcbd22'],
|
55 |
+
'sadness': ['#d62728', '#ff9896', '#2ca02c', '#bcbd22'],
|
56 |
+
'surprise': ['#9467bd', '#c5b0d5', '#ff7f0e', '#d62728'],
|
57 |
+
'neutral': ['#1f77b4', '#aec7e8', '#ff7f0e', '#d62728']
|
58 |
+
}
|
59 |
+
|
60 |
+
# Define default options
|
61 |
+
default_options = [
|
62 |
+
"I'm so excited for my vacation next week!",
|
63 |
+
"I'm feeling so stressed about work.",
|
64 |
+
"I just received great news from my doctor!",
|
65 |
+
"I can't wait to see my best friend tomorrow.",
|
66 |
+
"I'm feeling so lonely and sad today."
|
67 |
+
]
|
68 |
+
|
69 |
+
|
70 |
+
# Create dropdown with default options
|
71 |
+
selected_option = st.selectbox("Select a default option or enter your own text:", default_options)
|
72 |
+
|
73 |
+
# Display text input with selected option as default value
|
74 |
+
text_input = st.text_input("Enter text to analyze emotions:", selected_option)
|
75 |
+
|
76 |
+
# Add submit button
|
77 |
+
if st.button("Submit"):
|
78 |
+
|
79 |
+
# Call API and get predicted probabilities for each emotion category
|
80 |
+
response = query(text_input)
|
81 |
+
predicted_probabilities = response[0]
|
82 |
+
|
83 |
+
# Sort the predicted probabilities in descending order
|
84 |
+
sorted_probs = sorted(predicted_probabilities, key=lambda x: x['score'], reverse=True)
|
85 |
+
|
86 |
+
# Get the top 4 emotion categories and their scores
|
87 |
+
top_emotions = sorted_probs[:4]
|
88 |
+
top_scores = [e['score'] for e in top_emotions]
|
89 |
+
|
90 |
+
# Normalize the scores so that they add up to 100%
|
91 |
+
total = sum(top_scores)
|
92 |
+
normalized_scores = [score/total * 100 for score in top_scores]
|
93 |
+
|
94 |
+
# Create the gauge charts for the top 4 emotion categories using the normalized scores
|
95 |
+
fig = make_subplots(rows=2, cols=2, specs=[[{'type': 'indicator'}, {'type': 'indicator'}],
|
96 |
+
[{'type': 'indicator'}, {'type': 'indicator'}]],
|
97 |
+
vertical_spacing=0.4)
|
98 |
+
|
99 |
+
for i, emotion in enumerate(top_emotions):
|
100 |
+
category = emotion['label']
|
101 |
+
color = color_map[category]
|
102 |
+
value = normalized_scores[i]
|
103 |
+
row = i // 2 + 1
|
104 |
+
col = i % 2 + 1
|
105 |
+
fig.add_trace(go.Indicator(
|
106 |
+
domain={'x': [0, 1], 'y': [0, 1]},
|
107 |
+
value=value,
|
108 |
+
mode="gauge+number",
|
109 |
+
title={'text': category.capitalize()},
|
110 |
+
gauge={'axis': {'range': [None, 100]},
|
111 |
+
'bar': {'color': color[3]},
|
112 |
+
'bgcolor': 'white',
|
113 |
+
'borderwidth': 2,
|
114 |
+
'bordercolor': color[1],
|
115 |
+
'steps': [{'range': [0, 33], 'color': color[0]},
|
116 |
+
{'range': [33, 66], 'color': color[1]},
|
117 |
+
{'range': [66, 100], 'color': color[2]}],
|
118 |
+
'threshold': {'line': {'color': "black", 'width': 4},
|
119 |
+
'thickness': 0.5,
|
120 |
+
'value': 50}}), row=row, col=col)
|
121 |
+
|
122 |
+
|
123 |
+
# Update layout
|
124 |
+
fig.update_layout(height=400, margin=dict(t=50, b=5, l=0, r=0))
|
125 |
+
|
126 |
+
# Display gauge charts
|
127 |
+
st.plotly_chart(fig, use_container_width=True)
|
128 |
+
|
README.md
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# GoEmotions Dashboard - Analyzing Emotions in Text
|
2 |
+
|
3 |
+
This is a Python script that uses Streamlit, Plotly, and the Hugging Face API to create a web-based dashboard for analyzing emotions in text.
|
4 |
+
|
5 |
+
## Requirements:
|
6 |
+
|
7 |
+
Python 3.7 or higher
|
8 |
+
Hugging Face API
|
9 |
+
|
10 |
+
## Installation
|
11 |
+
|
12 |
+
- Clone this repository to your local machine.
|
13 |
+
- Install the required packages using pip:
|
14 |
+
|
15 |
+
```bash
|
16 |
+
pip install -r requirements.txt
|
17 |
+
```
|
18 |
+
|
19 |
+
- Create a free account on the Hugging Face website to get an API key.
|
20 |
+
|
21 |
+
- Create a `.env` file in the root directory of the project and add your
|
22 |
+
- Hugging Face API key like this: `HF_API_KEY=<your_api_key_here>`
|
23 |
+
|
24 |
+
## Usage
|
25 |
+
|
26 |
+
- Navigate to the root directory of the project.
|
27 |
+
- Run the Streamlit app by typing `streamlit run app.py` in the command line.
|
28 |
+
|
29 |
+
- A web-based dashboard will open in your default browser.
|
30 |
+
- Type or paste a text input in the text box provided.
|
31 |
+
- The dashboard will display the detected emotions in a set of gauges, with each gauge representing the intensity of a specific emotion category.
|
32 |
+
- The gauge colors are based on a predefined color map for each emotion category.
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
plotly==5.3.1
|
2 |
+
streamlit==1.3.0
|
3 |
+
requests==2.26.0
|
4 |
+
python-dotenv==0.19.1
|