sankalpit commited on
Commit
e0a9d03
·
verified ·
1 Parent(s): f4de5d3

Create app.py

Browse files

Add app.py directly as remote push is failing

Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import gradio as gr
3
+ from textblob import TextBlob
4
+
5
+ def call_model(text: str, model_type: str = "textblob"):
6
+ """
7
+ Return raw sentiment analysis output from selected model.
8
+ """
9
+ if model_type == "textblob":
10
+ blob = TextBlob(text)
11
+ return blob.sentiment # returns namedtuple(polarity, subjectivity)
12
+
13
+ elif model_type == "transformer":
14
+ # Placeholder for future integration
15
+ return {"label": "POSITIVE", "score": 0.98}
16
+
17
+ else:
18
+ raise ValueError(f"Unsupported model type: {model_type}")
19
+
20
+
21
+ def sentiment_analysis(text: str) -> str:
22
+ """
23
+ Analyze the sentiment of the given text.
24
+
25
+ Args:
26
+ text (str): The text to analyze
27
+
28
+ Returns:
29
+ str: A JSON string containing polarity, subjectivity, and assessment
30
+ """
31
+ sentiment = call_model(text, model_type="textblob")
32
+
33
+ # Handle TextBlob response (namedtuple)
34
+ if isinstance(sentiment, tuple): # Simple check for TextBlob style
35
+ polarity = round(sentiment.polarity, 2)
36
+ subjectivity = round(sentiment.subjectivity, 2)
37
+ assessment = (
38
+ "positive" if polarity > 0 else
39
+ "negative" if polarity < 0 else
40
+ "neutral"
41
+ )
42
+ result = {
43
+ "polarity": polarity,
44
+ "subjectivity": subjectivity,
45
+ "assessment": assessment
46
+ }
47
+ else:
48
+ # Future: handle ML-based sentiment output
49
+ result = sentiment
50
+
51
+ return json.dumps(result)
52
+
53
+ # Create the Gradio interface
54
+ demo = gr.Interface(
55
+ fn=sentiment_analysis,
56
+ inputs=gr.Textbox(placeholder="Enter text to analyze..."),
57
+ outputs=gr.Textbox(), # Changed from gr.JSON() to gr.Textbox()
58
+ title="Text Sentiment Analysis",
59
+ description="Analyze the sentiment of text using TextBlob"
60
+ )
61
+
62
+ # Launch the interface and MCP server
63
+ if __name__ == "__main__":
64
+ demo.launch(mcp_server=True)