import json
import os
import numpy as np
import tensorflow as tf

import model, sample, encoder

def interact_model(
    model_name='345M',
    seed=None,
    nsamples=1,
    batch_size=1,
    length=108,
    temperature=0.9,
    top_k=38,
    raw_text=''
):
    if batch_size is None:
        batch_size = 1
    assert nsamples % batch_size == 0

    enc = encoder.get_encoder(model_name)
    hparams = model.default_hparams()
    with open(os.path.join('models', model_name, 'hparams.json')) as f:
        hparams.override_from_dict(json.load(f))

    if length is None:
        length = hparams.n_ctx // 2
    elif length > hparams.n_ctx:
        raise ValueError("Can't get samples longer than window size: %s" % hparams.n_ctx)

    with tf.Session(graph=tf.Graph()) as sess:
        context = tf.placeholder(tf.int32, [batch_size, None])
        np.random.seed(seed)
        tf.set_random_seed(seed)
        output = sample.sample_sequence(
            hparams=hparams, length=length,
            context=context,
            batch_size=batch_size,
            temperature=temperature, top_k=top_k
        )

        saver = tf.train.Saver()
        ckpt = tf.train.latest_checkpoint(os.path.join('models', model_name))
        saver.restore(sess, ckpt)

        text=''
        context_tokens = enc.encode(raw_text)
        #generated = 0
        for _ in range(nsamples // batch_size):
            out = sess.run(output, feed_dict={
                context: [context_tokens for _ in range(batch_size)]
            })[:, len(context_tokens):]
            for i in range(batch_size):
                #generated += 1
                text += enc.decode(out[i])

        # Ensure that generated text ends with punctuation or twitter handle
        #while (text[-1] not in '.?!') and ('@' not in text.split()[-1]):

    return text

5c328c897e81fbcb7e2fbd885a57e531d8740f39
<mask token>

<mask token>


def interact_model(model_name='345M', seed=None, nsamples=1, batch_size=1,
    length=108, temperature=0.9, top_k=38, raw_text=''):
    if batch_size is None:
        batch_size = 1
    assert nsamples % batch_size == 0
    enc = encoder.get_encoder(model_name)
    hparams = model.default_hparams()
    with open(os.path.join('models', model_name, 'hparams.json')) as f:
        hparams.override_from_dict(json.load(f))
    if length is None:
        length = hparams.n_ctx // 2
    elif length > hparams.n_ctx:
        raise ValueError("Can't get samples longer than window size: %s" %
            hparams.n_ctx)
    with tf.Session(graph=tf.Graph()) as sess:
        context = tf.placeholder(tf.int32, [batch_size, None])
        np.random.seed(seed)
        tf.set_random_seed(seed)
        output = sample.sample_sequence(hparams=hparams, length=length,
            context=context, batch_size=batch_size, temperature=temperature,
            top_k=top_k)
        saver = tf.train.Saver()
        ckpt = tf.train.latest_checkpoint(os.path.join('models', model_name))
        saver.restore(sess, ckpt)
        text = ''
        context_tokens = enc.encode(raw_text)
        for _ in range(nsamples // batch_size):
            out = sess.run(output, feed_dict={context: [context_tokens for
                _ in range(batch_size)]})[:, len(context_tokens):]
            for i in range(batch_size):
                text += enc.decode(out[i])
    return text

import json
import os
import numpy as np
import tensorflow as tf
import model, sample, encoder


def interact_model(model_name='345M', seed=None, nsamples=1, batch_size=1,
    length=108, temperature=0.9, top_k=38, raw_text=''):
    if batch_size is None:
        batch_size = 1
    assert nsamples % batch_size == 0
    enc = encoder.get_encoder(model_name)
    hparams = model.default_hparams()
    with open(os.path.join('models', model_name, 'hparams.json')) as f:
        hparams.override_from_dict(json.load(f))
    if length is None:
        length = hparams.n_ctx // 2
    elif length > hparams.n_ctx:
        raise ValueError("Can't get samples longer than window size: %s" %
            hparams.n_ctx)
    with tf.Session(graph=tf.Graph()) as sess:
        context = tf.placeholder(tf.int32, [batch_size, None])
        np.random.seed(seed)
        tf.set_random_seed(seed)
        output = sample.sample_sequence(hparams=hparams, length=length,
            context=context, batch_size=batch_size, temperature=temperature,
            top_k=top_k)
        saver = tf.train.Saver()
        ckpt = tf.train.latest_checkpoint(os.path.join('models', model_name))
        saver.restore(sess, ckpt)
        text = ''
        context_tokens = enc.encode(raw_text)
        for _ in range(nsamples // batch_size):
            out = sess.run(output, feed_dict={context: [context_tokens for
                _ in range(batch_size)]})[:, len(context_tokens):]
            for i in range(batch_size):
                text += enc.decode(out[i])
    return text

import json
import os
import numpy as np
import tensorflow as tf

import model, sample, encoder

def interact_model(
    model_name='345M',
    seed=None,
    nsamples=1,
    batch_size=1,
    length=108,
    temperature=0.9,
    top_k=38,
    raw_text=''
):
    if batch_size is None:
        batch_size = 1
    assert nsamples % batch_size == 0

    enc = encoder.get_encoder(model_name)
    hparams = model.default_hparams()
    with open(os.path.join('models', model_name, 'hparams.json')) as f:
        hparams.override_from_dict(json.load(f))

    if length is None:
        length = hparams.n_ctx // 2
    elif length > hparams.n_ctx:
        raise ValueError("Can't get samples longer than window size: %s" % hparams.n_ctx)

    with tf.Session(graph=tf.Graph()) as sess:
        context = tf.placeholder(tf.int32, [batch_size, None])
        np.random.seed(seed)
        tf.set_random_seed(seed)
        output = sample.sample_sequence(
            hparams=hparams, length=length,
            context=context,
            batch_size=batch_size,
            temperature=temperature, top_k=top_k
        )

        saver = tf.train.Saver()
        ckpt = tf.train.latest_checkpoint(os.path.join('models', model_name))
        saver.restore(sess, ckpt)

        text=''
        context_tokens = enc.encode(raw_text)
        #generated = 0
        for _ in range(nsamples // batch_size):
            out = sess.run(output, feed_dict={
                context: [context_tokens for _ in range(batch_size)]
            })[:, len(context_tokens):]
            for i in range(batch_size):
                #generated += 1
                text += enc.decode(out[i])

        # Ensure that generated text ends with punctuation or twitter handle
        #while (text[-1] not in '.?!') and ('@' not in text.split()[-1]):

    return text
