Tangineer commited on
Commit
32cbf01
·
verified ·
1 Parent(s): d157df7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Hugging face spaces 需要一个 app.py 文件来构建 space。因此,Python 文件的名称必须是 app.py
3
+ """
4
+
5
+ import gradio as gr
6
+ from textblob import TextBlob
7
+
8
+ def sentiment_analysis(text: str) -> dict:
9
+ """
10
+ Analyze the sentiment of the given text.
11
+
12
+ Args:
13
+ text (str): The text to analyze
14
+
15
+ Returns:
16
+ dict: A dictionary containing polarity, subjectivity, and assessment
17
+ """
18
+ blob = TextBlob(text)
19
+ sentiment = blob.sentiment
20
+
21
+ return {
22
+ "polarity": round(sentiment.polarity, 2), # -1 (negative) to 1 (positive)
23
+ "subjectivity": round(sentiment.subjectivity, 2), # 0 (objective) to 1 (subjective)
24
+ "assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral"
25
+ }
26
+
27
+ # Create the Gradio interface
28
+ demo = gr.Interface(
29
+ fn=sentiment_analysis,
30
+ inputs=gr.Textbox(placeholder="Enter text to analyze..."),
31
+ outputs=gr.JSON(),
32
+ title="Text Sentiment Analysis",
33
+ description="Analyze the sentiment of text using TextBlob"
34
+ )
35
+
36
+ # Launch the interface and MCP server
37
+ if __name__ == "__main__":
38
+ demo.launch(mcp_server=True)