-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
162 lines (142 loc) · 4.92 KB
/
main.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 29 14:42:58 2019
@author: rober
"""
import os,sys
from scripts import configure, gui, file_chooser, twitter_api, sort_twits, file_formatter, text_generator
class MainApp():
def __init__(self):
self.config = configure.init()
self.myTextGenRnn=None
def trending_twit_maker(self):
gui.out("Retrieving twits...")
filename=None
myTwitterAPI=twitter_api.TwitterAPI()
filename = myTwitterAPI.get_trending_twits()
sort_twits.sort_by_retweets(filename)
filename=file_formatter.json_to_txt(filename)
self.myTextGenRnn=text_generator.TextGenRnn()
self.train(filename)
return
def user_twit_maker(self):
myTwitterAPI=twitter_api.TwitterAPI()
username=gui.inp("username > ")
status,filename = myTwitterAPI.get_twits_from_user(username)
if status>100:
filename=file_formatter.json_to_txt(filename,show_username=False)
self.myTextGenRnn=text_generator.TextGenRnn()
self.train(filename)
elif status >1:
gui.out("we couldn't retrieve all the tweets, or they aren't enough (they were %s)"%(status))
else:
gui.out("please, try again.")
return
def new(self):
header = """\n=== TwitterTrendingBot ===
[0] Trending twits
[1] By username
[2] Back
"""
flag=1
while flag:
gui.out(header)
i=gui.inp("> ")
try:
mode = int(i)
if mode in [0,1,2]:flag=0
except:
gui.out("<%s> no es un número válido."%(i))
if mode == 0:
self.trending_twit_maker()
if mode == 1:
self.user_twit_maker()
else:
sys.exit()
return
def load(self):
header = """\n=== TwitterTrendingBot ===
Load
[0] From twits file
[1] From a previous trained model
[2] Back
"""
flag=1
while flag:
gui.out(header)
i=gui.inp("> ")
try:
mode = int(i)
if mode in [0,1,2]:flag=0
except:
gui.out("<%s> no es un número válido."%(i))
if mode == 0:
filename = file_chooser.by_folder("./data",[".txt"])
file, file_extension = os.path.splitext(filename)
if file_extension == ".json":
sort_twits.sort_by_retweets(filename)
filename=file_formatter.json_to_txt(filename)
self.myTextGenRnn=text_generator.TextGenRnn()
self.train(filename)
elif mode == 1:
filename = file_chooser.by_folder("./trained_models",[".hdf5"])
self.myTextGenRnn=text_generator.TextGenRnn()
self.myTextGenRnn.load_model(filename)
mode=0
header = """\n=== TwitterTrendingBot ===
Model loaded.
[0] Train
[1] Generate
[2] Back
"""
main_flag=1
while main_flag:
flag=1
while flag:
gui.out(header)
i=gui.inp("> ")
try:
mode = int(i)
if mode in [0,1,2]:flag=0
except:
gui.out("<%s> no es un número válido."%(i))
if mode == 0:
gui.out("choose a file to train with.")
filename = file_chooser.by_folder("./data",[".txt"])
self.train(filename)
if mode == 1:
self.myTextGenRnn.textgen.generate()
else:
main_flag=0
return
def train(self,filename):
num_epochs=int(gui.inp("num_epochs: "))
save_epochs=int(gui.inp("save_epochs: "))
self.myTextGenRnn.train_from_file(filename,num_epochs,save_epochs)
return
def run(self):
mode=0
header = """\n=== TwitterTrendingBot ===
[0] New
[1] Load model
[2] Exit
"""
while True:
flag=1
while flag:
gui.out(header)
i=gui.inp("> ")
try:
mode = int(i)
if mode in [0,1,2]:flag=0
except:
gui.out("<%s> no es un número válido."%(i))
if mode == 0:
self.new()
if mode == 1:
self.load()
else:
sys.exit()
if __name__ == "__main__":
myMainApp=MainApp()
myMainApp.run()