Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
main.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import string
|
| 3 |
+
import nltk
|
| 4 |
+
from fastapi import FastAPI, HTTPException
|
| 5 |
+
from pydantic import BaseModel
|
| 6 |
+
from typing import Optional
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
from pyngrok import ngrok
|
| 9 |
+
import nest_asyncio
|
| 10 |
+
from fastapi.responses import RedirectResponse
|
| 11 |
+
|
| 12 |
+
# Download NLTK resources
|
| 13 |
+
nltk.download('punkt')
|
| 14 |
+
nltk.download('wordnet')
|
| 15 |
+
|
| 16 |
+
# Initialize FastAPI app
|
| 17 |
+
app = FastAPI()
|
| 18 |
+
|
| 19 |
+
# Text preprocessing functions
|
| 20 |
+
def remove_urls(text):
|
| 21 |
+
return re.sub(r'http[s]?://\S+', '', text)
|
| 22 |
+
|
| 23 |
+
def remove_punctuation(text):
|
| 24 |
+
regular_punct = string.punctuation
|
| 25 |
+
return re.sub(r'['+regular_punct+']', '', text)
|
| 26 |
+
|
| 27 |
+
def lower_case(text):
|
| 28 |
+
return text.lower()
|
| 29 |
+
|
| 30 |
+
def lemmatize(text):
|
| 31 |
+
wordnet_lemmatizer = nltk.WordNetLemmatizer()
|
| 32 |
+
tokens = nltk.word_tokenize(text)
|
| 33 |
+
return ' '.join([wordnet_lemmatizer.lemmatize(w) for w in tokens])
|
| 34 |
+
|
| 35 |
+
# Model loading
|
| 36 |
+
lyx_pipe = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base")
|
| 37 |
+
|
| 38 |
+
# Input data model
|
| 39 |
+
class TextInput(BaseModel):
|
| 40 |
+
text: str
|
| 41 |
+
|
| 42 |
+
# Welcome endpoint
|
| 43 |
+
@app.get('/')
|
| 44 |
+
async def welcome():
|
| 45 |
+
# Redirect to the Swagger UI page
|
| 46 |
+
return RedirectResponse(url="/docs")
|
| 47 |
+
|
| 48 |
+
# Sentiment analysis endpoint
|
| 49 |
+
@app.post('/analyze/')
|
| 50 |
+
async def Predict_Sentiment(text_input: TextInput):
|
| 51 |
+
text = text_input.text
|
| 52 |
+
|
| 53 |
+
# Text preprocessing
|
| 54 |
+
text = remove_urls(text)
|
| 55 |
+
text = remove_punctuation(text)
|
| 56 |
+
text = lower_case(text)
|
| 57 |
+
text = lemmatize(text)
|
| 58 |
+
|
| 59 |
+
# Perform sentiment analysis
|
| 60 |
+
try:
|
| 61 |
+
return lyx_pipe(text)
|
| 62 |
+
except Exception as e:
|
| 63 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 64 |
+
|
| 65 |
+
# Run the FastAPI app using Uvicorn
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
# Create ngrok tunnel
|
| 68 |
+
ngrok_tunnel = ngrok.connect(7860)
|
| 69 |
+
print('Public URL:', ngrok_tunnel.public_url)
|
| 70 |
+
|
| 71 |
+
# Allow nested asyncio calls
|
| 72 |
+
nest_asyncio.apply()
|
| 73 |
+
|
| 74 |
+
# Run the FastAPI app with Uvicorn
|
| 75 |
+
import uvicorn
|
| 76 |
+
uvicorn.run(app, port=7860)
|
test.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi.testclient import TestClient
|
| 2 |
+
from main import app
|
| 3 |
+
|
| 4 |
+
client = TestClient(app)
|
| 5 |
+
|
| 6 |
+
def test_positive_sentiment():
|
| 7 |
+
response = client.post("/predict", json={"text": "I love this product! It's amazing."})
|
| 8 |
+
assert response.status_code == 200
|
| 9 |
+
assert response.json() == {"sentiment": "joy"} # Positive sentiment corresponds to joy
|
| 10 |
+
|
| 11 |
+
def test_negative_sentiment():
|
| 12 |
+
response = client.post("/predict", json={"text": "This product is terrible. I regret buying it."})
|
| 13 |
+
assert response.status_code == 200
|
| 14 |
+
assert response.json() == {"sentiment": "anger"} # Negative sentiment corresponds to anger
|
| 15 |
+
|
| 16 |
+
def test_neutral_sentiment():
|
| 17 |
+
response = client.post("/predict", json={"text": "This product is okay. It meets my expectations."})
|
| 18 |
+
assert response.status_code == 200
|
| 19 |
+
assert response.json() == {"sentiment": "neutral"} # Neutral sentiment remains unchanged
|
| 20 |
+
|
| 21 |
+
def test_anger_sentiment():
|
| 22 |
+
response = client.post("/predict", json={"text": "This product makes me furious!"})
|
| 23 |
+
assert response.status_code == 200
|
| 24 |
+
assert response.json() == {"sentiment": "anger"} # Emotion of anger
|
| 25 |
+
|
| 26 |
+
def test_disgust_sentiment():
|
| 27 |
+
response = client.post("/predict", json={"text": "I find this product revolting."})
|
| 28 |
+
assert response.status_code == 200
|
| 29 |
+
assert response.json() == {"sentiment": "disgust"} # Emotion of disgust
|
| 30 |
+
|
| 31 |
+
def test_fear_sentiment():
|
| 32 |
+
response = client.post("/predict", json={"text": "This product scares me."})
|
| 33 |
+
assert response.status_code == 200
|
| 34 |
+
assert response.json() == {"sentiment": "fear"} # Emotion of fear
|
| 35 |
+
|
| 36 |
+
def test_sadness_sentiment():
|
| 37 |
+
response = client.post("/predict", json={"text": "This product makes me really sad."})
|
| 38 |
+
assert response.status_code == 200
|
| 39 |
+
assert response.json() == {"sentiment": "sadness"} # Emotion of sadness
|
| 40 |
+
|
| 41 |
+
def test_surprise_sentiment():
|
| 42 |
+
response = client.post("/predict", json={"text": "I'm amazed by this product!"})
|
| 43 |
+
assert response.status_code == 200
|
| 44 |
+
assert response.json() == {"sentiment": "surprise"} # Emotion of surprise
|