File size: 1,365 Bytes
ecc5d7e
 
 
 
0c9f7d2
ecc5d7e
c7b5438
ecc5d7e
 
 
420668f
ecc5d7e
 
957bbf8
83408b5
ecc5d7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f330dd
4f0d358
 
 
 
 
ecc5d7e
8f330dd
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
import os
import subprocess

GIT_TOKEN = os.environ.get("GIT_TOKEN")
GIT_USER = os.environ.get("GIT_USER")

lib_url = f"git+https://{GIT_USER}:{GIT_TOKEN}@github.com/vasudevgupta7/huggingface-task@main"
subprocess.run(f"pip3 install -q {lib_url}".split())

import torch
import numpy as np
import gradio as gr
from transformers import AutoModel, AutoTokenizer
from pytorch_pretrained_biggan import BigGAN
from huggingface_task.models import AutoEncoder
from huggingface_task.run_model import generate_image_from_text

biggan_id = 'biggan-deep-128'
text_encoder_id = "distilbert-base-uncased"
autoencoder_id = "vasudevgupta/biggan-mapping-model"

text_tokenizer = AutoTokenizer.from_pretrained(text_encoder_id)
text_model = AutoModel.from_pretrained(text_encoder_id)

autoencoder = AutoEncoder.from_pretrained(autoencoder_id)
biggan = BigGAN.from_pretrained(biggan_id)

device = "cuda" if torch.cuda.is_available() else "cpu"
biggan.to(device).eval()
text_model.to(device).eval()
autoencoder.to(device).eval()

def generate_image(text_query):
  array = generate_image_from_text(text_query, text_tokenizer, text_model, autoencoder, biggan, device=device)
  array = ((array + 1.0) / 2.0) * 256
  array.clip(0, 255, out=array)
  array = np.asarray(np.uint8(array), dtype=np.uint8)
  return array

gr.Interface(fn=generate_image, inputs="text", outputs="image").launch()