I'm trying to feed a prompt to a GPT-2 model, which is trained on song lyrics. With this model, you can feed it a prompt and it will attempt to finish it. The problem I'm having is that I need for the prompt to be formatted correctly like song lyrics - with each lyric on a newline. Since this model has been trained on formatted lyrics, the prompt needs to look the same to produce above average results.
input() doesn't work, because hitting return submits the text. I've also tried sys.stdin.read() as well and nothing seems to happen, just returns ' '.
Is it because I'm running it in a notebook? I'm only like 8 months deep into this python hobby, so I could be asking for something that can't happen.
Here's the code, would appreciate any ideas you've got.
prompt = input('Input Prompt:')
gpt2.generate(sess,
length=200,
temperature=.99,
prefix= prompt, #this is where the prompt goes
nsamples=5,
top_p=.9,
batch_size=5,
run_name='run1'
)
To take multiple lines of input and put them in a list of strings, you can use sys.stdin.readlines() which will keep accepting returns until you give it an EOF [ctrl + d in *nix, ctrl + z in Windows].
E.g.:
user_input = sys.stdin.readlines("Favourite foods followed by EOF:") # User gives input...
print(user_input)
Output:
>> ["Spam\n", "Spam\n", "Spam\n"]
Obviously this leaves you with newlines, so you could do something hacky like:
user_input = [line.rstrip() for line in sys.stdin.readlines()]
Which will give you:
>> ["Spam", "Spam", "Spam"]
Sounds like an interesting ML problem, though dealing with a list of strings may complicate things significantly. You might want to make them one long string using something like join, e.g.
" ".join(user_input) # Used with a separator of " ", but you could use newlines or something
Which gives one string of:
>> "Spam Spam Spam"
Can't you use a while loop to read the input? e.g.
prompt = []
line = input("Input prompt ending with an empty line: ")
while line:
prompt.append(line)
line = input()
prompt = "\n".join(prompt)
python - How to get multiline input from the user - Stack Overflow
Python: Multiline input converted into a list - Stack Overflow
multiline input in terminal - Ideas - Discussions on Python.org
Store multi-line input into a String (Python) - Stack Overflow
Videos
Keep calling the input() function until the line it reads in is empty. The use the .split method (no args = space as deliminator). Use a list-comp to convert each string to an int and append this to your examList.
Here's the code:
examList = []
i = input()
while i != '':
examList.append([int(s) for s in i.split()])
i = input()
And with your input, examList is:
[[3], [2, 1], [1, 1, 0], [2, 1, 1], [4, 3, 0, 1, 2], [2], [1, 2], [1, 3]]
The above method is for Python3 which allows you to call input() and enter nothing (which is why we use this as a signal to check that we are done - i != '').
However, from the docs, we see that, in Python2, an empty entry to input() throws an EOF error. To get around this, I guess we could just make it so that the multi-line input is ended when the string such as: END is entered. This means that you must stop the entry with a line saying 'END':
examList = []
i = raw_input()
while i != 'END':
examList.append([int(s) for s in i.split()])
i = raw_input()
Note that I've used raw_input as to not perform type conversion.
which works the same as above.
You can use sys.stdin.readlines().
For example, start with
import sys
user_input = sys.stdin.readlines()
at this point, the value of user_input should be
['3\n', '2 1\n', '1 1 0\n', '2 1 1\n', '4 3 0 1 2\n', '2\n', '1 2\n', '1 3']
then, you should be able to get your desired output by performing the following processing
examList = []
for input_string in user_input:
examList.append([int(value) for value in input_string.split()])
What we are doing here is iterating through user_input. For each input_string, we split() the words, convert them to int and put them back into a new list. Then, the new list will be added into examList.
raw_input can correctly handle the EOF, so we can write a loop, read till we have received an EOF (Ctrl-D) from user:
Python 3
print("Enter/Paste your content. Ctrl-D or Ctrl-Z ( windows ) to save it.")
contents = []
while True:
try:
line = input()
except EOFError:
break
contents.append(line)
Python 2
print "Enter/Paste your content. Ctrl-D or Ctrl-Z ( windows ) to save it."
contents = []
while True:
try:
line = raw_input("")
except EOFError:
break
contents.append(line)
In Python 3.x the raw_input() of Python 2.x has been replaced by input() function. However in both the cases you cannot input multi-line strings, for that purpose you would need to get input from the user line by line and then .join() them using \n, or you can also take various lines and concatenate them using + operator separated by \n
To get multi-line input from the user you can go like:
no_of_lines = 5
lines = ""
for i in xrange(no_of_lines):
lines+=input()+"\n"
print(lines)
Or
lines = []
while True:
line = input()
if line:
lines.append(line)
else:
break
text = '\n'.join(lines)
The problem with your code is that input() stops as soon as you hit Enter, to get continuous input you need to use a either a while loop or a for loop and take input until user enters a sentinel value:
Using for-loop and iter:
def multiline_input(sentinel=''):
for inp in iter(input, sentinel):
yield inp.split()
...
>>> lis = list(multiline_input())
1 2 3
400 500 600
a b c
>>> lis
[['1', '2', '3'], ['400', '500', '600'], ['a', 'b', 'c']]
Using while loop:
def multiline_input(sentinel=''):
while True:
inp = input()
if inp != sentinel:
yield inp.split()
else:
break
...
>>> lis = list(multiline_input())
1 2
3 4 5
6 7 8 9
10
>>> lis
[['1', '2'], ['3', '4', '5'], ['6', '7', '8', '9'], ['10']]
It may be something I do not understand, but in the performance of such code here
aString = """
4 3
10 3
100 5
1000000000 8
"""
aString_list = [x for x in (y.split() for y in aString.split('\n')) if x]
print(aString_list)
we get a result:
[['4', '3'], ['10', '3'], ['100', '5'], ['1000000000', '8']]
You can use readlines() method of file objects:
import sys
userInput = sys.stdin.readlines()
You can easily create one, using generators. Here is one such implementation. Note you can either press a blank return or any Keyboard Interrupt to break out of the inputloop
>>> def multi_input():
try:
while True:
data=raw_input()
if not data: break
yield data
except KeyboardInterrupt:
return
>>> userInput = list(multi_input())
359716482
867345912
413928675
398574126
>>> userInput
['359716482', '867345912', '413928675', '398574126']
>>>
Using splitlines() to split by newline character and strip() to remove unnecessary white spaces.
>>> names='''
... Apple
... Ball
... Cat'''
>>> names
'\n Apple\n Ball\n Cat'
>>> names_list = [y for y in (x.strip() for x in names.splitlines()) if y]
>>> # if x.strip() is used to remove empty lines
>>> names_list
['Apple', 'Ball', 'Cat']
names.splitlines() should give you just that.