mgbam commited on
Commit
1a49f35
·
verified ·
1 Parent(s): 6e69305

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -20
app.py CHANGED
@@ -1,26 +1,35 @@
 
1
  import gradio as gr
2
- import os
3
 
4
- from smolagents import InferenceClientModel, CodeAgent, MCPClient
 
 
 
 
 
 
 
 
 
5
 
 
 
 
 
 
6
 
7
- try:
8
- mcp_client = MCPClient(
9
- {"url": "https://abidlabs-mcp-tool-http.hf.space/gradio_api/mcp/sse"}
10
- )
11
- tools = mcp_client.get_tools()
12
 
13
- model = InferenceClientModel(token=os.getenv("HUGGINGFACE_API_TOKEN"))
14
- agent = CodeAgent(tools=[*tools], model=model, additional_authorized_imports=["json", "ast", "urllib", "base64"])
 
 
 
 
 
 
15
 
16
- demo = gr.ChatInterface(
17
- fn=lambda message, history: str(agent.run(message)),
18
- type="messages",
19
- examples=["Analyze the sentiment of the following text 'This is awesome'"],
20
- title="Agent with MCP Tools",
21
- description="This is a simple agent that uses MCP tools to answer questions.",
22
- )
23
-
24
- demo.launch()
25
- finally:
26
- mcp_client.disconnect()
 
1
+ import json
2
  import gradio as gr
3
+ from textblob import TextBlob
4
 
5
+ def sentiment_analysis(text: str) -> str:
6
+ """
7
+ Analyze the sentiment of the given text.
8
+ Args:
9
+ text (str): The text to analyze
10
+ Returns:
11
+ str: A JSON string containing polarity, subjectivity, and assessment
12
+ """
13
+ blob = TextBlob(text)
14
+ sentiment = blob.sentiment
15
 
16
+ result = {
17
+ "polarity": round(sentiment.polarity, 2), # -1 (negative) to 1 (positive)
18
+ "subjectivity": round(sentiment.subjectivity, 2), # 0 (objective) to 1 (subjective)
19
+ "assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral"
20
+ }
21
 
22
+ return json.dumps(result)
 
 
 
 
23
 
24
+ # Create the Gradio interface
25
+ demo = gr.Interface(
26
+ fn=sentiment_analysis,
27
+ inputs=gr.Textbox(placeholder="Enter text to analyze..."),
28
+ outputs=gr.Textbox(), # Changed from gr.JSON() to gr.Textbox()
29
+ title="Text Sentiment Analysis",
30
+ description="Analyze the sentiment of text using TextBlob"
31
+ )
32
 
33
+ # Launch the interface and MCP server
34
+ if __name__ == "__main__":
35
+ demo.launch(mcp_server=True)