simonesp's picture
Update app.py
1362328
raw
history blame
3.84 kB
# -*- coding: utf-8 -*-
"""20221027_Generate_Beatles_with_Gradio_faster_version (2).ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1B2KVLt04uBMgr2ejW19FYaEgk9lnNS0E
# Build a demo
Put together a Gradio App bases on the Huggingface tutorials so that users can generate Beatles-like poetry based on an input prompt.
"""
# Import libraries
from transformers import pipeline
from numpy import random
import gradio as gr
import re
import torch
from torch import autocast
from PIL import Image
import os
# Array with song cover art styles
image_input_styles = ["Random", "Pencil sketch", "Oil painting", "Pop art", "Piet Mondriaan"]
# Get image type for image input
"""
The default setting for the art style dropdown is "Random". The below function determines which style is chosen
If set to "Random", copy the art style array and remove "Random" to prevent "Random" from being a chosen art style
"""
def get_image_input(title, given_input_style):
if given_input_style == 'Random':
image_input_styles_new = image_input_styles.copy()
image_input_styles_new.pop(0)
random_choice = random.randint(len(image_input_styles_new)-1)
final_style = image_input_styles_new[random_choice]
else:
final_style = given_input_style
image_input = 'Cover for ' + title.lower() + ' in style of ' + final_style
return image_input, final_style
# Available models for generate lyrics pipeline
checkpoint = 'wvangils/GPT-Medium-Beatles-Lyrics-finetuned-newlyrics'
# checkpoint = 'wvangils/GPT-Neo-125m-Beatles-Lyrics-finetuned-newlyrics'
# checkpoint = 'wvangils/BLOOM-560m-Beatles-Lyrics-finetuned'
# Setup all the pipelines we need
title_generator = pipeline('summarization', model='czearing/story-to-title')
lyrics_generator = pipeline("text-generation", model=checkpoint)
# For the image generator we use stable diffusion from an existing HuggingFace space
stable_diffusion = gr.Blocks.load(name="spaces/stabilityai/stable-diffusion-1")
# Create 4 images for the given prompt and receive the first one
# This function uses an existing HuggingFace space where the number of created images cannot be modified
def get_image(prompt):
gallery_dir = stable_diffusion(prompt, fn_index=2)
images = [os.path.join(gallery_dir, img) for img in os.listdir(gallery_dir)]
return [images[0]]
# Lyrics generation
def generate_beatles(input_prompt, temperature, top_p, given_input_style):
# Create generator for different models
generated_lyrics = lyrics_generator(input_prompt
, max_length = 100
, num_return_sequences = 1
, return_full_text = True
#, num_beams = 1
#, early_stopping = True # Werkt niet goed lijkt
, temperature = temperature
#, top_k = 50 # Default 50
, top_p = top_p # Default 1.0
, no_repeat_ngram_size = 3 # Default = 0
, repetition_penalty = 1.0 # Default = 1.0
#, do_sample = True # Default = False
)[0]["generated_text"]
# Put lyrics in the right form
lyrics_sentences = re.sub('\n', '. ', generated_lyrics)
# Create a title based on the generated lyrics
title = title_generator(lyrics_sentences, min_length=1, max_length=10, repetition_penalty=2.5)[0]['summary_text']
# Create an image based on the generated title
image_input, image_style = get_image_input(title, given_input_style)
# Generate the image
image = get_image(image_input)
return (title, generated_lyrics, image, image_style)