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)