-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
74 lines (66 loc) · 2.06 KB
/
app.py
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
from tensorflow import keras
import uvicorn
from config import settings
from pydantic import BaseModel
from typing import List
from fastapi import FastAPI
# from config import settings
from tweet_generator import tweet_generator
import tensorflow as tf
import numpy as np
tweet = ''
app = FastAPI()
model = tf.keras.models.load_model('SentimentAnalysis')
class Reviews(BaseModel):
text: str
@ app.get('/')
def index():
return {'message': 'Welcome to the Text Classifier Engine'}
@ app.get('/generate-tweet/{name}')
def get_name(name: str):
sentiment = ''
TPCK = settings.TPCK
TSCK = settings.TSCK
TPAK = settings.TPAK
TSAK = settings.TSAK
twitter_bot = tweet_generator.PersonTweeter(name, TPCK, TSCK, TPAK, TSAK)
random_tweet = twitter_bot.generate_random_tweet()
tweet = random_tweet
result = model.predict([tweet])
if result>=0.5:
sentiment = 'Positive sentiment is associated with this tweet'
else:
sentiment = 'Negative sentiment is associated with this tweet'
return {'Tweet': random_tweet,
'Sentiment Analysis':sentiment
}
@app.get('/predict/{sentence}')
def predict_review(sentence:str):
sentiment = ''
prediction = model.predict([sentence])
result = prediction.tolist()[0][0]
if result>=0.5:
sentiment = 'Positive sentiment is associated with this tweet'
else:
sentiment = 'Negative sentiment is associated with this tweet'
return {
'prediction': result,
'Sentiment Analysis':sentiment
}
@app.post('/predict')
def predict_review(data:Reviews):
sentiment = ''
received = data.dict()
text = received['text']
prediction = model.predict([text])
result = prediction.tolist()[0][0]
if result>=0.5:
sentiment = 'Positive sentiment is associated with this tweet'
else:
sentiment = 'Negative sentiment is associated with this tweet'
return {
'prediction': result,
'Sentiment Analysis':sentiment
}
if __name__ == '__main__':
uvicorn.run(app, host='127.0.0.1', port=8000)