banyapon commited on
Commit
eb33817
·
1 Parent(s): 2fca36c
Files changed (1) hide show
  1. app.py +59 -8
app.py CHANGED
@@ -1,10 +1,14 @@
1
- import spaces
2
  import gradio as gr
3
- from gradio_pannellum import Pannellum
 
4
  import torch
 
 
 
 
5
  from huggingface_hub import snapshot_download
6
  from txt2panoimg import Text2360PanoramaImagePipeline
7
- from PIL import Image
8
 
9
  # Download the model
10
  model_path = snapshot_download("archerfmy0831/sd-t2i-360panoimage")
@@ -16,7 +20,7 @@ txt2panoimg = Text2360PanoramaImagePipeline(model_path, torch_dtype=torch.float1
16
  def text_to_pano(prompt, upscale):
17
  input_data = {'prompt': prompt, 'upscale': upscale, 'refinement': False}
18
  output = txt2panoimg(input_data)
19
- return output, output
20
 
21
  title = """<h1 align="center">SD-T2I-360PanoImage</h1>
22
  <p align="center">360° Panorama Image Generation</p>
@@ -26,6 +30,38 @@ title = """<h1 align="center">SD-T2I-360PanoImage</h1>
26
  </center></p>
27
  """
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  with gr.Blocks(theme='bethecloud/storj_theme') as demo:
30
  gr.HTML(title)
31
  with gr.Row():
@@ -35,21 +71,36 @@ with gr.Blocks(theme='bethecloud/storj_theme') as demo:
35
  t2p_generate = gr.Button("Generate Panorama")
36
  with gr.Column(variant="default"):
37
  t2p_output = Pannellum(show_label=False, interactive=True)
38
-
39
  with gr.Row():
40
  t2p_image_output = gr.Image(label="Generated Image")
 
 
 
41
 
42
  # Add a hidden component to store a random value
43
  update_trigger = gr.State(value=0)
44
 
45
  def generate_with_update(prompt, upscale, trigger):
46
  output, image = text_to_pano(prompt, upscale)
47
- return output, image, trigger + 1
 
48
 
49
  t2p_generate.click(
50
  generate_with_update,
51
  inputs=[t2p_input, t2p_upscale, update_trigger],
52
- outputs=[t2p_output, t2p_image_output, update_trigger]
 
 
 
 
 
 
 
 
 
 
 
53
  )
54
 
55
- demo.launch()
 
 
1
  import gradio as gr
2
+ from PIL import Image
3
+ from diffusers import StableDiffusionPipeline
4
  import torch
5
+ import numpy as np
6
+ import base64
7
+ from gradio_pannellum import Pannellum
8
+ import io
9
  from huggingface_hub import snapshot_download
10
  from txt2panoimg import Text2360PanoramaImagePipeline
11
+ import spaces
12
 
13
  # Download the model
14
  model_path = snapshot_download("archerfmy0831/sd-t2i-360panoimage")
 
20
  def text_to_pano(prompt, upscale):
21
  input_data = {'prompt': prompt, 'upscale': upscale, 'refinement': False}
22
  output = txt2panoimg(input_data)
23
+ return output, output
24
 
25
  title = """<h1 align="center">SD-T2I-360PanoImage</h1>
26
  <p align="center">360° Panorama Image Generation</p>
 
30
  </center></p>
31
  """
32
 
33
+ def create_aframe_html(image):
34
+ buffered = io.BytesIO()
35
+ image.save(buffered, format="JPEG")
36
+ img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
37
+
38
+ aframe_html = f"""
39
+ <html>
40
+ <head>
41
+ <script src="https://aframe.io/releases/1.3.0/aframe.min.js"></script>
42
+ </head>
43
+ <body>
44
+ <a-scene>
45
+ <a-sky src="data:image/jpeg;base64,{img_str}"></a-sky>
46
+ </a-scene>
47
+ </body>
48
+ </html>
49
+ """
50
+ return aframe_html
51
+
52
+ def create_aframe_preview(image):
53
+ buffered = io.BytesIO()
54
+ image.save(buffered, format="JPEG")
55
+ img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
56
+
57
+ aframe_html = f"""
58
+ <a-scene embedded style="width: 100%; height: 400px;">
59
+ <a-sky src="data:image/jpeg;base64,{img_str}" rotation="0 -130 0"></a-sky>
60
+ <a-text font="kelsonsans" value="Create from AI" width="6" position="-2.5 0.25 -1.5" rotation="0 15 0"></a-text>
61
+ </a-scene>
62
+ """
63
+ return gr.HTML(f'<iframe srcdoc="{aframe_html}" allowfullscreen frameborder="0"></iframe>')
64
+
65
  with gr.Blocks(theme='bethecloud/storj_theme') as demo:
66
  gr.HTML(title)
67
  with gr.Row():
 
71
  t2p_generate = gr.Button("Generate Panorama")
72
  with gr.Column(variant="default"):
73
  t2p_output = Pannellum(show_label=False, interactive=True)
74
+
75
  with gr.Row():
76
  t2p_image_output = gr.Image(label="Generated Image")
77
+ output_html = gr.HTML(label="A-Frame HTML Code (Copy and use in Glitch)")
78
+ copy_html_button = gr.Button("Copy A-Frame HTML")
79
+ aframe_preview = gr.HTML(label="A-Frame Preview")
80
 
81
  # Add a hidden component to store a random value
82
  update_trigger = gr.State(value=0)
83
 
84
  def generate_with_update(prompt, upscale, trigger):
85
  output, image = text_to_pano(prompt, upscale)
86
+ html = create_aframe_html(image)
87
+ return output, image, html, create_aframe_preview(image), trigger + 1
88
 
89
  t2p_generate.click(
90
  generate_with_update,
91
  inputs=[t2p_input, t2p_upscale, update_trigger],
92
+ outputs=[t2p_output, t2p_image_output, output_html, aframe_preview, update_trigger]
93
+ )
94
+
95
+ output_image.change(create_aframe_preview, inputs=[output_image], outputs=[aframe_preview])
96
+
97
+
98
+ # Function to copy A-Frame HTML
99
+ copy_html_button.click(
100
+ None,
101
+ inputs=[output_html],
102
+ outputs=[],
103
+ _js="(html) => {navigator.clipboard.writeText(html).then(() => alert('Copied to clipboard!')).catch(err => console.error('Failed to copy: ', err));}"
104
  )
105
 
106
+ demo.launch()