Spaces:
Sleeping
Sleeping
Commit
·
96606a5
1
Parent(s):
033b96e
add sentiment analysis for chinese via SnowNLP
Browse files- app.py +56 -6
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import json
|
2 |
import gradio as gr
|
3 |
from textblob import TextBlob
|
|
|
4 |
|
5 |
def sentiment_analysis(text: str) -> str:
|
6 |
'''
|
@@ -23,13 +24,62 @@ def sentiment_analysis(text: str) -> str:
|
|
23 |
|
24 |
return json.dumps(result)
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
# Gradio interface
|
27 |
-
demo = gr.Interface(
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
)
|
34 |
|
35 |
# Launch the interface and MCP server
|
|
|
1 |
import json
|
2 |
import gradio as gr
|
3 |
from textblob import TextBlob
|
4 |
+
from snownlp import SnowNLP
|
5 |
|
6 |
def sentiment_analysis(text: str) -> str:
|
7 |
'''
|
|
|
24 |
|
25 |
return json.dumps(result)
|
26 |
|
27 |
+
|
28 |
+
def chinese_sentiment_analysis(text: str) -> str:
|
29 |
+
'''
|
30 |
+
Analyse the sentiment of the given Chinese text
|
31 |
+
|
32 |
+
Args:
|
33 |
+
text (str): The text to analyse
|
34 |
+
|
35 |
+
Returns:
|
36 |
+
str: A JSON string containing polarity, subjectivity, and assessment
|
37 |
+
'''
|
38 |
+
s = SnowNLP(text)
|
39 |
+
|
40 |
+
# SnowNLP 的情感分析返回值範圍是 0 到 1,0 表示負面,1 表示正面
|
41 |
+
polarity = s.sentiments
|
42 |
+
subjectivity = None # SnowNLP 不提供主觀性評估,可設為 None 或其他值
|
43 |
+
|
44 |
+
result = {
|
45 |
+
'polarity': round(polarity, 2), # 0 (negative) to 1 (positive)
|
46 |
+
'subjectivity': subjectivity, # SnowNLP 不提供主觀性評估
|
47 |
+
'assessment': 'positive' if polarity > 0.5 else 'negative' if polarity < 0.5 else 'neutral'
|
48 |
+
}
|
49 |
+
|
50 |
+
return json.dumps(result)
|
51 |
+
|
52 |
# Gradio interface
|
53 |
+
# demo = gr.Interface(
|
54 |
+
# fn = sentiment_analysis,
|
55 |
+
# inputs = gr.Textbox(placeholder = 'Enter text to analyse...'),
|
56 |
+
# outputs = gr.Textbox(), # Changed from gr.JSON() to gr.Textbox()
|
57 |
+
# title = 'Text Sentiment Analysis',
|
58 |
+
# description = 'Analyse the sentiment of text using TextBlob'
|
59 |
+
# )
|
60 |
+
|
61 |
+
demo = gr.TabbedInterface(
|
62 |
+
[
|
63 |
+
gr.Interface(
|
64 |
+
fn = sentiment_analysis,
|
65 |
+
inputs = gr.Textbox(placeholder = 'Enter text to analyse...'),
|
66 |
+
outputs = gr.Textbox(),
|
67 |
+
title = 'Text Sentiment Analysis',
|
68 |
+
description = 'Analyse the sentiment of text using TextBlob',
|
69 |
+
api_name = 'sentiment_analysis'
|
70 |
+
),
|
71 |
+
gr.Interface(
|
72 |
+
fn = chinese_sentiment_analysis,
|
73 |
+
inputs = gr.Textbox(placeholder = '要分析的中文...'),
|
74 |
+
outputs = gr.Textbox(),
|
75 |
+
title = 'Chinese Sentiment Analysis',
|
76 |
+
description = 'Analyse the sentiment of Chinese text using SnowNLP',
|
77 |
+
api_name = 'chinese_sentiment_analysis'),
|
78 |
+
],
|
79 |
+
[
|
80 |
+
'sentiment analysis',
|
81 |
+
'chinese sentiment analysis',
|
82 |
+
]
|
83 |
)
|
84 |
|
85 |
# Launch the interface and MCP server
|
requirements.txt
CHANGED
@@ -1,2 +1,3 @@
|
|
1 |
gradio[mcp]
|
2 |
-
textblob
|
|
|
|
1 |
gradio[mcp]
|
2 |
+
textblob
|
3 |
+
snownlp
|