AlekseyKorshuk commited on
Commit
e774b65
β€’
1 Parent(s): 52f8f2b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -17
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import subprocess
2
  from pathlib import Path
3
  import einops
4
- import gradio as gr
5
  import numpy as np
6
  import torch
7
  from huggingface_hub import hf_hub_download
@@ -9,9 +8,11 @@ from PIL import Image
9
  from torch import nn
10
  from torchvision.utils import save_image
11
  from huggingface_hub.hf_api import HfApi
 
12
 
13
  hfapi = HfApi()
14
 
 
15
  class Generator(nn.Module):
16
  def __init__(self, num_channels=4, latent_dim=100, hidden_size=64):
17
  super(Generator, self).__init__()
@@ -45,7 +46,7 @@ class Generator(nn.Module):
45
 
46
 
47
  @torch.no_grad()
48
- def interpolate(save_dir='./lerp/', frames=100, rows=8, cols=8):
49
  save_dir = Path(save_dir)
50
  save_dir.mkdir(exist_ok=True, parents=True)
51
 
@@ -80,7 +81,7 @@ def predict(model_name, choice, seed):
80
  torch.manual_seed(seed)
81
 
82
  if choice == 'interpolation':
83
- interpolate()
84
  return 'out.gif'
85
  else:
86
  z = torch.randn(64, 100, 1, 1)
@@ -92,17 +93,117 @@ def predict(model_name, choice, seed):
92
  return 'image.png'
93
 
94
 
95
- models = [model.modelId[model.modelId.index("/") + 1:] for model in hfapi.list_models(author="huggingnft")]
96
- gr.Interface(
97
- predict,
98
- inputs=[
99
- gr.inputs.Dropdown(models, label='Model'),
100
- gr.inputs.Dropdown(['image', 'interpolation'], label='Output Type'),
101
- gr.inputs.Slider(label='Seed', minimum=0, maximum=1000, default=42),
102
- ],
103
- outputs="image",
104
- title="Cryptopunks GAN",
105
- description="These CryptoPunks do not exist. You have the choice of either generating random punks, or a gif showing the interpolation between two random punk grids.",
106
- article="<p style='text-align: center'><a href='https://arxiv.org/pdf/1511.06434.pdf'>Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks</a> | <a href='https://github.com/teddykoker/cryptopunks-gan'>Github Repo</a></p>",
107
- examples=[["interpolation", 100], ["interpolation", 500], ["image", 100], ["image", 500]],
108
- ).launch(cache_examples=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import subprocess
2
  from pathlib import Path
3
  import einops
 
4
  import numpy as np
5
  import torch
6
  from huggingface_hub import hf_hub_download
 
8
  from torch import nn
9
  from torchvision.utils import save_image
10
  from huggingface_hub.hf_api import HfApi
11
+ import streamlit as st
12
 
13
  hfapi = HfApi()
14
 
15
+
16
  class Generator(nn.Module):
17
  def __init__(self, num_channels=4, latent_dim=100, hidden_size=64):
18
  super(Generator, self).__init__()
 
46
 
47
 
48
  @torch.no_grad()
49
+ def interpolate(model, save_dir='./lerp/', frames=100, rows=8, cols=8):
50
  save_dir = Path(save_dir)
51
  save_dir.mkdir(exist_ok=True, parents=True)
52
 
 
81
  torch.manual_seed(seed)
82
 
83
  if choice == 'interpolation':
84
+ interpolate(model)
85
  return 'out.gif'
86
  else:
87
  z = torch.randn(64, 100, 1, 1)
 
93
  return 'image.png'
94
 
95
 
96
+ model_names = [model.modelId[model.modelId.index("/") + 1:] for model in hfapi.list_models(author="huggingnft")]
97
+
98
+ st.set_page_config(page_title="Hugging NFT")
99
+
100
+ st.title("Hugging NFT")
101
+ st.sidebar.markdown(
102
+ """
103
+ <style>
104
+ .aligncenter {
105
+ text-align: center;
106
+ }
107
+ </style>
108
+ <p class="aligncenter">
109
+ <img src="https://raw.githubusercontent.com/AlekseyKorshuk/optimum-transformers/master/data/social_preview.png" width="300" />
110
+ </p>
111
+ """,
112
+ unsafe_allow_html=True,
113
+ )
114
+ st.sidebar.markdown(
115
+ """
116
+ <style>
117
+ .aligncenter {
118
+ text-align: center;
119
+ }
120
+ </style>
121
+
122
+ <p style='text-align: center'>
123
+ <a href="https://github.com/AlekseyKorshuk/huggingnft" target="_blank">GitHub</a>
124
+ </p>
125
+
126
+ <p class="aligncenter">
127
+ <a href="https://github.com/AlekseyKorshuk/huggingnft" target="_blank">
128
+ <img src="https://img.shields.io/github/stars/AlekseyKorshuk/huggingnft?style=social"/>
129
+ </a>
130
+ </p>
131
+ <p class="aligncenter">
132
+ <a href="https://twitter.com/alekseykorshuk" target="_blank">
133
+ <img src="https://img.shields.io/twitter/follow/alekseykorshuk?style=social"/>
134
+ </a>
135
+ </p>
136
+ """,
137
+ unsafe_allow_html=True,
138
+ )
139
+
140
+ st.markdown(
141
+ "πŸ€— [Hugging NFT](https://github.com/AlekseyKorshuk/huggingnft) - Generate NFT by OpenSea collection name.")
142
+
143
+ st.markdown(
144
+ "πŸš€οΈ SN-GAN used to train all models.")
145
+
146
+ st.markdown(
147
+ "⁉️ Want to train your model? Check [project repository](https://github.com/AlekseyKorshuk/huggingnft) and make in in few clicks!")
148
+ #
149
+ # st.markdown("πŸš€ Up to 1ms on Bert-based transformers")
150
+ #
151
+ # st.markdown(
152
+ # "‼️ NOTE: This Space **does not show** the real power of this project because: low recources, not possbile to optimize models. Check [project repository](https://github.com/AlekseyKorshuk/optimum-transformers) with real bechmarks!")
153
+
154
+ # st.sidebar.header("Settings:")
155
+ model_name = st.selectbox(
156
+ 'Choose model:',
157
+ model_names)
158
+
159
+ output_type = st.selectbox(
160
+ 'Output type:',
161
+ ['image', 'interpolation'])
162
+
163
+ seed_value = st.slider("Seed:",
164
+ min_value=1,
165
+ max_value=1000,
166
+ step=1,
167
+ value=100,
168
+ )
169
+
170
+ model_html = """
171
+
172
+ <div class="inline-flex flex-col" style="line-height: 1.5;">
173
+ <div class="flex">
174
+ <div
175
+ \t\t\tstyle="display:DISPLAY_1; margin-left: auto; margin-right: auto; width: 92px; height:92px; border-radius: 50%; background-size: cover; background-image: url(&#39;USER_PROFILE&#39;)">
176
+ </div>
177
+ </div>
178
+ <div style="text-align: center; margin-top: 3px; font-size: 16px; font-weight: 800">πŸ€– HuggingArtists Model πŸ€–</div>
179
+ <div style="text-align: center; font-size: 16px; font-weight: 800">USER_NAME</div>
180
+ <a href="https://genius.com/artists/USER_HANDLE">
181
+ \t<div style="text-align: center; font-size: 14px;">@USER_HANDLE</div>
182
+ </a>
183
+ </div>
184
+ """
185
+
186
+ if st.button("Run"):
187
+ with st.spinner(text=f"Generating..."):
188
+ st.image(predict(model_name, output_type, seed_value))
189
+ st.subheader("Please star project repository, this space and follow my Twitter:")
190
+ st.markdown(
191
+ """
192
+ <style>
193
+ .aligncenter {
194
+ text-align: center;
195
+ }
196
+ </style>
197
+ <p class="aligncenter">
198
+ <a href="https://github.com/AlekseyKorshuk/huggingnft" target="_blank">
199
+ <img src="https://img.shields.io/github/stars/AlekseyKorshuk/huggingnft?style=social"/>
200
+ </a>
201
+ </p>
202
+ <p class="aligncenter">
203
+ <a href="https://twitter.com/alekseykorshuk" target="_blank">
204
+ <img src="https://img.shields.io/twitter/follow/alekseykorshuk?style=social"/>
205
+ </a>
206
+ </p>
207
+ """,
208
+ unsafe_allow_html=True,
209
+ )