update
Browse files- app.py +132 -9
- requirements.txt +3 -1
app.py
CHANGED
@@ -1,13 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
|
10 |
-
|
11 |
-
description=description,
|
12 |
-
examples=examples)
|
13 |
-
iface.launch()
|
|
|
1 |
+
#!/usr/local/bin/python3
|
2 |
+
# -*- coding: utf-8 -*-
|
3 |
+
|
4 |
+
import time
|
5 |
+
import os
|
6 |
+
import requests
|
7 |
import gradio as gr
|
8 |
+
import simplejson as json
|
9 |
+
import oss2
|
10 |
+
|
11 |
+
CSS = '.gradio-container a {color:#b7adf4 !important}'
|
12 |
+
HEADERS = {'app-key': os.environ['a3'], 'origin': os.environ['a4'], 'referer': os.environ['a4']}
|
13 |
+
|
14 |
+
|
15 |
+
def login(loginId, password):
|
16 |
+
data = {'loginId': loginId, 'password': password}
|
17 |
+
cookies = {'720yun_v8_session': os.environ['a3']}
|
18 |
+
|
19 |
+
resp = requests.post(os.environ['a5'], data=data, headers=HEADERS, cookies=cookies)
|
20 |
+
|
21 |
+
if resp.status_code == 200:
|
22 |
+
return resp.json()['data']['token']
|
23 |
+
raise gr.Error('Error')
|
24 |
+
|
25 |
+
|
26 |
+
def create_panorama(prompt, negative_prompt):
|
27 |
+
data = {'api_key': os.environ['a2'], 'generator': 'stable-skybox-trt', 'prompt': prompt, 'negative_text': negative_prompt}
|
28 |
+
resp = requests.post(os.environ['a1'], data=data)
|
29 |
+
|
30 |
+
if resp.status_code == 200:
|
31 |
+
request_id = resp.json()['request']['id']
|
32 |
+
flag = True
|
33 |
+
while flag:
|
34 |
+
resp = requests.get(f'{os.environ["a1"]}/{request_id}?api_key={os.environ["a2"]}')
|
35 |
+
if resp.status_code == 200:
|
36 |
+
progress_data = resp.json()['request']
|
37 |
+
if progress_data['progress'] == 100 and progress_data['status'] == 'complete':
|
38 |
+
flag = False
|
39 |
+
return request_id, progress_data['file_url']
|
40 |
+
time.sleep(1)
|
41 |
+
|
42 |
+
|
43 |
+
def upload(token, file_id, file_url):
|
44 |
+
now = int(time.time())
|
45 |
+
data = [{
|
46 |
+
'fileId': f'{file_id}-{now}',
|
47 |
+
'name': f'AI素材-{now}',
|
48 |
+
'watermarked': 0,
|
49 |
+
'size': 1024 * 1024 * 6,
|
50 |
+
'albumId': 0,
|
51 |
+
'exif': {},
|
52 |
+
'gps': {},
|
53 |
+
'panoId': 0,
|
54 |
+
'action': 1
|
55 |
+
}]
|
56 |
+
HEADERS['app-authorization'] = token
|
57 |
+
resp = requests.post(os.environ['a6'], data={'panos': json.dumps(data)}, headers=HEADERS)
|
58 |
+
if resp.status_code == 200:
|
59 |
+
resp_data = resp.json()['data'][0]
|
60 |
+
access_key_id = resp_data['accessKeyId']
|
61 |
+
security_token = resp_data['securityToken']
|
62 |
+
accessKey_secret = resp_data['accessKeySecret']
|
63 |
+
bucket_name = resp_data['bucketName']
|
64 |
+
path = resp_data['path'][1:]
|
65 |
+
endpoint = resp_data['endpointO']
|
66 |
+
pano_id = resp_data['panoId']
|
67 |
+
task_id = resp_data['taskId']
|
68 |
+
expired = resp_data['expired']
|
69 |
+
|
70 |
+
auth = oss2.StsAuth(access_key_id, accessKey_secret, security_token)
|
71 |
+
bucket = oss2.Bucket(auth, endpoint, bucket_name)
|
72 |
+
|
73 |
+
input_stream = requests.get(file_url)
|
74 |
+
result = bucket.put_object(f'{path}/{pano_id}.jpg', input_stream)
|
75 |
+
if result.status == 200:
|
76 |
+
resp = requests.post(f'{os.environ["a6"]}/{task_id}', data={'status': 3, 'expired': expired}, headers=HEADERS)
|
77 |
+
time.sleep(5)
|
78 |
+
if resp.status_code == 200:
|
79 |
+
flag = True
|
80 |
+
while flag:
|
81 |
+
pano_ids = [pano['id'] for pano in requests.get(os.environ["a7"], headers=HEADERS).json()['data']]
|
82 |
+
if pano_id not in pano_ids:
|
83 |
+
flag = False
|
84 |
+
break
|
85 |
+
time.sleep(1)
|
86 |
+
data = {
|
87 |
+
'name': f'AI全景作品-{now}',
|
88 |
+
'materials': json.dumps([{
|
89 |
+
'type': 1,
|
90 |
+
'id': pano_id
|
91 |
+
}]),
|
92 |
+
'templateId': 2,
|
93 |
+
'publishPlatform': 1,
|
94 |
+
'keywords': 'AI全景',
|
95 |
+
'source': 99
|
96 |
+
}
|
97 |
+
resp = requests.post(os.environ['a8'], data=data, headers=HEADERS)
|
98 |
+
if resp.status_code == 200:
|
99 |
+
return resp.json()['data']['tid']
|
100 |
+
|
101 |
+
|
102 |
+
def main(loginId, password, prompt, negative_prompt, state, progress=gr.Progress()):
|
103 |
+
if 'token' not in state:
|
104 |
+
state['token'] = login(loginId, password)
|
105 |
+
token = state['token']
|
106 |
+
|
107 |
+
file_id, image = create_panorama(prompt, negative_prompt)
|
108 |
+
panorama_id = upload(token, file_id, image)
|
109 |
+
return f'https://www.720yun.com/vr/{panorama_id}'
|
110 |
+
|
111 |
+
|
112 |
+
with gr.Blocks(css=CSS) as demo:
|
113 |
+
session = gr.State({})
|
114 |
|
115 |
+
gr.Markdown("""
|
116 |
+
# Generate your own panorama with AI
|
117 |
+
Your **[720yun.com](https://www.720yun.com)** account is required.
|
118 |
+
Support by **[720yun.com](https://www.720yun.com)**.
|
119 |
+
Prompt example: a beautiful matte painting of northernmost continent, a gigantic square fortress covered by blizzard, texture smooth, aerial view, epic composition, post apocalyptic, sharp focus, sci-fi, futuristic, fantasy, clean background trending, by Jan Urschel and Sergey Vasnev and Emmanuel Shiu and Liam Wong and Michal Karcz, ornate, cinematic, cinematic lighting, light effect, epic, airy vibrant theme, octane render, unreal engine, concise and clear, 4k hd wallpaper, trending on artstation and cgsociety
|
120 |
+
""")
|
121 |
+
with gr.Row():
|
122 |
+
with gr.Column():
|
123 |
+
login_id = gr.Textbox(label='LoginId', placeholder='720yun loginId')
|
124 |
+
with gr.Column():
|
125 |
+
password = gr.Textbox(label='Password', type='password', placeholder='720yun password')
|
126 |
+
with gr.Row():
|
127 |
+
prompt = gr.Textbox(label='Prompt', lines=5, placeholder='Enter your prompt')
|
128 |
+
with gr.Row():
|
129 |
+
negative_prompt = gr.Textbox(label='Negative prompt', lines=2, placeholder='Enter your negative prompt')
|
130 |
+
with gr.Row():
|
131 |
+
out = gr.Textbox(label='Panorama Url')
|
132 |
|
133 |
+
btn = gr.Button('Run')
|
134 |
+
btn.click(fn=main, inputs=[login_id, password, prompt, negative_prompt, session], outputs=out, show_progress=True)
|
135 |
|
136 |
+
demo.queue().launch()
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1 +1,3 @@
|
|
1 |
-
requests
|
|
|
|
|
|
1 |
+
requests
|
2 |
+
oss2
|
3 |
+
simplejson
|