Spaces:
Runtime error
Runtime error
Refine code and use text instead of file
Browse files123456712 unmodified lines20212223242526 unmodified lines52535455565758596019 unmodified lines8081828384858687888920 unmodified lines11011111211311411511611711811912012112212341 unmodified lines1651661671681691701711721731 unmodified line17517617717817918018118218318413 unmodified lines19819920020120220320420520620720820921023 unmodified lines23423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426511 unmodified lines27727827928028128228328428528628712 unmodified lines300301302303304305306307308309310311312313314315316317318319from huggingface_hub import hf_hub_url, cached_downloadimport streamlit as stimport ioimport gc######################################################################################################### The RWKV Language Model - https://github.com/BlinkDL/RWKV-LM12 unmodified linesdevice = 'cuda' if torch.cuda.is_available() else 'cpu'class ToBinary(torch.autograd.Function):26 unmodified linesclass REncoderSmall(nn.Module):def __init__(self, args):super().__init__()self.args = argsdd = 8self.Bxx = nn.BatchNorm2d(dd * 64)19 unmodified linesself.C22 = nn.Conv2d(dd * 64, 256, kernel_size=3, padding=1)self.C23 = nn.Conv2d(256, dd * 64, kernel_size=3, padding=1)self.COUT = nn.Conv2d(dd * 64,args.my_img_bit,kernel_size=3,padding=1)def forward(self, img):ACT = F.mish20 unmodified linesclass RDecoderSmall(nn.Module):def __init__(self, args):super().__init__()self.args = argsdd = 8self.CIN = nn.Conv2d(args.my_img_bit,dd * 64,kernel_size=3,padding=1)self.B00 = nn.BatchNorm2d(dd * 64)self.C00 = nn.Conv2d(dd * 64, 256, kernel_size=3, padding=1)41 unmodified linesclass REncoderLarge(nn.Module):def __init__(self, args, dd, ee, ff):super().__init__()self.args = argsself.CXX = nn.Conv2d(3, dd, kernel_size=3, padding=1)self.BXX = nn.BatchNorm2d(dd)self.CX0 = nn.Conv2d(dd, ee, kernel_size=3, padding=1)1 unmodified lineself.R0 = ResBlock(dd * 4, ff)self.R1 = ResBlock(dd * 16, ff)self.R2 = ResBlock(dd * 64, ff)self.CZZ = nn.Conv2d(dd * 64,args.my_img_bit,kernel_size=3,padding=1)def forward(self, x):ACT = F.mish13 unmodified linesclass RDecoderLarge(nn.Module):def __init__(self, args, dd, ee, ff):super().__init__()self.args = argsself.CZZ = nn.Conv2d(args.my_img_bit,dd * 64,kernel_size=3,padding=1)self.BZZ = nn.BatchNorm2d(dd * 64)self.R0 = ResBlock(dd * 64, ff)self.R1 = ResBlock(dd * 16, ff)23 unmodified linesgc.collect()if model_prefix == 'out-v7c_d8_256-224-13bit-OB32x0.5-745':R_ENCODER, R_DECODER = REncoderSmall, RDecoderSmallelse:if 'd16_512' in model_prefix:dd, ee, ff = 16, 64, 512elif 'd32_1024' in model_prefix:dd, ee, ff = 32, 128, 1024R_ENCODER, R_DECODER = ((lambda args: REncoderLarge(args, dd, ee, ff)),(lambda args: RDecoderLarge(args, dd, ee, ff)))args = types.SimpleNamespace()args.my_img_bit = 13encoder = R_ENCODER(args).eval().to(device)decoder = R_DECODER(args).eval().to(device)zpow = torch.tensor([2**i for i in range(0, 13)]).reshape(13, 1, 1)zpow = zpow.to(device).long()encoder.load_state_dict(torch.load(cached_download(hf_hub_url(MODEL_REPO, f'{model_prefix}-E.pth'))))decoder.load_state_dict(torch.load(cached_download(hf_hub_url(MODEL_REPO, f'{model_prefix}-D.pth'))))encoder.eval()decoder.eval()return encoder, decoder11 unmodified linesz = encoder(img)z = ToBinary.apply(z)return z.cpu().numpy()def decode(model_prefix, z):_, decoder = prepare_model(model_prefix)decoded = decoder(torch.Tensor(z).to(device))return VF.to_pil_image(decoded[0])12 unmodified linesif uploaded_file is not None:image = Image.open(uploaded_file)col_in.image(image, 'Input Image')z = encode(model_prefix, image)with io.BytesIO() as buffer:np.save(buffer, z)col_out.download_button(label="Download Encoded Data",data=buffer,file_name=uploaded_file.name + '.npy',)col_out.image(decode(model_prefix, z), 'Output Image preview')with decoder_tab:col_in, col_out = st.columns(2)uploaded_file = col_in.file_uploader('Choose an Encoded Data')if uploaded_file is not None:z = np.load(uploaded_file)image = decode(model_prefix, z)col_out.image(image, 'Output Image')12345678912 unmodified lines222324252627282926 unmodified lines565758596061626319 unmodified lines8384858687888920 unmodified lines11011111211311411511611711811941 unmodified lines1611621631641651661671681 unmodified line17017117217317417517613 unmodified lines19019119219319419519619719823 unmodified lines22222322422522622722822923023123223323423523623723823924024124224311 unmodified lines25525625725825926026126226326426526626726826927027127227327427527627712 unmodified lines290291292293294295296297298299300301302303import base64from huggingface_hub import hf_hub_downloadimport streamlit as stimport ioimport gcimport json######################################################################################################### The RWKV Language Model - https://github.com/BlinkDL/RWKV-LM12 unmodified linesdevice = 'cuda' if torch.cuda.is_available() else 'cpu'IMG_BITS = 13class ToBinary(torch.autograd.Function):26 unmodified linesclass REncoderSmall(nn.Module):def __init__(self):super().__init__()dd = 8self.Bxx = nn.BatchNorm2d(dd * 64)19 unmodified linesself.C22 = nn.Conv2d(dd * 64, 256, kernel_size=3, padding=1)self.C23 = nn.Conv2d(256, dd * 64, kernel_size=3, padding=1)self.COUT = nn.Conv2d(dd * 64, IMG_BITS, kernel_size=3, padding=1)def forward(self, img):ACT = F.mish20 unmodified linesclass RDecoderSmall(nn.Module):def __init__(self):super().__init__()dd = 8self.CIN = nn.Conv2d(IMG_BITS, dd * 64, kernel_size=3, padding=1)self.B00 = nn.BatchNorm2d(dd * 64)self.C00 = nn.Conv2d(dd * 64, 256, kernel_size=3, padding=1)41 unmodified linesclass REncoderLarge(nn.Module):def __init__(self, dd, ee, ff):super().__init__()self.CXX = nn.Conv2d(3, dd, kernel_size=3, padding=1)self.BXX = nn.BatchNorm2d(dd)self.CX0 = nn.Conv2d(dd, ee, kernel_size=3, padding=1)1 unmodified lineself.R0 = ResBlock(dd * 4, ff)self.R1 = ResBlock(dd * 16, ff)self.R2 = ResBlock(dd * 64, ff)self.CZZ = nn.Conv2d(dd * 64, IMG_BITS, kernel_size=3, padding=1)def forward(self, x):ACT = F.mish13 unmodified linesclass RDecoderLarge(nn.Module):def __init__(self, dd, ee, ff):super().__init__()self.CZZ = nn.Conv2d(IMG_BITS, dd * 64, kernel_size=3, padding=1)self.BZZ = nn.BatchNorm2d(dd * 64)self.R0 = ResBlock(dd * 64, ff)self.R1 = ResBlock(dd * 16, ff)23 unmodified linesgc.collect()if model_prefix == 'out-v7c_d8_256-224-13bit-OB32x0.5-745':R_ENCODER, R_DECODER = REncoderSmall(), RDecoderSmall()else:if 'd16_512' in model_prefix:dd, ee, ff = 16, 64, 512elif 'd32_1024' in model_prefix:dd, ee, ff = 32, 128, 1024R_ENCODER = REncoderLarge(dd, ee, ff)R_DECODER = RDecoderLarge(dd, ee, ff)encoder = R_ENCODER.eval().to(device)decoder = R_DECODER.eval().to(device)encoder.load_state_dict(torch.load(hf_hub_download(MODEL_REPO, f'{model_prefix}-E.pth')))decoder.load_state_dict(torch.load(hf_hub_download(MODEL_REPO, f'{model_prefix}-D.pth')))return encoder, decoder11 unmodified linesz = encoder(img)z = ToBinary.apply(z)with io.BytesIO() as buffer:np.save(buffer, np.packbits(z.cpu().numpy().astype('bool')))z_b64 = base64.b64encode(buffer.getvalue()).decode()return json.dumps({"shape": list(z.shape), "data": z_b64})def decode(model_prefix, z_str):_, decoder = prepare_model(model_prefix)z_json = json.loads(z_str)with io.BytesIO() as buffer:buffer.write(base64.b64decode(z_json["data"]))buffer.seek(0)z = np.load(buffer)z = np.unpackbits(z).astype('float').reshape(z_json["shape"])decoded = decoder(torch.Tensor(z).to(device))return VF.to_pil_image(decoded[0])12 unmodified linesif uploaded_file is not None:image = Image.open(uploaded_file)col_in.image(image, 'Input Image')z_str = encode(model_prefix, image)col_out.write("Encoded to:")col_out.code(z_str,language=None)col_out.image(decode(model_prefix, z_str), 'Output Image preview')with decoder_tab:col_in, col_out = st.columns(2)z_str = col_in.text_area('Paste encoded string here:')if len(z_str) > 0:image = decode(model_prefix, z_str)col_out.image(image, 'Output Image')
app.py+41-57