File size: 7,527 Bytes
158667b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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):
    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=torch.float16)
    pipe.enable_attention_slicing()
    pipe = pipe.to("cuda")
    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")
    #generator = pipeline('text-generation', model='EleutherAI/gpt-neo-2.7B', device=device, torch_dtype=torch.float16)
    #generator = pipeline('text-generation', model=this_model_addr, torch_dtype=torch.float16)
    #generator = pipeline('text-generation', model=this_model_addr)
    generator = pipeline('text-generation', model=this_model_addr, device=device, torch_dtype=torch.float16)
    # 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()
    )