Spaces:
Sleeping
Sleeping
File size: 4,934 Bytes
033b96e 5ac4fa3 033b96e 96606a5 033b96e 96606a5 b8eb8a3 5ac4fa3 b8eb8a3 5ac4fa3 b8eb8a3 c0b77e8 96606a5 b8eb8a3 96606a5 b8eb8a3 96606a5 5268ae5 b8eb8a3 5268ae5 96606a5 033b96e b8eb8a3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
import json
import os
import gradio as gr
from textblob import TextBlob
from snownlp import SnowNLP
def sentiment_analysis(text: str) -> str:
'''
Analyse the sentiment of the given text
Args:
text (str): The text to analyse
Returns:
str: A JSON string containing polarity, subjectivity, and assessment
'''
blob = TextBlob(text)
sentiment = blob.sentiment
result = {
'polarity': round(sentiment.polarity, 2), # -1 (negative) to 1 (positive)
'subjectivity': round(sentiment.subjectivity, 2), # 0 (objective) to 1 (subjective)
'assessment': 'positive' if sentiment.polarity > 0 else 'negative' if sentiment.polarity < 0 else 'neutral'
}
return json.dumps(result)
def chinese_sentiment_analysis(text: str) -> str:
'''
Analyse the sentiment of the given Chinese text
Args:
text (str): The text to analyse
Returns:
str: A JSON string containing polarity, subjectivity, and assessment
'''
s = SnowNLP(text)
# SnowNLP 的情感分析返回值範圍是 0 到 1,0 表示負面,1 表示正面
polarity = s.sentiments
subjectivity = None # SnowNLP 不提供主觀性評估,可設為 None 或其他值
result = {
'polarity': round(polarity, 2), # 0 (negative) to 1 (positive)
'subjectivity': subjectivity, # SnowNLP 不提供主觀性評估
'assessment': 'positive' if polarity > 0.5 else 'negative' if polarity < 0.5 else 'neutral'
}
return json.dumps(result)
def batch_sentiment_analysis(file_path: str) -> str:
'''
Batch process sentiment analysis from JSON file
Args:
file_path (str): Path to JSON file with {text: ''} format
Returns:
str: JSON string with analysis results in values
'''
with open(file_path, 'r', encoding = 'utf-8') as f:
data = json.load(f)
for key in data:
analysis_result = json.loads( sentiment_analysis(key) )
data[key] = analysis_result
dir_name = os.path.dirname(file_path)
base_name = os.path.basename(file_path)
output_path = os.path.join(dir_name, f'processed_{base_name}')
with open(output_path, 'w', encoding = 'utf-8') as f:
json.dump(data, f, ensure_ascii = False, indent = 2)
return output_path
def batch_chinese_sentiment_analysis(file_path: str) -> str:
'''
Batch process Chinese sentiment analysis from JSON file
Args:
file_path (str): Path to JSON file with {text: ''} format
Returns:
str: JSON string with analysis results in values
'''
with open(file_path, 'r', encoding = 'utf-8') as f:
data = json.load(f)
for key in data:
analysis_result = json.loads( chinese_sentiment_analysis(key) )
data[key] = analysis_result
dir_name = os.path.dirname(file_path)
base_name = os.path.basename(file_path)
output_path = os.path.join(dir_name, f'processed_{base_name}')
with open(output_path, 'w', encoding = 'utf-8') as f:
json.dump(data, f, ensure_ascii = False, indent = 2)
return output_path
# gradio interface
demo = gr.TabbedInterface(
[
gr.Interface(
fn = sentiment_analysis,
inputs = gr.Textbox(placeholder = 'Enter text to analyse...'),
outputs = gr.Textbox(),
title = 'Text Sentiment Analysis',
description = 'Analyse the sentiment of text using TextBlob',
api_name = 'sentiment_analysis'
),
gr.Interface(
fn = chinese_sentiment_analysis,
inputs = gr.Textbox(placeholder = '要分析的中文...'),
outputs = gr.Textbox(),
title = '中文情感分析',
description = 'Analyse the sentiment of Chinese text using SnowNLP',
api_name = 'chinese_sentiment_analysis'
),
gr.Interface(
fn = batch_sentiment_analysis,
inputs = gr.File(label = 'Upload JSON File'),
outputs = gr.File(label = 'Download Results'),
title = 'Batch Sentiment Analysis',
description = 'Process JSON file with multiple texts (English)',
api_name = 'batch_sentiment_analysis'
),
gr.Interface(
fn = batch_chinese_sentiment_analysis,
inputs = gr.File(label = '上傳JSON文件'),
outputs = gr.File(label = '下載分析結果'),
title = '批量中文情感分析',
description = 'Batch process Chinese sentiment analysis from JSON file',
api_name = 'batch_chinese_sentiment_analysis'
)
],
[
'sentiment analysis',
'中文情感分析',
'batch processing',
'批次中文情感分析'
]
)
# Launch the interface and MCP server
if __name__ == '__main__':
demo.launch(mcp_server = True)
|