e-motion project using gradio
Browse files- README.md +5 -4
- assets/e-motion_logo_17.svg +15 -0
- requirements.txt +12 -0
- src/app.py +6 -0
- src/gradio_interface.py +87 -0
- src/twitter_roberta.py +32 -0
README.md
CHANGED
@@ -1,12 +1,13 @@
|
|
1 |
---
|
2 |
title: E Motion
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.13.0
|
8 |
-
app_file: app.py
|
9 |
pinned: false
|
|
|
10 |
---
|
11 |
|
12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
title: E Motion
|
3 |
+
emoji: π
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: blue
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.13.0
|
8 |
+
app_file: /src/app.py
|
9 |
pinned: false
|
10 |
+
license: mit
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
assets/e-motion_logo_17.svg
ADDED
requirements.txt
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Main dependencies
|
2 |
+
|
3 |
+
gradio==4.2.0
|
4 |
+
transformers==4.35.0
|
5 |
+
scipy==1.11.3
|
6 |
+
numpy==1.26.1
|
7 |
+
torch==1.12.1
|
8 |
+
|
9 |
+
# Development dependencies
|
10 |
+
pylint==3.0.2
|
11 |
+
isort==5.12.0
|
12 |
+
black==23.11.0
|
src/app.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Main module to run the Gradio interface for sentiment analysis."""
|
2 |
+
|
3 |
+
from gradio_interface import demo
|
4 |
+
|
5 |
+
if __name__ == "__main__":
|
6 |
+
demo.launch(inbrowser=True)
|
src/gradio_interface.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Module for setting up the Gradio interface for sentiment analysis."""
|
2 |
+
import pathlib
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
from twitter_roberta import predict_sentiment
|
6 |
+
|
7 |
+
theme = gr.themes.Base(
|
8 |
+
primary_hue="indigo",
|
9 |
+
font=[
|
10 |
+
gr.themes.GoogleFont("MD Mono"),
|
11 |
+
"ui-sans-serif",
|
12 |
+
"system-ui",
|
13 |
+
"sans-serif",
|
14 |
+
],
|
15 |
+
font_mono=[
|
16 |
+
gr.themes.GoogleFont("Lato"),
|
17 |
+
"ui-monospace",
|
18 |
+
"Consolas",
|
19 |
+
"monospace",
|
20 |
+
],
|
21 |
+
).set(
|
22 |
+
body_background_fill_dark="linear-gradient(45deg, rgba(23,19,57,1) 0%, rgba(6,2,13,1) 100%);",
|
23 |
+
body_background_fill="linear-gradient(45deg, rgba(184,201,255,1) 0%, rgba(114,52,224,1) 100%);",
|
24 |
+
body_text_color="*primary_900",
|
25 |
+
body_text_color_subdued="*neutral_950",
|
26 |
+
body_text_color_subdued_dark="*primary_300",
|
27 |
+
button_secondary_background_fill="*primary_300",
|
28 |
+
button_secondary_background_fill_dark="*primary_600",
|
29 |
+
button_secondary_background_fill_hover="*primary_100",
|
30 |
+
button_secondary_background_fill_hover_dark="*primary_400",
|
31 |
+
button_secondary_text_color="*neutral_950",
|
32 |
+
)
|
33 |
+
|
34 |
+
|
35 |
+
# Set up the Gradio interface for the application.
|
36 |
+
with gr.Blocks(theme=theme, title="π E-motion π") as demo:
|
37 |
+
with gr.Row():
|
38 |
+
with gr.Column(scale=3):
|
39 |
+
pass
|
40 |
+
with gr.Column(scale=1):
|
41 |
+
gr.Image(
|
42 |
+
"assets/e-motion_logo_17.svg", # Convert the Path object to a string
|
43 |
+
height=145,
|
44 |
+
show_download_button=False,
|
45 |
+
container=False,
|
46 |
+
interactive=False,
|
47 |
+
)
|
48 |
+
with gr.Column(scale=3):
|
49 |
+
pass
|
50 |
+
with gr.Row():
|
51 |
+
with gr.Column():
|
52 |
+
box = gr.Textbox(
|
53 |
+
placeholder="Type something to check sentiment! π€",
|
54 |
+
label="π Give it a go!",
|
55 |
+
info="We are classifying meaning behind your text.",
|
56 |
+
max_lines=10,
|
57 |
+
)
|
58 |
+
gr.ClearButton(box)
|
59 |
+
with gr.Column():
|
60 |
+
outputs = gr.Label(
|
61 |
+
value="π΄ nothing to show yet...",
|
62 |
+
num_top_classes=3,
|
63 |
+
label="results",
|
64 |
+
)
|
65 |
+
btn = gr.Button("Classify")
|
66 |
+
# pylint: disable=no-member
|
67 |
+
btn.click(predict_sentiment, inputs=[box], outputs=[outputs])
|
68 |
+
# pylint: enable=no-member
|
69 |
+
gr.Markdown("Choose some ideas from below and see what it brings you back:")
|
70 |
+
gr.Examples(
|
71 |
+
[
|
72 |
+
"I love you.",
|
73 |
+
"Do you wanna go eat something with us?",
|
74 |
+
"Go away!",
|
75 |
+
"Amazing work, I see some improvements to make though.",
|
76 |
+
"Are you out of your mind!?",
|
77 |
+
"I can't shake off this constant feeling of worry and fear. It's affecting my daily life, and I don't know how to cope.",
|
78 |
+
"I can't help but feel like I'm not good enough. No matter what I do, it feels like I'm always falling short.",
|
79 |
+
"I'm so tired of feeling like this. I just want to feel normal again.",
|
80 |
+
"I feel like I'm going crazy. I can't stop thinking about all the things that could go wrong.",
|
81 |
+
],
|
82 |
+
inputs=[box],
|
83 |
+
)
|
84 |
+
|
85 |
+
|
86 |
+
if __name__ == "__main__":
|
87 |
+
demo.launch()
|
src/twitter_roberta.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Module for handling Twitter RoBERTa model loading and sentiment prediction."""
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
from scipy.special import softmax
|
5 |
+
from transformers import AutoConfig, AutoModelForSequenceClassification, AutoTokenizer
|
6 |
+
|
7 |
+
# Load tokenizer and model
|
8 |
+
MODEL = "cardiffnlp/twitter-roberta-base-sentiment-latest"
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
10 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
|
11 |
+
config = AutoConfig.from_pretrained(MODEL)
|
12 |
+
|
13 |
+
|
14 |
+
def preprocess(text: str) -> str:
|
15 |
+
"""Preprocess the input text by replacing user mentions and URLs."""
|
16 |
+
return " ".join(
|
17 |
+
[
|
18 |
+
"@user" if t.startswith("@") else "http" if t.startswith("http") else t
|
19 |
+
for t in text.split()
|
20 |
+
],
|
21 |
+
)
|
22 |
+
|
23 |
+
|
24 |
+
def predict_sentiment(text: str) -> dict:
|
25 |
+
"""Predict the sentiment of the given text using the RoBERTa model."""
|
26 |
+
text = preprocess(text)
|
27 |
+
encoded_input = tokenizer(text, return_tensors="pt")
|
28 |
+
output = model(**encoded_input)
|
29 |
+
scores = output[0][0].detach().numpy()
|
30 |
+
scores = softmax(scores)
|
31 |
+
ranking = np.argsort(scores)[::-1]
|
32 |
+
return {config.id2label[rank]: np.round(float(scores[rank]), 4) for rank in ranking}
|