-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamlit_app.py
66 lines (42 loc) · 1.89 KB
/
streamlit_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
import streamlit as st
import tensorflow as tf
import numpy as np
# Load and prepare text
text = open("corpus/obras.txt", 'rb').read().decode(encoding='utf-8')
vocab = sorted(set(text))
char2idx = {u:i for i, u in enumerate(vocab)}
idx2char = np.array(vocab)
# Load model
model = tf.keras.models.load_model('model/modelo.h5', compile = False)
# Predictions
def generate_text(model, start_string):
num_generate = 1000
input_eval = [char2idx[s] for s in start_string]
input_eval = tf.expand_dims(input_eval, 0)
text_generated = []
temperature = 0.5
model.reset_states()
for i in range(num_generate):
predictions = model(input_eval)
predictions = tf.squeeze(predictions, 0)
predictions = predictions / temperature
predicted_id = tf.random.categorical(predictions, num_samples=1)[-1,0].numpy()
input_eval = tf.expand_dims([predicted_id], 0)
text_generated.append(idx2char[predicted_id])
text = ''.join(text_generated)
text = text.rsplit('.', 1)[0]
return (start_string + text + '.')
# Streamlit
st.set_page_config(page_title= 'Gerador de Texto - Guimarães Rosa', page_icon=":eyeglasses:")
st.title('Gerador de texto de João Guimarães Rosa')
st.write('Inteligência Artficial treinada em todas as obras de João Guimarães Rosa para escrever como o autor.')
input_text = st.text_input('(Opcional) Escreva o início da frase:')
if st.button('Gerar texto'):
if len(input_text) == 0:
generated_text = generate_text(model, start_string = ' ')
st.write(generated_text)
else:
generated_text = generate_text(model, start_string = input_text)
st.write(generated_text)
with st.beta_expander("Sobre o projeto"):
st.markdown("Todo o código do projeto está disponível no nosso repositório do [github](https://github.com/turing-usp/gerador-texto-guimaraes).")