Alberto Carmona commited on
Commit
102ae13
·
1 Parent(s): c5fc769

Add the logic to generate the poem

Browse files
Files changed (1) hide show
  1. app.py +45 -4
app.py CHANGED
@@ -1,4 +1,6 @@
1
  import gradio as gr
 
 
2
 
3
  author_set = {'Leopoldo Lugones', 'Nacho Buzón', 'Octavio Paz', 'Luis Cañizal de la Fuente', 'Juan de Salinas', 'Vicente Huidobro', 'Hilario Barrero',
4
  'Ramón de Campoamor', 'Anna Ajmátova', 'Víctor Hugo López Cancino', 'Ramón María del Valle-Inclán', 'Infantiles', 'Jorge Luis Borges',
@@ -45,12 +47,52 @@ author_set = {'Leopoldo Lugones', 'Nacho Buzón', 'Octavio Paz', 'Luis Cañizal
45
  'Jorge Teillier', 'Félix María de Samaniego', 'Nicolás Fernández de Moratín', 'Juan Boscán', 'Manuel María Flores', 'Gutierre de Cetina',
46
  'Alfonsina Storni', 'José Luis Rey Cano', 'Jorge Manrique', 'Nicanor Parra'}
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  def poem_generate(author, sentiment, words, text):
50
- debug_poem = 'Y cuando se despertó, el dinosaurio seguía ahí'
51
  # Pending: Translate poem to English, so that text can be the input of the latentdiffussion
52
- gen_image = poem_to_image(debug_poem)
53
- return debug_poem, gen_image
54
 
55
 
56
  def poem_to_image(poem):
@@ -91,7 +133,6 @@ with gr.Blocks() as demo:
91
  b1 = gr.Button("Generate Poem & Illustration")
92
 
93
  output_poem_txt = gr.Textbox(lines=7, label='Poema generado')
94
- output_poem_txt2 = gr.Textbox(lines=7, label='Poema generado 2')
95
  output_image = gr.Image(type="filepath", shape=(256, 256))
96
 
97
  b1.click(poem_generate, inputs=[input_author, input_sentiment, input_include_words, input_initial_text], outputs=[output_poem_txt, output_image])
 
1
  import gradio as gr
2
+ import random
3
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
 
5
  author_set = {'Leopoldo Lugones', 'Nacho Buzón', 'Octavio Paz', 'Luis Cañizal de la Fuente', 'Juan de Salinas', 'Vicente Huidobro', 'Hilario Barrero',
6
  'Ramón de Campoamor', 'Anna Ajmátova', 'Víctor Hugo López Cancino', 'Ramón María del Valle-Inclán', 'Infantiles', 'Jorge Luis Borges',
 
47
  'Jorge Teillier', 'Félix María de Samaniego', 'Nicolás Fernández de Moratín', 'Juan Boscán', 'Manuel María Flores', 'Gutierre de Cetina',
48
  'Alfonsina Storni', 'José Luis Rey Cano', 'Jorge Manrique', 'Nicanor Parra'}
49
 
50
+ model_name = 'hackathon-pln-es/poem-gen-spanish-t5-small'
51
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
52
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
53
+
54
+
55
+ def make_poem(author, sentiment, words, text):
56
+ num_lines=5
57
+ poem = text
58
+ prev_output = ''
59
+ l_words = [x.strip() for x in words.split(',')]
60
+
61
+ # Add empty strings to words
62
+ if num_lines > len(l_words):
63
+ diff = num_lines - len(l_words)
64
+ l_words += [''] * diff
65
+
66
+ random.shuffle(l_words)
67
+
68
+ for i in range(num_lines):
69
+ word = l_words[i]
70
+ if word == '':
71
+ input_text = f"""poema: estilo: {author} && sentimiento: {sentiment} && texto: {poem} """
72
+ else:
73
+ input_text = f"""poema: estilo: {author} && sentimiento: {sentiment} && palabras: {word} && texto: {poem} """
74
+ inputs = tokenizer(input_text, return_tensors="pt")
75
+
76
+ outputs = model.generate(inputs["input_ids"],
77
+ do_sample = True,
78
+ max_length = 30,
79
+ repetition_penalty = 20.0,
80
+ top_k = 50,
81
+ top_p = 0.92)
82
+ detok_outputs = [tokenizer.decode(x, skip_special_tokens=True) for x in outputs]
83
+ pre_output = detok_outputs[0]
84
+
85
+ poem += '\n' + pre_output
86
+ # audio = tts_es(poem)
87
+ # return poem, audio
88
+ return poem
89
+
90
 
91
  def poem_generate(author, sentiment, words, text):
92
+ poem_txt = make_poem(author, sentiment, words, text)
93
  # Pending: Translate poem to English, so that text can be the input of the latentdiffussion
94
+ poem_img = poem_to_image(poem_txt)
95
+ return poem_txt, poem_img
96
 
97
 
98
  def poem_to_image(poem):
 
133
  b1 = gr.Button("Generate Poem & Illustration")
134
 
135
  output_poem_txt = gr.Textbox(lines=7, label='Poema generado')
 
136
  output_image = gr.Image(type="filepath", shape=(256, 256))
137
 
138
  b1.click(poem_generate, inputs=[input_author, input_sentiment, input_include_words, input_initial_text], outputs=[output_poem_txt, output_image])