BigGAN / app.py
vasudevgupta's picture
Update app.py
83408b5
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()