Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +24 -0
- my_server.py +48 -0
- requirements.txt +3 -0
Dockerfile
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
# Create a new user - myuser
|
| 4 |
+
RUN useradd -ms /bin/bash myuser
|
| 5 |
+
|
| 6 |
+
# Switch to that user
|
| 7 |
+
USER myuser
|
| 8 |
+
|
| 9 |
+
# Create a .cache directory for Hugging Face transformers
|
| 10 |
+
# This is necessary to avoid permission issues when running the container as a non-root user,
|
| 11 |
+
# especially when using Hugging Face libraries.
|
| 12 |
+
# The directory will be used to store cached files, models, etc.
|
| 13 |
+
# The permissions are set to allow read/write access for the user.
|
| 14 |
+
RUN mkdir -p /home/myuser/.cache && chmod -R 755 /home/myuser/.cache
|
| 15 |
+
|
| 16 |
+
ADD requirements.txt .
|
| 17 |
+
|
| 18 |
+
RUN pip install -r requirements.txt
|
| 19 |
+
|
| 20 |
+
ADD my_server.py .
|
| 21 |
+
|
| 22 |
+
EXPOSE 8000
|
| 23 |
+
|
| 24 |
+
CMD ["python", "my_server.py"]
|
my_server.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
from typing import List
|
| 3 |
+
from mcp.server.fastmcp import FastMCP
|
| 4 |
+
|
| 5 |
+
# Load Sentiment Pipeline from HuggingFace
|
| 6 |
+
sentiment_pipeline = pipeline("sentiment-analysis")
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# Create an MCP server
|
| 10 |
+
mcp = FastMCP("Second-MCP-Server")
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
#### Tool ####
|
| 14 |
+
# Tool to do sentiment analysis for a list of sentences
|
| 15 |
+
@mcp.tool()
|
| 16 |
+
def sentiment_analyzer(sentences: List[str]) -> List[dict]:
|
| 17 |
+
"""
|
| 18 |
+
Analyzes the sentiment of a list of input sentences using a preloaded sentiment analysis pipeline.
|
| 19 |
+
Args:
|
| 20 |
+
sentences (List[str]): A list of input strings to be analyzed.
|
| 21 |
+
Returns:
|
| 22 |
+
List[dict]: A list of dictionaries, each containing:
|
| 23 |
+
- 'text' (str): The original input sentence.
|
| 24 |
+
- 'sentiment' (str): The predicted sentiment label (e.g., 'POSITIVE', 'NEGATIVE', etc.).
|
| 25 |
+
|
| 26 |
+
Example:
|
| 27 |
+
sentiment_analyzer(["I love this!", "This is terrible."])
|
| 28 |
+
[{'text': 'I love this!', 'sentiment': 'POSITIVE'},
|
| 29 |
+
{'text': 'This is terrible.', 'sentiment': 'NEGATIVE'}]
|
| 30 |
+
"""
|
| 31 |
+
result = sentiment_pipeline(sentences)
|
| 32 |
+
sentiments = []
|
| 33 |
+
for i in range(len(sentences)):
|
| 34 |
+
sentiments.append({'text': sentences[i],
|
| 35 |
+
'sentiment': result[i]['label']})
|
| 36 |
+
|
| 37 |
+
return sentiments
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
#### Prompt ####
|
| 41 |
+
@mcp.prompt()
|
| 42 |
+
def review_code(sentences: List[str]) -> str:
|
| 43 |
+
return f"Analyze the sentiment of the following sentences:\n\n{sentences}"
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
# Initialize and run the server
|
| 48 |
+
mcp.run(transport='sse')
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
gradio[mcp]
|