Spaces:
Sleeping
Sleeping
翟宇翔
commited on
Commit
·
dcef9d0
1
Parent(s):
3993ee1
提交app.py和requirements.txt
Browse files- .gitignore +1 -0
- app.py +45 -0
- requirements.txt +2 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
/venv
|
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from textblob import TextBlob
|
| 3 |
+
|
| 4 |
+
# 此段函数接受文本输入并返回一个字典,包含三项信息:极性(polarity)、主观性(subjectivity)、评价(assessment)
|
| 5 |
+
def sentiment_analysis(text: str) -> dict:
|
| 6 |
+
#以下字符串非常重要,该字符串协助Gradio生成MCP工具模式
|
| 7 |
+
"""
|
| 8 |
+
Perform sentiment analysis on input text using TextBlob library.
|
| 9 |
+
|
| 10 |
+
The function calculates polarity (sentiment score from -1 to 1) and subjectivity
|
| 11 |
+
(how opinionated the text is from 0 to 1), then provides a human-readable assessment.
|
| 12 |
+
|
| 13 |
+
Args:
|
| 14 |
+
text (str): Input text to be analyzed for sentiment. Should be in English for best results.
|
| 15 |
+
|
| 16 |
+
Returns:
|
| 17 |
+
dict: Dictionary containing three keys:
|
| 18 |
+
- polarity (float): Sentiment score between -1.0 (negative) and 1.0 (positive)
|
| 19 |
+
- subjectivity (float): Score between 0.0 (objective) and 1.0 (subjective)
|
| 20 |
+
- assessment (str): Categorical classification ('positive', 'negative', or 'neutral')
|
| 21 |
+
"""
|
| 22 |
+
# 使用TextBlob分析文本情感
|
| 23 |
+
blob = TextBlob(text)
|
| 24 |
+
sentiment = blob.sentiment
|
| 25 |
+
|
| 26 |
+
# Format and return sentiment analysis results
|
| 27 |
+
return {
|
| 28 |
+
"polarity": round(sentiment.polarity, 2), # -1 (negative) to 1 (positive)
|
| 29 |
+
"subjectivity": round(sentiment.subjectivity, 2), # 0 (objective) to 1 (subjective)
|
| 30 |
+
"assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral"
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
# gr.Interface 同时创建网页用户界面和 MCP 服务器
|
| 34 |
+
demo = gr.Interface(
|
| 35 |
+
fn=sentiment_analysis, # 调用前面定义的sentiment_analysis方法
|
| 36 |
+
inputs=gr.Textbox(placeholder="Enter text to analyze..."), # 输入和输出组件定义工具的模式
|
| 37 |
+
outputs=gr.JSON(), # JSON 输出组件确保正确的序列化
|
| 38 |
+
title="Text Sentiment Analysis", # Interface title
|
| 39 |
+
description="Analyze the sentiment of text using TextBlob" # Description below title
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Launch the web interface when script is run directly
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
# 置 mcp_server=True 启用 MCP 服务器
|
| 45 |
+
demo.launch(mcp_server=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio[mcp]
|
| 2 |
+
textblob
|