Spaces:
Sleeping
Sleeping
molokhovdmitry
commited on
Commit
•
af5df29
1
Parent(s):
d684f95
Add emotions model
Browse files- .gitignore +2 -0
- main.py +29 -6
- models.py +10 -0
- requirements.txt +5 -0
- yt_api.py +5 -4
.gitignore
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
# Byte-compiled / optimized / DLL files
|
2 |
__pycache__/
|
3 |
*.py[cod]
|
|
|
1 |
+
example_notebook.ipynb
|
2 |
+
|
3 |
# Byte-compiled / optimized / DLL files
|
4 |
__pycache__/
|
5 |
*.py[cod]
|
main.py
CHANGED
@@ -1,6 +1,9 @@
|
|
1 |
-
from fastapi import FastAPI
|
2 |
-
from yt_api import get_comments
|
3 |
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
|
|
|
|
|
|
|
4 |
|
5 |
|
6 |
class Settings(BaseSettings):
|
@@ -11,8 +14,7 @@ class Settings(BaseSettings):
|
|
11 |
settings = Settings()
|
12 |
app = FastAPI(title='social-stat')
|
13 |
|
14 |
-
|
15 |
-
YT_API_KEY = settings.YT_API_KEY
|
16 |
|
17 |
|
18 |
@app.get('/')
|
@@ -20,6 +22,27 @@ def home():
|
|
20 |
return 'social-stat'
|
21 |
|
22 |
|
23 |
-
@app.
|
24 |
def predict(video_id):
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Response
|
|
|
2 |
from pydantic_settings import BaseSettings, SettingsConfigDict
|
3 |
+
import pandas as pd
|
4 |
+
|
5 |
+
from yt_api import get_comments
|
6 |
+
from models import init_emotions_model
|
7 |
|
8 |
|
9 |
class Settings(BaseSettings):
|
|
|
14 |
settings = Settings()
|
15 |
app = FastAPI(title='social-stat')
|
16 |
|
17 |
+
emotions_clf = init_emotions_model()
|
|
|
18 |
|
19 |
|
20 |
@app.get('/')
|
|
|
22 |
return 'social-stat'
|
23 |
|
24 |
|
25 |
+
@app.get('/predict')
|
26 |
def predict(video_id):
|
27 |
+
# Get comments
|
28 |
+
comments = get_comments(video_id, settings.YT_API_KEY)
|
29 |
+
comments_df = pd.DataFrame(comments)
|
30 |
+
|
31 |
+
# Predict emotions
|
32 |
+
text_list = comments_df['text_display'].to_list()
|
33 |
+
preds = emotions_clf(text_list)
|
34 |
+
|
35 |
+
# Add predictions to DataFrame
|
36 |
+
preds_df = []
|
37 |
+
for pred in preds:
|
38 |
+
pred_dict = {}
|
39 |
+
for emotion in pred:
|
40 |
+
pred_dict[emotion['label']] = emotion['score']
|
41 |
+
preds_df.append(pred_dict)
|
42 |
+
preds_df = pd.DataFrame(preds_df)
|
43 |
+
comments_df = pd.concat([comments_df, preds_df], axis=1)
|
44 |
+
|
45 |
+
# Return DataFrame as a JSON file
|
46 |
+
return Response(
|
47 |
+
content=comments_df.to_json(orient='records'),
|
48 |
+
media_type='application/json')
|
models.py
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
|
3 |
+
|
4 |
+
def init_emotions_model():
|
5 |
+
classifier = pipeline(
|
6 |
+
task="text-classification",
|
7 |
+
model="SamLowe/roberta-base-go_emotions",
|
8 |
+
top_k=None)
|
9 |
+
|
10 |
+
return classifier
|
requirements.txt
CHANGED
@@ -2,3 +2,8 @@ requests
|
|
2 |
fastapi
|
3 |
uvicorn
|
4 |
pydantic_settings
|
|
|
|
|
|
|
|
|
|
|
|
2 |
fastapi
|
3 |
uvicorn
|
4 |
pydantic_settings
|
5 |
+
torch
|
6 |
+
torchvision
|
7 |
+
torchaudio
|
8 |
+
transformers
|
9 |
+
pandas
|
yt_api.py
CHANGED
@@ -13,7 +13,7 @@ def get_comments(video_id, api_key):
|
|
13 |
while 'nextPageToken' in response.keys():
|
14 |
response = get_response(
|
15 |
video_id, api_key, page_token=response['nextPageToken'])
|
16 |
-
comment_list
|
17 |
|
18 |
return comment_list
|
19 |
|
@@ -34,7 +34,7 @@ def get_response(video_id, api_key, page_token=None, max_results=100):
|
|
34 |
|
35 |
def response_to_comments(response):
|
36 |
"""Converts JSON response to `comment_list` dict."""
|
37 |
-
comment_list =
|
38 |
for comment in response['items']:
|
39 |
comment = comment['snippet']
|
40 |
can_reply = comment['canReply']
|
@@ -43,7 +43,8 @@ def response_to_comments(response):
|
|
43 |
comment_id = comment['id']
|
44 |
comment = comment['snippet']
|
45 |
try:
|
46 |
-
comment_list
|
|
|
47 |
'video_id': comment['videoId'],
|
48 |
'channel_id': comment['authorChannelId']['value'],
|
49 |
'author_display_name': comment['authorDisplayName'],
|
@@ -54,7 +55,7 @@ def response_to_comments(response):
|
|
54 |
'like_count': comment['likeCount'],
|
55 |
'can_reply': can_reply,
|
56 |
'total_reply_count': total_reply_count,
|
57 |
-
}
|
58 |
except Exception as e:
|
59 |
print(f"Error: {e}\nComment: {comment}")
|
60 |
continue
|
|
|
13 |
while 'nextPageToken' in response.keys():
|
14 |
response = get_response(
|
15 |
video_id, api_key, page_token=response['nextPageToken'])
|
16 |
+
comment_list += (response_to_comments(response))
|
17 |
|
18 |
return comment_list
|
19 |
|
|
|
34 |
|
35 |
def response_to_comments(response):
|
36 |
"""Converts JSON response to `comment_list` dict."""
|
37 |
+
comment_list = []
|
38 |
for comment in response['items']:
|
39 |
comment = comment['snippet']
|
40 |
can_reply = comment['canReply']
|
|
|
43 |
comment_id = comment['id']
|
44 |
comment = comment['snippet']
|
45 |
try:
|
46 |
+
comment_list.append({
|
47 |
+
'comment_id': comment_id,
|
48 |
'video_id': comment['videoId'],
|
49 |
'channel_id': comment['authorChannelId']['value'],
|
50 |
'author_display_name': comment['authorDisplayName'],
|
|
|
55 |
'like_count': comment['likeCount'],
|
56 |
'can_reply': can_reply,
|
57 |
'total_reply_count': total_reply_count,
|
58 |
+
})
|
59 |
except Exception as e:
|
60 |
print(f"Error: {e}\nComment: {comment}")
|
61 |
continue
|