banyapon commited on
Commit
6bb18db
·
1 Parent(s): 80816e5

checkpoint 360 aframe fixed image

Browse files
Files changed (1) hide show
  1. app.py +16 -59
app.py CHANGED
@@ -10,23 +10,24 @@ import io
10
  from huggingface_hub import snapshot_download
11
  from txt2panoimg import Text2360PanoramaImagePipeline
12
 
13
-
14
  # Download the model
15
  model_path = snapshot_download("archerfmy0831/sd-t2i-360panoimage")
16
 
17
  # Initialize pipelines
18
- txt2panoimg = Text2360PanoramaImagePipeline(model_path, torch_dtype=torch.float16)
19
 
 
20
  @spaces.GPU(duration=200)
21
  def text_to_pano(prompt, upscale):
22
  try:
23
  input_data = {'prompt': prompt, 'upscale': upscale, 'refinement': False}
24
- output = txt2panoimg(input_data)
25
  return output, output
26
  except Exception as e:
27
  print(f"Error generating panorama: {e}")
28
  return None, None
29
 
 
30
  title = """
31
  <div style="text-align: left;">
32
  <img src="https://ant.dpu.ac.th/wp-content/uploads/2024/04/dpulogo.png" width="200"/>
@@ -39,12 +40,11 @@ title = """
39
  </p>
40
  </div>
41
  """
42
-
43
  def create_aframe_html(image):
44
  buffered = io.BytesIO()
45
  image.save(buffered, format="JPEG")
46
  img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
47
-
48
  aframe_html = f"""
49
  <html>
50
  <head>
@@ -58,8 +58,7 @@ def create_aframe_html(image):
58
  </html>
59
  """
60
  return aframe_html
61
-
62
-
63
  def create_aframe_preview(image):
64
  if image is None:
65
  return gr.HTML("Error generating image. Please check the prompt or try again.")
@@ -79,34 +78,8 @@ def create_aframe_preview(image):
79
  """
80
  return gr.HTML(aframe_html)
81
 
82
- def get_image_url(image):
83
- if image is None:
84
- return None
85
- buffered = io.BytesIO()
86
- image.save(buffered, format="JPEG")
87
- img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
88
- return f"data:image/jpeg;base64,{img_str}"
89
-
90
- def get_aframe_code(image_url):
91
- if image_url is None:
92
- return "Error generating A-Frame code. Please generate an image first."
93
- aframe_html = f"""
94
- <html>
95
- <head>
96
- <script src="https://aframe.io/releases/1.3.0/aframe.min.js"></script>
97
- </head>
98
- <body>
99
- <a-scene>
100
- <a-sky src="{image_url}" rotation="0 -130 0"></a-sky>
101
- <a-text font="kelsonsans" value="Create from AI" width="6" position="-2.5 0.25 -1.5" rotation="0 15 0"></a-text>
102
- </a-scene>
103
- </body>
104
- </html>
105
- """
106
- return aframe_html
107
-
108
-
109
 
 
110
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
111
  gr.HTML(title)
112
  with gr.Row():
@@ -116,41 +89,25 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
116
  t2p_generate = gr.Button("Generate 360° Image")
117
  with gr.Column(variant="panel"):
118
  t2p_output = Pannellum(show_label=False, interactive=True)
119
-
120
  with gr.Row():
121
- with gr.Column():
122
- t2p_image_output = gr.Image(label="Generated Image")
123
- image_url_output = gr.Textbox(label="Image URL")
124
-
125
- # Modified download button:
126
- download_button = gr.Button("Download Image")
127
- download_button.click(
128
- lambda img: gr.File.update(value=img, visible=True), inputs=[t2p_image_output], outputs=[gr.File(label="Image", visible=False)]
129
- )
130
- with gr.Column(variant="panel"):
131
- with gr.Column():
132
- output_html = gr.Textbox(label="A-Frame HTML Code (Copy and use in Glitch)", lines=15)
133
- gr.Markdown("Copy the HTML code below and paste it into a new Glitch project to view your 360° panorama in VR.")
134
-
135
- with gr.Column(variant="panel"): # Add wrapper for A-Frame to avoid split screen
136
- aframe_preview = gr.HTML(label="A-Frame Preview")
137
 
138
- update_trigger = gr.State(value=0)
139
 
140
  def generate_with_update(prompt, upscale, trigger):
141
  output, image = text_to_pano(prompt, upscale)
142
- image_url = get_image_url(image)
143
- html = create_aframe_html(image) if image is not None else "Failed to generate A-Frame HTML. Please try again."
144
  preview = create_aframe_preview(image)
145
- return output, image, html, preview, image_url, trigger + 1
146
 
147
  t2p_generate.click(
148
  generate_with_update,
149
  inputs=[t2p_input, t2p_upscale, update_trigger],
150
- outputs=[t2p_output, t2p_image_output, output_html, aframe_preview, image_url_output, update_trigger]
151
  )
152
- t2p_image_output.change(fn=get_image_url, inputs=[t2p_image_output], outputs=[image_url_output])
153
 
154
- image_url_output.change(fn=get_aframe_code, inputs=[image_url_output], outputs=[output_html])
155
 
156
- demo.launch()
 
10
  from huggingface_hub import snapshot_download
11
  from txt2panoimg import Text2360PanoramaImagePipeline
12
 
 
13
  # Download the model
14
  model_path = snapshot_download("archerfmy0831/sd-t2i-360panoimage")
15
 
16
  # Initialize pipelines
17
+ pipe = Text2360PanoramaImagePipeline.from_pretrained(model_path, torch_dtype=torch.float16)
18
 
19
+ # Function to generate panorama
20
  @spaces.GPU(duration=200)
21
  def text_to_pano(prompt, upscale):
22
  try:
23
  input_data = {'prompt': prompt, 'upscale': upscale, 'refinement': False}
24
+ output = pipe(input_data)
25
  return output, output
26
  except Exception as e:
27
  print(f"Error generating panorama: {e}")
28
  return None, None
29
 
30
+ # Define the page title and description
31
  title = """
32
  <div style="text-align: left;">
33
  <img src="https://ant.dpu.ac.th/wp-content/uploads/2024/04/dpulogo.png" width="200"/>
 
40
  </p>
41
  </div>
42
  """
43
+ # Function to create A-Frame HTML
44
  def create_aframe_html(image):
45
  buffered = io.BytesIO()
46
  image.save(buffered, format="JPEG")
47
  img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
 
48
  aframe_html = f"""
49
  <html>
50
  <head>
 
58
  </html>
59
  """
60
  return aframe_html
61
+ # Function to create A-Frame Preview
 
62
  def create_aframe_preview(image):
63
  if image is None:
64
  return gr.HTML("Error generating image. Please check the prompt or try again.")
 
78
  """
79
  return gr.HTML(aframe_html)
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
+ # Create the Gradio interface
83
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
84
  gr.HTML(title)
85
  with gr.Row():
 
89
  t2p_generate = gr.Button("Generate 360° Image")
90
  with gr.Column(variant="panel"):
91
  t2p_output = Pannellum(show_label=False, interactive=True)
92
+
93
  with gr.Row():
94
+ t2p_image_output = gr.Image(label="Generated Image")
95
+ with gr.Column(variant="panel"): # Add wrapper for A-Frame to avoid split screen
96
+ aframe_preview = gr.HTML(label="A-Frame Preview")
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
+ update_trigger = gr.State(value=0)
99
 
100
  def generate_with_update(prompt, upscale, trigger):
101
  output, image = text_to_pano(prompt, upscale)
 
 
102
  preview = create_aframe_preview(image)
103
+ return output, image, preview, trigger + 1
104
 
105
  t2p_generate.click(
106
  generate_with_update,
107
  inputs=[t2p_input, t2p_upscale, update_trigger],
108
+ outputs=[t2p_output, t2p_image_output, aframe_preview, update_trigger]
109
  )
110
+ t2p_image_output.change(create_aframe_preview, inputs=[t2p_image_output], outputs=[aframe_preview])
111
 
 
112
 
113
+ demo.launch()