Team Ai
Apppublic

emanism6/Text-Classification-Fastapi

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
0likes
main.py77 linesDownload Raw Back to root
1import re2import string3import nltk4from fastapi import FastAPI, HTTPException5from pydantic import BaseModel6from typing import Optional7from transformers import pipeline8from pyngrok import ngrok9import nest_asyncio10from fastapi.responses import RedirectResponse11 12# Download NLTK resources13nltk.download('punkt')14nltk.download('wordnet')15 16# Initialize FastAPI app17app = FastAPI()18 19# Text preprocessing functions20def remove_urls(text):21    return re.sub(r'http[s]?://\S+', '', text)22 23def remove_punctuation(text):24    regular_punct = string.punctuation25    return re.sub(r'['+regular_punct+']', '', text)26 27def lower_case(text):28    return text.lower()29 30def 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 loading36lyx_pipe = pipeline("text-classification", model="lxyuan/distilbert-base-multilingual-cased-sentiments-student")37 38# Input data model39class TextInput(BaseModel):40    text: str41 42# Welcome endpoint43@app.get('/')44async def welcome():45    # Redirect to the Swagger UI page46    return RedirectResponse(url="/docs")47 48# Sentiment analysis endpoint49@app.post('/analyze/')50async def Predict_Sentiment(text_input: TextInput):    51    text = text_input.text52 53    # Text preprocessing54    text = remove_urls(text)55    text = remove_punctuation(text)56    text = lower_case(text)57    text = lemmatize(text)58 59    # Perform sentiment analysis60    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 Uvicorn66if __name__ == "__main__":67    # Create ngrok tunnel68    ngrok_tunnel = ngrok.connect(7860)69    print('Public URL:', ngrok_tunnel.public_url)70 71    # Allow nested asyncio calls72    nest_asyncio.apply()73 74    # Run the FastAPI app with Uvicorn75    import uvicorn76    uvicorn.run(app, port=7860)77