milyiyo commited on
Commit
abaee04
1 Parent(s): 9ef00b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -2
app.py CHANGED
@@ -1,4 +1,5 @@
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,8 +46,43 @@ 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
- def make_poem(author, sentiment, words, text):
49
- return [author, sentiment, words, text]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
  iface = gr.Interface(
52
  fn=make_poem,
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
 
4
  author_set = {'Leopoldo Lugones', 'Nacho Buzón', 'Octavio Paz', 'Luis Cañizal de la Fuente', 'Juan de Salinas', 'Vicente Huidobro', 'Hilario Barrero',
5
  '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',
 
46
  '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',
47
  'Alfonsina Storni', 'José Luis Rey Cano', 'Jorge Manrique', 'Nicanor Parra'}
48
 
49
+ model_name = 'hackathon-pln-es/poem-gen-spanish-t5-small'
50
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
51
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
52
+
53
+ def make_poem(author, text, sentiment, words):
54
+ num_lines=5
55
+ poem = text
56
+ prev_output = ''
57
+ l_words = [x.strip() for x in words.split(',')]
58
+
59
+ # Add empty strings to words
60
+ if num_lines > len(l_words):
61
+ diff = num_lines - len(l_words)
62
+ l_words += [''] * diff
63
+
64
+ random.shuffle(l_words)
65
+
66
+ for i in range(num_lines):
67
+ word = l_words[i]
68
+ if word == '':
69
+ input_text = f"""poema: estilo: {author} && sentimiento: {sentiment} && texto: {poem} """
70
+ else:
71
+ input_text = f"""poema: estilo: {author} && sentimiento: {sentiment} && palabras: {word} && texto: {poem} """
72
+ inputs = tokenizer(input_text, return_tensors="pt")
73
+
74
+ outputs = model.generate(inputs["input_ids"],
75
+ do_sample = True,
76
+ max_length = 30,
77
+ repetition_penalty = 20.0,
78
+ top_k = 50,
79
+ top_p = 0.92)
80
+ detok_outputs = [tokenizer.decode(x, skip_special_tokens=True) for x in outputs]
81
+ pre_output = detok_outputs[0]
82
+
83
+ pre_output = pre_output.replace('.', ' ')
84
+ poem += '\n' + pre_output
85
+ return poem
86
 
87
  iface = gr.Interface(
88
  fn=make_poem,