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 - How to create mutiple list from mutiple lines input - Stack Overflow
Store an input of multiple lines into a list in python - Stack Overflow
Python: Multiline input converted into a list - Stack Overflow
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)
If you just append the values from your initial input line to lst it allows you to take each line and make it into a 2D list
>>> lst = []
>>> lst.append([int(y) for y in input().split()])
5 2
>>> lst.append([int(y) for y in input().split()])
6 1
>>> lst
[[5, 2], [6, 1]]
>>>
However, you mentioned multiple lines so I assume you want to take all the input at once. Using this answer, I adapted the code to append each line as a list (as above). Input will only stop when a Ctrl-D (posix) or Ctrl-Z (windows) character is found
>>> lst = []
>>> while True:
... try:
... line = input()
... except EOFError:
... break
... lst.append([int(y) for y in line.split()])
...
5 2
6 1
7 3
4 2
10 5
12 4
^Z
>>> lst
[[5, 2], [6, 1], [7, 3], [4, 2], [10, 5], [12, 4]]
>>>
Hope this helps!
EDIT: Thought I would add, this could then be used later by iterating over the outer list
>>> for item in lst:
... print(item)
...
[5, 2]
[6, 1]
[7, 3]
[4, 2]
[10, 5]
[12, 4]
>>>
One Liner:
[[int(x) for x in input().split()] for i in range(6)]
5 2
6 1
7 3
4 2
10 5
12 4
#[[5, 2], [6, 1], [7, 3], [4, 2], [10, 5], [12, 4]]
You can generalize 6 to n in range
Here, I am using a list comprehension instead of list(map(int, input().split())).
A link to document
https://python-reference.readthedocs.io/en/latest/docs/comprehensions/list_comprehension.html
Use a list comprehension, calling the input() function for each iteration, like so:
l = [int(input()) for _ in range(int(input()))]
output for print(l), with the first input being 4:
[1, 2, 3, 4]
Updated answer based on comment discussion.
num = input()
li = []
for _ in range(num):
data = int(input())
li.append(data)
print(li)
input
4
6
3
9
4
output
[ 6 , 3 , 9 , 4 ]
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']]
I recently took a coding assessment where I was tasked to compute the square of the input which is relatively easy. The issue was that the input was a multiple line input:
7
16
and the expected output is
49
256
The given code was
for line in sys.stdin:
# your code here
print(line, end="")I tried to do ls = list(line.split()) but ls[0] is
['7']
['16']
and ls[1] is None
I also tried ls = list(line.split('\n')) but ls is
['7', '']
['16', '']
So how was I supposed to process the input to get ['7', '16'] rather than a 2 dimensional list?
From there I know how continue with make it an integer using map, creating a for loop for each item of the list and printing the square of each item.
I dont have a picture of the question since I was monitored on webcam but this is roughly what I remembered from the question.
edit: It was an online assessment platform so I am not sure exactly how the input was written as (like the raw input). Also I can only modify the code inside for line in sys.stdin:
Also, does anyone know how to write the input for sys.stdin using jupyternotebook such that I can practice this problem?