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

checkpoint 360 app

Browse files
Files changed (1) hide show
  1. app.py +39 -31
app.py CHANGED
@@ -10,24 +10,26 @@ import io
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,48 +42,41 @@ title = """
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>
51
  <script src="https://aframe.io/releases/1.3.0/aframe.min.js"></script>
52
  </head>
53
  <body>
54
  <a-scene>
55
- <a-sky src="data:image/jpeg;base64,{img_str}"></a-sky>
56
  </a-scene>
57
  </body>
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.")
65
 
66
- buffered = io.BytesIO()
67
- image.save(buffered, format="JPEG")
68
- img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
69
-
70
- aframe_html = f"""
71
  <a-scene embedded style="width: 100%; height: 400px;">
72
- <a-assets>
73
- <img id="pano-img" src="data:image/jpeg;base64,{img_str}">
74
- </a-assets>
75
- <a-sky src="#pano-img" rotation="0 -130 0"></a-sky>
76
  <a-text font="kelsonsans" value="Create from AI" width="6" position="-2.5 0.25 -1.5" rotation="0 15 0"></a-text>
77
  </a-scene>
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():
86
  with gr.Column():
87
  t2p_input = gr.Textbox(label="Enter your prompt", lines=3)
@@ -90,24 +85,37 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
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()
 
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
  pipe = Text2360PanoramaImagePipeline.from_pretrained(model_path, torch_dtype=torch.float16)
19
+ pipe = pipe.to("cuda")
20
+
21
 
22
+ @spaces.GPU(duration=200) # Assuming you have a GPU available
 
23
  def text_to_pano(prompt, upscale):
24
  try:
25
  input_data = {'prompt': prompt, 'upscale': upscale, 'refinement': False}
26
+ image = pipe(input_data).images[0]
27
+ return image
28
  except Exception as e:
29
  print(f"Error generating panorama: {e}")
30
+ return None
31
+
32
 
 
33
  title = """
34
  <div style="text-align: left;">
35
  <img src="https://ant.dpu.ac.th/wp-content/uploads/2024/04/dpulogo.png" width="200"/>
 
42
  </p>
43
  </div>
44
  """
45
+
46
+
47
  def create_aframe_html(image):
48
  buffered = io.BytesIO()
49
  image.save(buffered, format="JPEG")
50
  img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
51
+ return f"""
52
  <html>
53
  <head>
54
  <script src="https://aframe.io/releases/1.3.0/aframe.min.js"></script>
55
  </head>
56
  <body>
57
  <a-scene>
58
+ <a-sky src="{img_str}"></a-sky>
59
  </a-scene>
60
  </body>
61
  </html>
62
  """
63
+
64
+
65
  def create_aframe_preview(image):
66
  if image is None:
67
  return gr.HTML("Error generating image. Please check the prompt or try again.")
68
 
69
+ return gr.HTML(f"""
 
 
 
 
70
  <a-scene embedded style="width: 100%; height: 400px;">
71
+ <a-sky src="{image}" rotation="0 -130 0"></a-sky>
 
 
 
72
  <a-text font="kelsonsans" value="Create from AI" width="6" position="-2.5 0.25 -1.5" rotation="0 15 0"></a-text>
73
  </a-scene>
74
+ """)
 
75
 
76
 
 
77
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
78
  gr.HTML(title)
79
+
80
  with gr.Row():
81
  with gr.Column():
82
  t2p_input = gr.Textbox(label="Enter your prompt", lines=3)
 
85
  with gr.Column(variant="panel"):
86
  t2p_output = Pannellum(show_label=False, interactive=True)
87
 
88
+
89
  with gr.Row():
90
+ with gr.Column():
91
+ t2p_image_output = gr.Image(label="Generated Image", type="pil")
92
+ image_url_output = gr.Textbox(label="Image URL")
93
+ gr.Button("Download Image").click(
94
+ None, inputs=[t2p_image_output], outputs=[t2p_image_output], _js="(img) => {window.open(img,'_blank');}"
95
+ )
96
+ with gr.Column(variant="panel"):
97
+ output_html = gr.Textbox(label="A-Frame HTML Code (Copy and use in Glitch)", lines=15)
98
+ gr.Markdown("Copy the HTML code below and paste it into a new Glitch project to view your 360° panorama in VR.")
99
+
100
+ with gr.Column():
101
+ aframe_preview = gr.HTML(label="A-Frame Preview")
102
 
103
+ update_trigger = gr.State(value=0)
104
 
105
  def generate_with_update(prompt, upscale, trigger):
106
+ image = text_to_pano(prompt, upscale)
107
+ image_url = get_image_url(image)
108
+ html = create_aframe_html(image) if image is not None else "Failed to generate A-Frame HTML. Please try again."
109
  preview = create_aframe_preview(image)
110
+ return output, image, html, preview, image_url, trigger + 1
111
 
112
  t2p_generate.click(
113
  generate_with_update,
114
  inputs=[t2p_input, t2p_upscale, update_trigger],
115
+ outputs=[t2p_output, t2p_image_output, output_html, aframe_preview, image_url_output, update_trigger]
116
  )
117
+ t2p_image_output.change(fn=get_image_url, inputs=[t2p_image_output], outputs=[image_url_output])
118
 
119
+ image_url_output.change(fn=get_aframe_code, inputs=[image_url_output], outputs=[output_html])
120
 
121
  demo.launch()