Update app.py
Browse files
app.py
CHANGED
@@ -7,14 +7,14 @@ import random
|
|
7 |
import bg
|
8 |
from rembg import remove as rm
|
9 |
import datetime
|
10 |
-
from huggingface_hub import (HfApi)
|
11 |
import uuid
|
12 |
import json
|
13 |
import requests
|
14 |
uid=uuid.uuid4()
|
15 |
proc1=diff
|
16 |
proc2=diff
|
17 |
-
|
18 |
save_data='https://huggingface.co/datasets/Omnibus/game-data-1/raw/main/'
|
19 |
token_self = os.environ['HF_TOKEN']
|
20 |
api=HfApi(token=token_self)
|
@@ -26,7 +26,30 @@ platform = 'https://huggingface.co/spaces/Omnibus/game-test/resolve/main/assets/
|
|
26 |
star = 'https://huggingface.co/spaces/Omnibus/game-test/resolve/main/assets/star.png'
|
27 |
bomb = 'https://huggingface.co/spaces/Omnibus/game-test/resolve/main/assets/bomb.png'
|
28 |
dude = 'https://huggingface.co/spaces/Omnibus/game-test/resolve/main/assets/dude.png'
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
|
32 |
def game_fn(sky=sky,star=star,bomb=bomb,platform=platform,dude=dude):
|
@@ -96,8 +119,18 @@ def enemy_fn(inp):
|
|
96 |
out_url = f'https://omnibus-game-test.hf.space/file={out_file}'
|
97 |
return out,out_url
|
98 |
|
99 |
-
def save_game(background_img,star_img,enemy_img,platform_img,dude_img):
|
100 |
user_repo=save_data.split('datasets/',1)[1].split('/raw',1)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
#out_lod=[]
|
102 |
try:
|
103 |
r = requests.get(f'{save_data}game_data.json')
|
@@ -107,13 +140,12 @@ def save_game(background_img,star_img,enemy_img,platform_img,dude_img):
|
|
107 |
except:
|
108 |
lod=[]
|
109 |
pass
|
110 |
-
|
111 |
-
timename=timestamp.replace(" ","--").replace(":","-").replace(".","-")
|
112 |
print(timestamp)
|
113 |
try:
|
114 |
api.upload_file(
|
115 |
path_or_fileobj=background_img,
|
116 |
-
path_in_repo=f"{save_data.split('main/',1)[1]}/images/background/{
|
117 |
repo_id=save_data.split('datasets/',1)[1].split('/raw',1)[0],
|
118 |
token=token_self,
|
119 |
repo_type="dataset",
|
@@ -124,7 +156,7 @@ def save_game(background_img,star_img,enemy_img,platform_img,dude_img):
|
|
124 |
try:
|
125 |
api.upload_file(
|
126 |
path_or_fileobj=star_img,
|
127 |
-
path_in_repo=f"{save_data.split('main/',1)[1]}/images/star/{
|
128 |
repo_id=save_data.split('datasets/',1)[1].split('/raw',1)[0],
|
129 |
token=token_self,
|
130 |
repo_type="dataset",
|
@@ -135,7 +167,7 @@ def save_game(background_img,star_img,enemy_img,platform_img,dude_img):
|
|
135 |
try:
|
136 |
api.upload_file(
|
137 |
path_or_fileobj=enemy_img,
|
138 |
-
path_in_repo=f"{save_data.split('main/',1)[1]}/images/enemy/{
|
139 |
repo_id=save_data.split('datasets/',1)[1].split('/raw',1)[0],
|
140 |
token=token_self,
|
141 |
repo_type="dataset",
|
@@ -145,9 +177,11 @@ def save_game(background_img,star_img,enemy_img,platform_img,dude_img):
|
|
145 |
pass
|
146 |
block = {'index': len(lod) + 1,
|
147 |
'timestamp': timestamp,
|
148 |
-
'
|
149 |
-
'
|
150 |
-
'
|
|
|
|
|
151 |
#'platform_url': f'https://huggingface.co/datasets/{repo}/{space}/raw/main/{platform_img}',
|
152 |
#'dude_url': f'https://huggingface.co/datasets/{repo}/{space}/raw/main/{dude_img}',
|
153 |
}
|
@@ -169,7 +203,38 @@ def save_game(background_img,star_img,enemy_img,platform_img,dude_img):
|
|
169 |
print("success")
|
170 |
except Exception as e:
|
171 |
print (e)
|
172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
score_js="""
|
174 |
|
175 |
function(text_input) {
|
@@ -204,25 +269,27 @@ with gr.Blocks() as app:
|
|
204 |
update_game=gr.Button("Make Game")
|
205 |
#start_prompt=gr.Textbox(value="beautiful landscape, real, 8k",visible=False)
|
206 |
html_game = gr.HTML()
|
207 |
-
|
|
|
|
|
|
|
208 |
get_score=gr.Button("Get Score")
|
|
|
|
|
|
|
209 |
with gr.Row():
|
210 |
text_input=gr.Textbox()
|
211 |
url_params=gr.Textbox()
|
212 |
-
|
213 |
def return_score(text):
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
out=gr.update(visible=False)
|
218 |
-
#out=gr.HTML.update("""<head><meta http-equiv="refresh" content="0;URL='https://huggingface.co/spaces"></head>""")'''
|
219 |
-
return ["" + text + ""]
|
220 |
-
get_score.click(return_score,text_input,[text_input],_js=score_js)
|
221 |
-
save_btn.click(save_game,[out_im_sky,out_im_star,out_im_enemy,out_im_enemy,out_im_enemy],None)
|
222 |
update_game.click(game_fn,[out_sky_url,out_star_url,out_enemy_url],html_game)
|
223 |
btn_sky.click(sky_fn,prompt_sky,[out_im_sky,out_sky_url])
|
224 |
btn_star.click(star_fn,prompt_star,[out_im_star,out_star_url])
|
225 |
btn_enemy.click(enemy_fn,prompt_enemy,[out_im_enemy,out_enemy_url])
|
|
|
226 |
|
227 |
#app.load(sky_fn,prompt_sky,[out_im_sky,out_sky_url]).then(game_fn,[out_sky_url],html_game)
|
228 |
app.queue(concurrency_count=10).launch()
|
|
|
7 |
import bg
|
8 |
from rembg import remove as rm
|
9 |
import datetime
|
10 |
+
from huggingface_hub import (HfApi,file_exists)
|
11 |
import uuid
|
12 |
import json
|
13 |
import requests
|
14 |
uid=uuid.uuid4()
|
15 |
proc1=diff
|
16 |
proc2=diff
|
17 |
+
this_url='https://omnibus-game-test.hf.space/'
|
18 |
save_data='https://huggingface.co/datasets/Omnibus/game-data-1/raw/main/'
|
19 |
token_self = os.environ['HF_TOKEN']
|
20 |
api=HfApi(token=token_self)
|
|
|
26 |
star = 'https://huggingface.co/spaces/Omnibus/game-test/resolve/main/assets/star.png'
|
27 |
bomb = 'https://huggingface.co/spaces/Omnibus/game-test/resolve/main/assets/bomb.png'
|
28 |
dude = 'https://huggingface.co/spaces/Omnibus/game-test/resolve/main/assets/dude.png'
|
29 |
+
def predict(text, url_params):
|
30 |
+
load_game=url_params.get('game')
|
31 |
+
print (load_game)
|
32 |
+
if load_game != None:
|
33 |
+
try:
|
34 |
+
r = requests.get(f'{save_data}/games/{load_game}.omnigame')
|
35 |
+
lod = json.loads(r.text)
|
36 |
+
#out_lod.append(lod)
|
37 |
+
print (lod)
|
38 |
+
except:
|
39 |
+
print("cannot load game")
|
40 |
+
pass
|
41 |
+
|
42 |
+
else:
|
43 |
+
pass
|
44 |
+
return ["" + text + "", 'out']
|
45 |
+
get_window_url_params = """
|
46 |
+
function(text_input, url_params) {
|
47 |
+
console.log(text_input, url_params);
|
48 |
+
const params = new URLSearchParams(window.location.search);
|
49 |
+
url_params = Object.fromEntries(params);
|
50 |
+
return [text_input, url_params];
|
51 |
+
}
|
52 |
+
"""
|
53 |
|
54 |
|
55 |
def game_fn(sky=sky,star=star,bomb=bomb,platform=platform,dude=dude):
|
|
|
119 |
out_url = f'https://omnibus-game-test.hf.space/file={out_file}'
|
120 |
return out,out_url
|
121 |
|
122 |
+
def save_game(name,score,background_img,star_img,enemy_img,platform_img,dude_img):
|
123 |
user_repo=save_data.split('datasets/',1)[1].split('/raw',1)[0]
|
124 |
+
timestamp=str(datetime.datetime.now())
|
125 |
+
timename=timestamp.replace(" ","--").replace(":","-").replace(".","-")
|
126 |
+
if name != "":
|
127 |
+
if file_exists(f"{user_repo}", f"games/{name}.omnigame",repo_type="dataset"):
|
128 |
+
return "Game Saved with this Name. Choose a different Name."
|
129 |
+
else:
|
130 |
+
game=name
|
131 |
+
else:
|
132 |
+
game=timename
|
133 |
+
|
134 |
#out_lod=[]
|
135 |
try:
|
136 |
r = requests.get(f'{save_data}game_data.json')
|
|
|
140 |
except:
|
141 |
lod=[]
|
142 |
pass
|
143 |
+
|
|
|
144 |
print(timestamp)
|
145 |
try:
|
146 |
api.upload_file(
|
147 |
path_or_fileobj=background_img,
|
148 |
+
path_in_repo=f"{save_data.split('main/',1)[1]}/images/background/{game}-background_img.png",
|
149 |
repo_id=save_data.split('datasets/',1)[1].split('/raw',1)[0],
|
150 |
token=token_self,
|
151 |
repo_type="dataset",
|
|
|
156 |
try:
|
157 |
api.upload_file(
|
158 |
path_or_fileobj=star_img,
|
159 |
+
path_in_repo=f"{save_data.split('main/',1)[1]}/images/star/{game}-star_img.png",
|
160 |
repo_id=save_data.split('datasets/',1)[1].split('/raw',1)[0],
|
161 |
token=token_self,
|
162 |
repo_type="dataset",
|
|
|
167 |
try:
|
168 |
api.upload_file(
|
169 |
path_or_fileobj=enemy_img,
|
170 |
+
path_in_repo=f"{save_data.split('main/',1)[1]}/images/enemy/{game}-enemy_img.png",
|
171 |
repo_id=save_data.split('datasets/',1)[1].split('/raw',1)[0],
|
172 |
token=token_self,
|
173 |
repo_type="dataset",
|
|
|
177 |
pass
|
178 |
block = {'index': len(lod) + 1,
|
179 |
'timestamp': timestamp,
|
180 |
+
'game_name': game,
|
181 |
+
'score': score,
|
182 |
+
'background_url': f'https://huggingface.co/datasets/{user_repo}/resolve/main/images/background/{game}-background_img.png',
|
183 |
+
'star_url': f'https://huggingface.co/datasets/{user_repo}/resolve/main/images/star/{game}-star_img.png',
|
184 |
+
'enemy_url': f'https://huggingface.co/datasets/{user_repo}/resolve/main/images/enemy/{game}-enemy_img.png',
|
185 |
#'platform_url': f'https://huggingface.co/datasets/{repo}/{space}/raw/main/{platform_img}',
|
186 |
#'dude_url': f'https://huggingface.co/datasets/{repo}/{space}/raw/main/{dude_img}',
|
187 |
}
|
|
|
203 |
print("success")
|
204 |
except Exception as e:
|
205 |
print (e)
|
206 |
+
return f'Error Saving Game: {e}'
|
207 |
+
block = {'game_name': game,
|
208 |
+
'timestamp': timestamp,
|
209 |
+
'background_url': f'https://huggingface.co/datasets/{user_repo}/resolve/main/images/background/{game}-background_img.png',
|
210 |
+
'star_url': f'https://huggingface.co/datasets/{user_repo}/resolve/main/images/star/{game}-star_img.png',
|
211 |
+
'enemy_url': f'https://huggingface.co/datasets/{user_repo}/resolve/main/images/enemy/{game}-enemy_img.png',
|
212 |
+
#'platform_url': f'https://huggingface.co/datasets/{repo}/{space}/raw/main/{platform_img}',
|
213 |
+
#'dude_url': f'https://huggingface.co/datasets/{repo}/{space}/raw/main/{dude_img}',
|
214 |
+
}
|
215 |
+
print(block)
|
216 |
+
lod.append(block)
|
217 |
+
|
218 |
+
json_object = json.dumps(lod, indent=4)
|
219 |
+
with open("tmp2.json", "w") as outfile:
|
220 |
+
outfile.write(json_object)
|
221 |
+
try:
|
222 |
+
api.upload_file(
|
223 |
+
path_or_fileobj="tmp2.json",
|
224 |
+
path_in_repo=f"{save_data.split('main/',1)[1]}/games/{game}.omnigame",
|
225 |
+
repo_id=save_data.split('datasets/',1)[1].split('/raw',1)[0],
|
226 |
+
token=token_self,
|
227 |
+
repo_type="dataset",
|
228 |
+
)
|
229 |
+
os.remove("tmp2.json")
|
230 |
+
print("success")
|
231 |
+
game_url=f'{this_url}?game={game}'
|
232 |
+
except Exception as e:
|
233 |
+
print (e)
|
234 |
+
return f'Error Saving Game: {e}'
|
235 |
+
return f"Game Saved\nURL: {game_url}"
|
236 |
+
|
237 |
+
|
238 |
score_js="""
|
239 |
|
240 |
function(text_input) {
|
|
|
269 |
update_game=gr.Button("Make Game")
|
270 |
#start_prompt=gr.Textbox(value="beautiful landscape, real, 8k",visible=False)
|
271 |
html_game = gr.HTML()
|
272 |
+
with gr.Row():
|
273 |
+
game_name=gr.Textbox(label="Name for Game (optional)")
|
274 |
+
save_btn=gr.Button("Save")
|
275 |
+
message=gr.Textbox(label="Status",interactive=False)
|
276 |
get_score=gr.Button("Get Score")
|
277 |
+
with gr.Row():
|
278 |
+
score=gr.Textbox()
|
279 |
+
url_params=gr.Textbox()
|
280 |
with gr.Row():
|
281 |
text_input=gr.Textbox()
|
282 |
url_params=gr.Textbox()
|
283 |
+
acc=gr.Textbox()
|
284 |
def return_score(text):
|
285 |
+
return text
|
286 |
+
get_score.click(return_score,score,[score],_js=score_js)
|
287 |
+
save_btn.click(save_game,[game_name,score,out_im_sky,out_im_star,out_im_enemy,out_im_enemy,out_im_enemy],message)
|
|
|
|
|
|
|
|
|
|
|
288 |
update_game.click(game_fn,[out_sky_url,out_star_url,out_enemy_url],html_game)
|
289 |
btn_sky.click(sky_fn,prompt_sky,[out_im_sky,out_sky_url])
|
290 |
btn_star.click(star_fn,prompt_star,[out_im_star,out_star_url])
|
291 |
btn_enemy.click(enemy_fn,prompt_enemy,[out_im_enemy,out_enemy_url])
|
292 |
+
app.load(predict,[text_input,url_params],[text_output,acc], _js=get_window_url_params)
|
293 |
|
294 |
#app.load(sky_fn,prompt_sky,[out_im_sky,out_sky_url]).then(game_fn,[out_sky_url],html_game)
|
295 |
app.queue(concurrency_count=10).launch()
|