File size: 1,932 Bytes
dd8100a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e14fa5b
dd8100a
 
 
 
 
 
 
2471454
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import re
import string
import nltk
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Optional
from transformers import pipeline
from pyngrok import ngrok
import nest_asyncio
from fastapi.responses import RedirectResponse

# Download NLTK resources
nltk.download('punkt')
nltk.download('wordnet')

# Initialize FastAPI app
app = FastAPI()

# Text preprocessing functions
def remove_urls(text):
    return re.sub(r'http[s]?://\S+', '', text)

def remove_punctuation(text):
    regular_punct = string.punctuation
    return re.sub(r'['+regular_punct+']', '', text)

def lower_case(text):
    return text.lower()

def lemmatize(text):
    wordnet_lemmatizer = nltk.WordNetLemmatizer()
    tokens = nltk.word_tokenize(text)
    return ' '.join([wordnet_lemmatizer.lemmatize(w) for w in tokens])

# Model loading
lyx_pipe = pipeline("text-classification", model="lxyuan/distilbert-base-multilingual-cased-sentiments-student")

# Input data model
class TextInput(BaseModel):
    text: str

# Welcome endpoint
@app.get('/')
async def welcome():
    # Redirect to the Swagger UI page
    return RedirectResponse(url="/docs")

# Sentiment analysis endpoint
@app.post('/analyze/')
async def Predict_Sentiment(text_input: TextInput):    
    text = text_input.text

    # Text preprocessing
    text = remove_urls(text)
    text = remove_punctuation(text)
    text = lower_case(text)
    text = lemmatize(text)

    # Perform sentiment analysis
    try:
        return lyx_pipe(text)
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

# Run the FastAPI app using Uvicorn
if __name__ == "__main__":
    # Create ngrok tunnel
    ngrok_tunnel = ngrok.connect(7860)
    print('Public URL:', ngrok_tunnel.public_url)

    # Allow nested asyncio calls
    nest_asyncio.apply()

    # Run the FastAPI app with Uvicorn
    import uvicorn
    uvicorn.run(app, port=7860)