Trend_Sentry / app.py
ItsProtesilaus's picture
Update app.py
c551c64 verified
Raw
History Blame Contribute Delete
8.29 kB
# import tensorflow as tf
# import numpy as np
# import os
# import pandas as pd
# from flask import Flask, request, jsonify
# import joblib
# import sklearn
# import re
# from helpers.helper_functions import remove_html_tags, remove_url, remove_digits, remove_punc, lower
# from models import Model
# from werkzeug.exceptions import RequestEntityTooLarge
# # Configure TensorFlow logging
# os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
# app = Flask(__name__)
# app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 * 1024 # 16 GB max-limit
# # Initialize model
# model = Model()
# # Define label mappings
# SENTIMENT_LABELS = ['negative', 'neutral', 'positive']
# EMOTION_LABELS = ['sadness', 'joy', 'love', 'anger', 'fear', 'surprise']
# @app.route('/')
# def index():
# return 'Model deployed successfully'
# @app.route('/predict', methods=['POST'])
# def predict():
# try:
# # Validate request
# data = request.get_json()
# if not data:
# return jsonify({'error': 'No input provided'}), 400
# # Handle array of objects
# if isinstance(data, list):
# texts = []
# tweet_ids = []
# for item in data:
# if not isinstance(item, dict) or 'text' not in item or 'id' not in item or not isinstance(item['text'], str):
# return jsonify({'error': f'Invalid object format: {item}. Each object must have "id" and "text" keys with a string value for "text"'}), 400
# texts.append(item['text'])
# tweet_ids.append(item['id'])
# else:
# return jsonify({'error': 'Input must be an array of objects'}), 400
# results = []
# for idx, text in enumerate(texts):
# # Preprocess text for sentiment
# text_sentiment = text
# text_sentiment = remove_html_tags(text_sentiment)
# text_sentiment = lower(text_sentiment)
# # Preprocess text for emotion
# text_emotion = text
# text_emotion = remove_html_tags(text_emotion)
# text_emotion = remove_url(text_emotion)
# text_emotion = remove_digits(text_emotion)
# text_emotion = remove_punc(text_emotion)
# # Convert to tensors and get predictions
# sentiment_tensor = tf.convert_to_tensor([text_sentiment])
# emotion_tensor = tf.convert_to_tensor([text_emotion])
# print(f"Sentiment tensor shape: {sentiment_tensor.shape}")
# print(f"Emotion tensor shape: {emotion_tensor.shape}")
# sentiment_pred, emotion_pred = model.predict(
# text_sentiment=sentiment_tensor,
# text_emotion=emotion_tensor
# )
# print(f"Sentiment prediction shape: {sentiment_pred.shape}")
# print(f"Emotion prediction shape: {emotion_pred.shape}")
# print(f"Sentiment prediction: {sentiment_pred}")
# print(f"Emotion prediction: {emotion_pred}")
# # Process predictions
# sentiment_idx = int(np.argmax(sentiment_pred, axis=1)[0])
# emotion_idx = int(np.argmax(emotion_pred, axis=1)[0])
# print(f"Sentiment index: {sentiment_idx}")
# print(f"Emotion index: {emotion_idx}")
# # Create result object including tweet_id
# result = {
# 'id': tweet_ids[idx], # Add the tweet_id
# 'text': text,
# 'sentiment': {
# 'label': SENTIMENT_LABELS[sentiment_idx],
# 'score': float(sentiment_pred[0][sentiment_idx]),
# 'raw_scores': [float(score) for score in sentiment_pred[0]]
# },
# 'emotion': {
# 'label': EMOTION_LABELS[emotion_idx],
# 'score': float(emotion_pred[0][emotion_idx]),
# 'raw_scores': [float(score) for score in emotion_pred[0]]
# }
# }
# results.append(result)
# return jsonify({'results': results})
# except RequestEntityTooLarge:
# return jsonify({'error': 'Request Entity Too Large'}), 413
# except IndexError as e:
# return jsonify({'error': f'IndexError: {str(e)}'}), 500
# except Exception as e:
# return jsonify({'error': f'Unexpected error: {str(e)}'}), 400
# if __name__ == '__main__':
# app.run(host='0.0.0.0' ,port=7860, debug=False)
import tensorflow as tf
import numpy as np
import os
import pandas as pd
from flask import Flask, request, jsonify
from helpers.helper_functions import remove_html_tags, remove_url, remove_digits, remove_punc, lower
from models import Model
from werkzeug.exceptions import RequestEntityTooLarge
# Configure TensorFlow logging and GPU
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Suppress warnings
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
physical_devices = tf.config.list_physical_devices('GPU')
if physical_devices:
tf.config.experimental.set_memory_growth(physical_devices[0], True)
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 * 1024 # 16 GB max-limit
# Initialize model globally
model = Model()
# Define label mappings
SENTIMENT_LABELS = ['negative', 'neutral', 'positive']
EMOTION_LABELS = ['sadness', 'joy', 'love', 'anger', 'fear', 'surprise']
# Vectorized preprocessing function
def preprocess_batch(texts, for_emotion=False):
df = pd.Series(texts)
df = df.apply(remove_html_tags).str.lower()
if for_emotion:
df = df.apply(remove_url).apply(remove_digits).apply(remove_punc)
return df.tolist()
@app.route('/')
def index():
return 'Model deployed successfully'
@app.route('/predict', methods=['POST'])
def predict():
try:
# Validate request
data = request.get_json()
if not data or not isinstance(data, list):
return jsonify({'error': 'Input must be an array of objects'}), 400
# Extract texts and IDs
texts = []
tweet_ids = []
for item in data:
if not isinstance(item, dict) or 'text' not in item or 'id' not in item or not isinstance(item['text'], str):
return jsonify({'error': f'Invalid object format: {item}'}), 400
texts.append(item['text'])
tweet_ids.append(item['id'])
# Batch preprocessing
texts_sentiment = preprocess_batch(texts, for_emotion=False)
texts_emotion = preprocess_batch(texts, for_emotion=True)
# Convert to tensors and ensure tf.string dtype
sentiment_tensor = tf.convert_to_tensor(texts_sentiment, dtype=tf.string)
emotion_tensor = tf.convert_to_tensor(texts_emotion, dtype=tf.string)
# Batch inference
sentiment_preds, emotion_preds = model.predict(
text_sentiment=sentiment_tensor,
text_emotion=emotion_tensor
)
# Process predictions
results = []
for idx, (sentiment_pred, emotion_pred) in enumerate(zip(sentiment_preds, emotion_preds)):
sentiment_idx = int(np.argmax(sentiment_pred))
emotion_idx = int(np.argmax(emotion_pred))
result = {
'id': tweet_ids[idx],
'text': texts[idx],
'sentiment': {
'label': SENTIMENT_LABELS[sentiment_idx],
'score': float(sentiment_pred[sentiment_idx]),
'raw_scores': [float(score) for score in sentiment_pred]
},
'emotion': {
'label': EMOTION_LABELS[emotion_idx],
'score': float(emotion_pred[emotion_idx]),
'raw_scores': [float(score) for score in emotion_pred]
}
}
results.append(result)
return jsonify({'results': results})
except RequestEntityTooLarge:
return jsonify({'error': 'Request Entity Too Large'}), 413
except Exception as e:
return jsonify({'error': f'Unexpected error: {str(e)}'}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0' ,port=7860, debug=False)