Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +95 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
import asyncio
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import re
|
| 5 |
+
import string
|
| 6 |
+
import nltk
|
| 7 |
+
nltk.download('punkt')
|
| 8 |
+
nltk.download('wordnet')
|
| 9 |
+
nltk.download('omw-1.4')
|
| 10 |
+
from nltk.stem import WordNetLemmatizer
|
| 11 |
+
import pickle
|
| 12 |
+
|
| 13 |
+
# Function to remove URLs from text
|
| 14 |
+
def remove_urls(text):
|
| 15 |
+
return re.sub(r'http[s]?://\S+', '', text)
|
| 16 |
+
|
| 17 |
+
# Function to remove punctuations from text
|
| 18 |
+
def remove_punctuation(text):
|
| 19 |
+
regular_punct = string.punctuation
|
| 20 |
+
return str(re.sub(r'['+regular_punct+']', '', str(text)))
|
| 21 |
+
|
| 22 |
+
# Function to convert the text into lower case
|
| 23 |
+
def lower_case(text):
|
| 24 |
+
return text.lower()
|
| 25 |
+
|
| 26 |
+
# Function to lemmatize text
|
| 27 |
+
def lemmatize(text):
|
| 28 |
+
wordnet_lemmatizer = WordNetLemmatizer()
|
| 29 |
+
|
| 30 |
+
tokens = nltk.word_tokenize(text)
|
| 31 |
+
lemma_txt = ''
|
| 32 |
+
for w in tokens:
|
| 33 |
+
lemma_txt = lemma_txt + wordnet_lemmatizer.lemmatize(w) + ' '
|
| 34 |
+
|
| 35 |
+
return lemma_txt
|
| 36 |
+
|
| 37 |
+
def load_model():
|
| 38 |
+
# Define the file path where the trained model is saved
|
| 39 |
+
model_file_path = "logistic_regression_model.pkl"
|
| 40 |
+
|
| 41 |
+
# Load the saved Logistic Regression model from the file
|
| 42 |
+
with open(model_file_path, 'rb') as file:
|
| 43 |
+
loaded_model = pickle.load(file)
|
| 44 |
+
|
| 45 |
+
return loaded_model
|
| 46 |
+
|
| 47 |
+
def load_tfidf():
|
| 48 |
+
# Define the file path where the TF-IDF vectorizer is saved
|
| 49 |
+
vectorizer_file_path = "tfidf_vectorizer.pkl"
|
| 50 |
+
|
| 51 |
+
# Load the saved TF-IDF vectorizer from the file
|
| 52 |
+
with open(vectorizer_file_path, 'rb') as file:
|
| 53 |
+
loaded_vectorizer = pickle.load(file)
|
| 54 |
+
|
| 55 |
+
return loaded_vectorizer
|
| 56 |
+
|
| 57 |
+
def preprocess(input_text):
|
| 58 |
+
# Preprocess the input text
|
| 59 |
+
input_text = remove_urls(input_text)
|
| 60 |
+
input_text = remove_punctuation(input_text)
|
| 61 |
+
input_text = lower_case(input_text)
|
| 62 |
+
input_text = lemmatize(input_text)
|
| 63 |
+
|
| 64 |
+
# Apply TF-IDF vectorization
|
| 65 |
+
input_text = [input_text]
|
| 66 |
+
tfidf = load_tfidf()
|
| 67 |
+
input_text = tfidf.transform(input_text)
|
| 68 |
+
|
| 69 |
+
return input_text
|
| 70 |
+
|
| 71 |
+
app = FastAPI()
|
| 72 |
+
|
| 73 |
+
@app.get('/')
|
| 74 |
+
async def welcome():
|
| 75 |
+
return "Welcome to our Sentiment Analysis API"
|
| 76 |
+
|
| 77 |
+
@app.post('/predict_sentiment')
|
| 78 |
+
async def predict_sentiment(input_text):
|
| 79 |
+
loaded_model = load_model()
|
| 80 |
+
predicted_sentiment = loaded_model.predict(preprocess(input_text))
|
| 81 |
+
if predicted_sentiment == 0:
|
| 82 |
+
sentiment = "Sentiment: Negative"
|
| 83 |
+
else:
|
| 84 |
+
sentiment = "Sentiment: Positive"
|
| 85 |
+
return sentiment
|
| 86 |
+
|
| 87 |
+
async def predict(input):
|
| 88 |
+
sentiment = await predict_sentiment(input)
|
| 89 |
+
return sentiment
|
| 90 |
+
|
| 91 |
+
# Create Gradio interface
|
| 92 |
+
iface = gr.Interface(fn=predict, inputs="text", outputs="text", title="Movie Review Sentiment Analysis API")
|
| 93 |
+
iface.launch(share=True)
|
| 94 |
+
|
| 95 |
+
asyncio.run(predict())
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.110.0
|
| 2 |
+
gradio==4.23.0
|
| 3 |
+
nltk==3.8.1
|