Derfel2025 commited on
Commit
3ef4419
·
1 Parent(s): c0a76b8

Add application file

Browse files
Files changed (1) hide show
  1. app.py +37 -0
app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #python -m venv venv
2
+ #source venv/Scripts/activate - for windows path file
3
+ import gradio as gr
4
+ from textblob import TextBlob
5
+
6
+ #always use type hints, and provide Args: in order to help gradio
7
+ #generate mcp documentation
8
+
9
+ def sentiment_analysis(text: str) -> dict:
10
+ """Analyse the sentiment of the given text
11
+
12
+ Args:
13
+ text (str): The text to analyse
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),
23
+ "subjectivity": round(sentiment.subjectivity, 2),
24
+ "assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral"
25
+ }
26
+
27
+ demo = gr.Interface(
28
+ fn=sentiment_analysis,
29
+ inputs=gr.Textbox(placeholder="Enter text to analyse.."),
30
+ outputs=gr.JSON(),
31
+ title="Text Sentiment Analysis",
32
+ description="Analyse the sentiment of text using TextBlob"
33
+ )
34
+
35
+ if __name__ == "__main__":
36
+ demo.launch(mcp_server=True)
37
+ #need a write access token from hugging face in order to push to git, if you push to a gi repo rather than adding code in hugging face manually