import argparse import binascii import glob import openai import os import os.path import numpy as np import matplotlib.pyplot as plt import random import sys import tempfile import time import torch from PIL import Image from IPython.display import Audio from diffusers import StableDiffusionPipeline from diffusers import DiffusionPipeline from transformers import pipeline from transformers import ViTFeatureExtractor, ViTForImageClassification from audiodiffusion import AudioDiffusion import requests notes = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"] def fake_gan(): images = [ (random.choice( [ "https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=387&q=80", "https://images.unsplash.com/photo-1554151228-14d9def656e4?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=386&q=80", "https://images.unsplash.com/photo-1542909168-82c3e7fdca5c?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8aHVtYW4lMjBmYWNlfGVufDB8fDB8fA%3D%3D&w=1000&q=80", "https://images.unsplash.com/photo-1546456073-92b9f0a8d413?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=387&q=80", "https://images.unsplash.com/photo-1601412436009-d964bd02edbc?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=464&q=80", ] ), f"label {i}" if i != 0 else "label" * 50) for i in range(3) ] return images def imageClassifier(inputImage): #fn=artist_lib.imageClassifier, #url = 'http://images.cocodataset.org/val2017/000000039769.jpg' #image = Image.open(requests.get(url, stream=True).raw) image = inputImage feature_extractor = ViTFeatureExtractor.from_pretrained('google/vit-base-patch16-224') model = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224') inputs = feature_extractor(images=image, return_tensors="pt") outputs = model(**inputs) logits = outputs.logits # model predicts one of the 1000 ImageNet classes predicted_class_idx = logits.argmax(-1).item() #print("Predicted class:", model.config.id2label[predicted_class_idx]) return "Predicted class:", model.config.id2label[predicted_class_idx] def audioGenerator(inputText): device = "cuda" if torch.cuda.is_available() else "cpu" pipe = DiffusionPipeline.from_pretrained("teticio/audio-diffusion-256").to(device) output = pipe() from IPython.display import display display(output.images[0]) display(Audio(output.audios[0], rate=pipe.mel.get_sample_rate())) print("sample rate is ", pipe.mel.get_sample_rate()) #print(Audio(output.audios[0])) sr=int(pipe.mel.get_sample_rate()) audio=Audio(output.audios[0]) #return int(pipe.mel.get_sample_rate()), Audio(output.audios[0]) return sr, audio def generate_spectrogram_audio_and_loop(model_id): audio_diffusion = AudioDiffusion(model_id=model_id) image, (sample_rate, audio) = audio_diffusion.generate_spectrogram_and_audio() loop = AudioDiffusion.loop_it(audio, sample_rate) if loop is None: loop = audio return image, (sample_rate, audio), (sample_rate, loop) def generate_tone(note, octave, duration): sr = 48000 a4_freq, tones_from_a4 = 440, 12 * (octave - 4) + (note - 9) frequency = a4_freq * 2 ** (tones_from_a4 / 12) duration = int(duration) audio = np.linspace(0, duration, duration * sr) audio = (20000 * np.sin(audio * (2 * np.pi * frequency))).astype(np.int16) return sr, audio def draw(inp, this_model, force_new): device = "cuda" if torch.cuda.is_available() else "cpu" dtype = "torch.float16" if torch.cuda.is_available() else "torch.float32" drawing = inp if this_model == "stable-diffusion-2": this_model_addr = "stabilityai/stable-diffusion-2" images_dir = 'images2/' elif this_model == "stable-diffusion-2-1": this_model_addr = "stabilityai/stable-diffusion-2-1" images_dir = 'images2-1/' elif this_model == "stable-diffusion-v1-5": this_model_addr = "runwayml/stable-diffusion-v1-5" images_dir = 'images/' else: raise gr.Error("Unknown Model!") mkdir_if_not_exist(images_dir) drawing_filename = images_dir + drawing.replace(' ', '_') + '.png' if os.path.exists(drawing_filename): if force_new: new_drawing_filename = images_dir + drawing.replace(' ', '_') + '.' + str(time.time()) + '.png' os.replace(drawing_filename, new_drawing_filename) else: print("found drawing ", drawing_filename) return Image.open(drawing_filename) print("generating drawing '", drawing, "'", drawing_filename) pipe = StableDiffusionPipeline.from_pretrained(this_model_addr, torch_dtype=dtype) pipe.enable_attention_slicing() pipe = pipe.to(device) image = pipe(drawing).images[0] image.seek(0) image.save(drawing_filename) return image def write_blog(inp, this_model, min_length, max_length, force_new): blog_post_name = inp if this_model == "gpt-neo-1.3B": this_model_addr = "EleutherAI/gpt-neo-1.3B" text_dir = 'text1.3/' elif this_model == "gpt-neo-2.7B": this_model_addr = "EleutherAI/gpt-neo-2.7B" text_dir = 'text2.7/' else: raise gr.Error("Unknown Model!") mkdir_if_not_exist(text_dir) target_filename = text_dir + blog_post_name.replace(' ', '_') + '.txt' if os.path.exists(target_filename): if force_new: new_target_filename = text_dir + blog_post_name.replace(' ', '_') + '.' + str(time.time()) + '.txt' os.replace(target_filename, new_target_filename) else: print("found drawing ", target_filename) with open(target_filename, 'r') as file: return file.read() print("generating blog '", blog_post_name, "'", target_filename) device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") dtype = "torch.float16" if torch.cuda.is_available() else "torch.float32" #generator = pipeline('text-generation', model='EleutherAI/gpt-neo-2.7B', device=device, torch_dtype=dtype) #generator = pipeline('text-generation', model=this_model_addr, torch_dtype=dtype) #generator = pipeline('text-generation', model=this_model_addr) generator = pipeline('text-generation', model=this_model_addr, device=device, torch_dtype=dtype) # AttributeError: 'TextGenerationPipeline' object has no attribute 'enable_attention_slicing' #generator.enable_attention_slicing() res = generator(blog_post_name, min_length=min_length, max_length=max_length, do_sample=True, temperature=0.7) blog_post_text = res[0]['generated_text'] with open(target_filename, 'w') as file: file.write(blog_post_text) return blog_post_text def nameMyPet(inp): animal = inp response = openai.Completion.create( model="text-davinci-003", prompt=generate_prompt(animal), temperature=0.6, ) return response.choices[0].text def mkdir_if_not_exist(path): if os.path.exists(path): return 0 else: os.mkdir(path) def generate_prompt(animal): return """Suggest three names for an animal that is a superhero. Animal: Cat Names: Captain Sharpclaw, Agent Fluffball, The Incredible Feline Animal: Dog Names: Ruff the Protector, Wonder Canine, Sir Barks-a-Lot Animal: {} Names:""".format( animal.capitalize() )