<jupyter_start><jupyter_text><jupyter_code>%tensorflow_version 1.x
!pip install gpt-2-simple
import gpt_2_simple as gpt2
#trying to load the saved model
from google.colab import drive
drive.mount('/content/drive')
sess = gpt2.start_tf_sess()

#!cp -r /content/drive/MyDrive/GPT2_Project/checkpoint/run1* /content/drive/MyDrive/GPT2_Project/models/124M

#load only if you want to generate text! if you want to keep on training model -skip this cell!
gpt2.load_gpt2(sess,checkpoint_dir='/content/drive/MyDrive/GPT2_Project/checkpoint',model_dir='/content/drive/MyDrive/GPT2_Project/models')

#def load_gpt2(sess, checkpoint='latest', run_name='run1', checkpoint_dir='checkpoint', model_name=None, model_dir='models', multi_gpu=False)

gpt2.download_gpt2(model_name="124M")
file_name = '/content/drive/MyDrive/GPT2_Project/Friends_Transcript.txt'
file_name
#the code for keep on trainig the current model that is stored on the drive:
gpt2.finetune(sess,
              dataset=file_name,
              model_name='124M',
              model_dir='/content/drive/MyDrive/GPT2_Project/models',
              steps=2000,
              restore_from='latest',
              run_name='run1',
              print_every=100,
              checkpoint_dir='/content/drive/MyDrive/GPT2_Project/checkpoint',
              sample_every=500,
              save_every=200,
              overwrite=True
              )

# def finetune(sess, dataset, steps=-1, model_name='124M', model_dir='models', combine=50000, batch_size=1, 
#              learning_rate=0.0001, accumulate_gradients=5, restore_from='latest', run_name='run1',
#              checkpoint_dir='checkpoint', sample_every=100, sample_length=1023, sample_num=1, multi_gpu=False, 
#              save_every=1000, print_every=1, max_checkpoints=1, use_memory_saving_gradients=False, 
#              only_train_transformer_layers=False, optimizer='adam', overwrite=False)

gpt2.generate(sess,
              checkpoint_dir = '/content/drive/MyDrive/GPT2_Project/checkpoint',
              model_dir='/content/drive/MyDrive/GPT2_Project/models',
              #return_as_list=True,
              length=250,
              temperature=0.7,
              prefix="Monica adopted a baby.",
              nsamples=1,
              batch_size=1,
              top_k=40
)


# def generate(sess, run_name='run1', checkpoint_dir='checkpoint', model_name=None, model_dir='models', 
#              sample_dir='samples', return_as_list=False,
#              truncate=None, destination_path=None, sample_delim='=' * 20 + '\n',
#              prefix=None, seed=None, nsamples=1, batch_size=1, length=1023, temperature=0.7,
#              top_k=0, top_p=0.0, include_prefix=True)
generated_script = gpt2.generate(sess,
              checkpoint_dir = '/content/drive/MyDrive/GPT2_Project/checkpoint',
              model_dir='/content/drive/MyDrive/GPT2_Project/models',
              #return_as_list=True,
              length=250,
              temperature=0.7,
              prefix="Monica adopted a baby.",
              nsamples=1,
              batch_size=1,
              top_k=40,
              return_as_list=True
)[0]

# generated_script = gpt2.generate(sess,
#               length=350,
#               temperature=0.7,
#               prefix="Chandler and Monica finally decided to leave New York",
#               nsamples=1,
#               batch_size=1,
#               top_k=40,
#               return_as_list=True
# )[0]
              
type(generated_script)
generated_script
!pip install -q gradio
!pip install -q git+https://github.com/huggingface/transformers.git
def generating_text(initial_text):
    return gpt2.generate(sess,
              checkpoint_dir = '/content/drive/MyDrive/GPT2_Project/checkpoint',
              model_dir='/content/drive/MyDrive/GPT2_Project/models',
              #return_as_list=True,
              length=250,
              temperature=0.7,
              prefix=initial_text,
              nsamples=1,
              batch_size=1,
              top_k=40,
              return_as_list=True
)[0]
   




def greet(name):
    return "Hello, my name is " +  name

iface = gr.Interface(greet,"textbox","textbox")    
iface.launch()

import gradio as gr
import tensorflow as tf
from transformers import TFGPT2LMHeadModel, GPT2Tokenizer
output_text = gr.outputs.Textbox()
#gr.Interface(generating_text,"textbox", output_text, title="GPT2 Finetune",
             #description="I finetuned GPT-2 on Friends script.").launch()
gr.Interface(generating_text,"text", "text",capture_session=True).launch()

!zip -r /content/file.zip /content/<jupyter_output>updating: content/models/ (stored 0%)
updating: content/models/124M/ (stored 0%)
updating: content/models/124M/hparams.json (deflated 28%)
updating: content/models/124M/model.ckpt.index (deflated 62%)
updating: content/models/124M/checkpoint (deflated 42%)
updating: content/models/124M/vocab.bpe (deflated 53%)
updating: content/models/124M/model.ckpt.data-00000-of-00001 (deflated 7%)
updating: content/models/124M/encoder.json (deflated 67%)
updating: content/models/124M/model.ckpt.meta (deflated 91%)
  adding: content/ (stored 0%)
  adding: content/.config/ (stored 0%)
  adding: content/.config/.last_update_check.json (deflated 23%)
  adding: content/.config/.last_opt_in_prompt.yaml (stored 0%)
  adding: content/.config/active_config (stored 0%)
  adding: content/.config/config_sentinel (stored 0%)
  adding: content/.config/.last_survey_prompt.yaml (stored 0%)
  adding: content/.config/configurations/ (stored 0%)
  adding: content/.config/configurations/config_default (deflated 15%)
  [...]<jupyter_text>New Section<jupyter_code>from google.colab import files
files.download("/content/file.zip")
from google.colab import drive
drive.mount('/content/drive')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1192644784327204, 'fraction_numerical': 0.03454201752878502, 'mean_word_length': 3.0756302521008405, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '180541', 'license': 'MIT', 'n_tokens_mistral': 2014, 'n_tokens_neox': 1882, 'n_words': 330, 'path': 'loading_and_keep_Finetuning_GPT2_Friends.ipynb', 'repo_name': 'zymoide/GPT2_Friends_Text_Generation', 'size': '5819'}