Spaces:
Runtime error
Runtime error
Upload 5 files
Browse files- Dockerfile +64 -0
- main.py +47 -0
- requirements.txt +7 -0
- test_main.py +35 -0
- test_results.txt +9 -0
Dockerfile
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# syntax=docker/dockerfile:1
|
2 |
+
|
3 |
+
# Comments are provided throughout this file to help you get started.
|
4 |
+
# If you need more help, visit the Dockerfile reference guide at
|
5 |
+
# https://docs.docker.com/go/dockerfile-reference/
|
6 |
+
|
7 |
+
# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7
|
8 |
+
|
9 |
+
ARG PYTHON_VERSION=3.12.3
|
10 |
+
FROM python:3.12.3-slim as base
|
11 |
+
|
12 |
+
# Prevents Python from writing pyc files.
|
13 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
14 |
+
|
15 |
+
# Keeps Python from buffering stdout and stderr to avoid situations where
|
16 |
+
# the application crashes without emitting any logs due to buffering.
|
17 |
+
ENV PYTHONUNBUFFERED=1
|
18 |
+
|
19 |
+
WORKDIR /app
|
20 |
+
|
21 |
+
# Copy the requirements file into the container at /app
|
22 |
+
#COPY requirements.txt .
|
23 |
+
|
24 |
+
# Create a non-privileged user that the app will run under.
|
25 |
+
# See https://docs.docker.com/go/dockerfile-user-best-practices/
|
26 |
+
ARG UID=10001
|
27 |
+
RUN adduser \
|
28 |
+
--disabled-password \
|
29 |
+
--gecos "" \
|
30 |
+
--home "/nonexistent" \
|
31 |
+
--shell "/sbin/nologin" \
|
32 |
+
--no-create-home \
|
33 |
+
--uid "${UID}" \
|
34 |
+
appuser
|
35 |
+
|
36 |
+
# Download dependencies as a separate step to take advantage of Docker's caching.
|
37 |
+
# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
|
38 |
+
# Leverage a bind mount to requirements.txt to avoid having to copy them into
|
39 |
+
# into this layer.
|
40 |
+
RUN --mount=type=cache,target=/root/.cache/pip \
|
41 |
+
--mount=type=bind,source=requirements.txt,target=requirements.txt \
|
42 |
+
python -m pip install -r requirements.txt
|
43 |
+
# Install the dependencies from the requirements.txt file
|
44 |
+
|
45 |
+
# Install pyngrok
|
46 |
+
#RUN pip install pyngrok
|
47 |
+
|
48 |
+
# Switch to the non-privileged user to run the application.
|
49 |
+
USER appuser
|
50 |
+
|
51 |
+
# Set the TRANSFORMERS_CACHE environment variable
|
52 |
+
ENV TRANSFORMERS_CACHE=/tmp/.cache/huggingface
|
53 |
+
|
54 |
+
# Create the cache folder with appropriate permissions
|
55 |
+
RUN mkdir -p $TRANSFORMERS_CACHE && chmod -R 777 $TRANSFORMERS_CACHE
|
56 |
+
|
57 |
+
# Copy the source code into the container.
|
58 |
+
COPY . .
|
59 |
+
|
60 |
+
# Expose the port that the application listens on.
|
61 |
+
EXPOSE 8000
|
62 |
+
|
63 |
+
# Run the application.
|
64 |
+
CMD uvicorn 'main:app' --host=0.0.0.0 --port=8000
|
main.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from transformers import pipeline
|
3 |
+
from pydantic import BaseModel
|
4 |
+
import nest_asyncio
|
5 |
+
#from pyngrok import ngrok
|
6 |
+
from fastapi.responses import RedirectResponse
|
7 |
+
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
# Load the model
|
11 |
+
text_classifier = pipeline("text-classification", model="mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis")
|
12 |
+
|
13 |
+
# Input data model
|
14 |
+
class TextInput(BaseModel):
|
15 |
+
text: str
|
16 |
+
|
17 |
+
@app.post('/analyze')
|
18 |
+
async def Predict_Sentiment(text_input: TextInput):
|
19 |
+
text = text_input.text
|
20 |
+
|
21 |
+
# Validate input text
|
22 |
+
if not text.strip(): # Check if text is empty or contains only whitespace
|
23 |
+
raise HTTPException(status_code=400, detail="Input text is empty or contains only whitespace.")
|
24 |
+
elif text.strip() == "--": # Check if text is "--"
|
25 |
+
raise HTTPException(status_code=400, detail="Invalid input text.")
|
26 |
+
elif text.isdigit(): # Check if text contains only digits
|
27 |
+
raise HTTPException(status_code=400, detail="Input text contains only digits.")
|
28 |
+
elif not any(c.isalpha() for c in text): # Check if text contains any alphabetic characters
|
29 |
+
raise HTTPException(status_code=400, detail="Input text contains no alphabetic characters.")
|
30 |
+
|
31 |
+
# Perform sentiment analysis
|
32 |
+
try:
|
33 |
+
return text_classifier(text)
|
34 |
+
except Exception as e:
|
35 |
+
raise HTTPException(status_code=500, detail=str(e))
|
36 |
+
|
37 |
+
@app.get('/')
|
38 |
+
async def html():
|
39 |
+
return "Welcome to Financial Sentiment Analysis API"
|
40 |
+
|
41 |
+
|
42 |
+
#ngrok_tunnel = ngrok.connect(8000)
|
43 |
+
#print('Public URL:', ngrok_tunnel.public_url)
|
44 |
+
nest_asyncio.apply()
|
45 |
+
#uvicorn.run(app, port=8000)
|
46 |
+
|
47 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn
|
3 |
+
pydantic
|
4 |
+
transformers
|
5 |
+
nest-asyncio
|
6 |
+
torch
|
7 |
+
pytest
|
test_main.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi.testclient import TestClient
|
2 |
+
from main import app
|
3 |
+
from main import TextInput
|
4 |
+
|
5 |
+
client = TestClient(app)
|
6 |
+
|
7 |
+
def test_positive_sentiment():
|
8 |
+
response = client.post("/analyze", json={"text": "Profits are up by 5 million this year."})
|
9 |
+
assert response.status_code == 200
|
10 |
+
# Ensure that the response is parsed as a list of dictionaries
|
11 |
+
data_list = response.json()
|
12 |
+
assert isinstance(data_list, list)
|
13 |
+
# Check if the first item in the list has a "label" key
|
14 |
+
assert data_list[0]["label"] == "positive"
|
15 |
+
|
16 |
+
def test_negative_sentiment():
|
17 |
+
response = client.post("/analyze", json={"text": "Operating profit totaled EUR 9.4 mn , down from EUR 11.7 mn in 2004 ."})
|
18 |
+
assert response.status_code == 200
|
19 |
+
# Ensure that the response is parsed as a list of dictionaries
|
20 |
+
data_list = response.json()
|
21 |
+
assert isinstance(data_list, list)
|
22 |
+
# Check if the first item in the list has a "label" key
|
23 |
+
assert data_list[0]["label"] == "negative"
|
24 |
+
|
25 |
+
def test_neutral_sentiment():
|
26 |
+
response = client.post("/analyze", json={"text": "The profits are same as last year"})
|
27 |
+
assert response.status_code == 200
|
28 |
+
# Ensure that the response is parsed as a list of dictionaries
|
29 |
+
data_list = response.json()
|
30 |
+
assert isinstance(data_list, list)
|
31 |
+
# Check if the first item in the list has a "label" key
|
32 |
+
assert data_list[0]["label"] == "neutral"
|
33 |
+
|
34 |
+
|
35 |
+
|
test_results.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
============================= test session starts =============================
|
2 |
+
platform win32 -- Python 3.12.3, pytest-8.2.0, pluggy-1.5.0
|
3 |
+
rootdir: C:\Users\Shahab\Desktop\Fin-Data-Sentiment-API
|
4 |
+
plugins: anyio-4.3.0
|
5 |
+
collected 3 items
|
6 |
+
|
7 |
+
test_main.py ... [100%]
|
8 |
+
|
9 |
+
============================== 3 passed in 7.56s ==============================
|