SiddanthEmani commited on
Commit
4932931
·
1 Parent(s): 0de0bef

Fixed deployment of client.py

Browse files
Files changed (2) hide show
  1. client.py +27 -47
  2. requirements.txt +1 -3
client.py CHANGED
@@ -1,56 +1,36 @@
1
  import gradio as gr
2
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
- from smolagents import CodeAgent, InferenceClientModel
5
- from smolagents.mcp_client import MCPClient
6
-
7
- # Determine if we're running locally or on HF Spaces
8
- is_spaces = os.environ.get('SPACE_ID') is not None
9
-
10
- # Define the MCP URL based on environment
11
- mcp_url = "/gradio_api/mcp/sse" if is_spaces else "http://localhost:7860/gradio_api/mcp/sse"
12
-
13
- # Initialize the client outside the try block, but don't connect yet
14
- mcp_client = None
15
-
16
  def create_demo():
17
- # Creating the chatbot interface
18
- return gr.ChatInterface(
19
- fn=lambda message, history: str(agent.run(message)) if agent else "Agent not connected",
20
- type="messages",
21
- examples=["What is the capital of France?"],
22
- title="Agent with Gradio and MCP",
23
- description="Simple agent used for asking questions.",
24
- messages=[],
25
  theme="ocean"
26
  )
 
27
 
28
- # Initialize the demo outside of the try-except
29
- demo = None
30
 
31
- try:
32
- # Create and connect the MCP client
33
- mcp_client = MCPClient({"url": mcp_url})
34
- tools = mcp_client.get_tools()
35
- model = InferenceClientModel()
36
- agent = CodeAgent(tools=[*tools], model=model)
37
-
38
- # Create the chat interface
39
- demo = create_demo()
40
-
41
- except Exception as e:
42
- # If client connection fails, create a demo that shows the error
43
- print(f"Error connecting to MCP server: {e}")
44
- agent = None
45
- demo = create_demo()
46
-
47
- # Only close the client if it was successfully created
48
  def cleanup():
49
- if mcp_client:
50
- try:
51
- mcp_client.close()
52
- except:
53
- pass
54
-
55
- # In app.py, we only import the demo object,
56
- # so we don't need to call launch() here
 
1
  import gradio as gr
2
  import os
3
+ from textblob import TextBlob
4
+
5
+ # Create a simple function for sentiment analysis
6
+ def sentiment_analysis(message, history):
7
+ """
8
+ Perform sentiment analysis on the input text and return a formatted result.
9
+ """
10
+ blob = TextBlob(message)
11
+ sentiment = blob.sentiment
12
+
13
+ assessment = "Positive" if sentiment.polarity > 0 else "Negative" if sentiment.polarity < 0 else "Neutral"
14
+
15
+ return f"Sentiment Analysis: {assessment}\nPolarity: {sentiment.polarity:.2f}\nSubjectivity: {sentiment.subjectivity:.2f}"
16
 
17
+ # Create our demo interface
 
 
 
 
 
 
 
 
 
 
 
18
  def create_demo():
19
+ # Create a basic ChatInterface with just sentiment analysis
20
+ interface = gr.ChatInterface(
21
+ fn=sentiment_analysis,
22
+ examples=["I love this application, it's amazing!",
23
+ "I'm feeling a bit disappointed with the results.",
24
+ "This is just a neutral statement about facts."],
25
+ title="Sentiment Analysis Chatbot",
26
+ description="Ask me to analyze the sentiment of your text!",
27
  theme="ocean"
28
  )
29
+ return interface
30
 
31
+ # Create the demo
32
+ demo = create_demo()
33
 
34
+ # No cleanup needed for this simplified version
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  def cleanup():
36
+ pass
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,4 +1,2 @@
1
- gradio[mcp]
2
- smolagents[mcp]
3
- mcp
4
  textblob
 
1
+ gradio
 
 
2
  textblob