-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_gen.py
67 lines (57 loc) · 2.23 KB
/
text_gen.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
from flask import Flask , render_template , request
from flask_pymongo import PyMongo
from pymongo import MongoClient
import fastai
from fastai import *
import pathlib
import pickle
temp = pathlib.PosixPath
pathlib.PosixPath = pathlib.WindowsPath
from fastai.text.all import *
app = Flask(__name__)
#app.config['MONGO_URI'] = 'mongodb://localhost:27017/posts'
#mongo = PyMongo(app)
# Connection to the MongoDB Server
mongoClient = MongoClient ('mongodb+srv://hamza:mongodb@mongodb-heroku.igizt.mongodb.net/myFirstDatabase?retryWrites=true&w=majority')
# Connection to the database
db = mongoClient.posts
#Collection
collection = db.record
generated_text = collection.find()
@app.route('/', methods = ['GET', 'POST'])
def home():
global cat
cat = ''
global clean_text
clean_text = ''
if request.method == 'POST':
# lodaing classification model
classifier = load_learner('reddit_classifier.pkl')
# classification
user_input = request.form.get('post_text')
cat,_,probs = classifier.predict(user_input)
cat = cat.capitalize()
print(cat)
# generate text
clean_text = generate_text(user_input)
# generate text again if the same text found in the database
for document in generated_text:
if (clean_text == document['Generated_Text']):
clean_text = generate_text(user_input)
# storing the generated text and category in database
collection.insert([dict(Category = cat , Generated_Text = clean_text)])
return render_template('index_main.html' , prediction = cat , comment = clean_text)
def generate_text(text_input):
# initialize tokenizer and model from pretrained GPT2 model
tokenizer = pickle.load(open('gpt2_tokenizer.pkl','rb'))
model = pickle.load(open('gpt2_language.pkl','rb'))
# encode user_input
inputs = tokenizer.encode(text_input, return_tensors='pt')
# we pass a maximum output length of 200 tokens
outputs = model.generate(inputs, max_length=100, do_sample=True)
#decode user_input
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
text = text.replace("\n", "")
return text
if __name__ == '__main__':
app.run(debug=True)