Pana360gen / app.py
banyapon's picture
checkpoint Aframe Block
9946212
import spaces
import gradio as gr
from PIL import Image
from diffusers import StableDiffusionPipeline
import torch
import numpy as np
import base64
from gradio_pannellum import Pannellum
import io
from huggingface_hub import snapshot_download
from txt2panoimg import Text2360PanoramaImagePipeline
# Download the model
model_path = snapshot_download("archerfmy0831/sd-t2i-360panoimage")
# Initialize pipelines
txt2panoimg = Text2360PanoramaImagePipeline(model_path, torch_dtype=torch.float16)
@spaces.GPU(duration=200)
def text_to_pano(prompt, upscale):
try:
input_data = {'prompt': prompt, 'upscale': upscale, 'refinement': False}
output = txt2panoimg(input_data)
return output, output
except Exception as e:
print(f"Error generating panorama: {e}")
return None, None
title = """
<div style="text-align: left;">
<img src="https://ant.dpu.ac.th/wp-content/uploads/2024/04/dpulogo.png" width="200"/>
<h4></h4>
<p>This Space is a playground designed to experiment with generating 360-degree images for use in Virtual Reality (VR) experiences. It utilizes A-Frame to render the images as WebXR using a Text-to-VR approach. This demonstration was presented at the MHESI Fair 2024 in Thailand. <br/> It is conducted by the College of Creative Design and Entertainment Technology, Dhurakij Pundit University, in the lab of Asst. Prof. Banyapon Poolsawas under the MIT License.</p>
<p>SD-T2I-360PanoImage Generate 360° Panorama Image Generation</p>
<p>
<a href="https://github.com/ArcherFMY/SD-T2I-360PanoImage/" target="_blank">[Github]</a>
<a href="https://huggingface.co/archerfmy0831/sd-t2i-360panoimage" target="_blank">[Models]</a>
</p>
</div>
"""
def create_aframe_html(image):
buffered = io.BytesIO()
image.save(buffered, format="JPEG")
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
return f"data:image/jpeg;base64,{img_str}"
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.HTML(title)
with gr.Row():
with gr.Column():
t2p_input = gr.Textbox(label="Enter your prompt", lines=3)
t2p_upscale = gr.Checkbox(label="Upscale (takes about 60 seconds 6144x3072 resolution)")
t2p_generate = gr.Button("Generate 360° Image")
with gr.Column(variant="panel"):
t2p_output = Pannellum(show_label=False, interactive=True)
with gr.Row():
t2p_image_output = gr.Image(label="Generated 360°Image")
# New A-Frame Code Block
with gr.Row():
output_html = gr.Textbox(label="A-Frame HTML Code (Copy and use in Glitch)", lines=15, interactive=False)
gr.Markdown("""
**Copy this code for Glitch.com:**
1. Create a new Glitch project ([https://glitch.com/](https://glitch.com/)).
2. Choose the **hello-webpage** template.
3. Paste this HTML code into the `index.html` file.
""")
update_trigger = gr.State(value=0)
def generate_with_update(prompt, upscale, trigger):
output, image = text_to_pano(prompt, upscale)
image_url = create_aframe_html(image)
html = f"""
<html>
<head>
<script src="https://aframe.io/releases/1.3.0/aframe.min.js"></script>
</head>
<body>
<a-scene>
<a-sky src="{image_url}" rotation="0 -130 0"></a-sky>
<a-text font="kelsonsans" value="Generated from AI" width="8" position="-2.5 0.25 -1.5" rotation="0 15 0"></a-text>
</a-scene>
</body>
</html>
""" if image_url is not None else "Failed to generate A-Frame HTML. Please try again."
return output, image, html, trigger + 1
t2p_generate.click(
generate_with_update,
inputs=[t2p_input, t2p_upscale, update_trigger],
outputs=[t2p_output, t2p_image_output, output_html, update_trigger]
)
demo.launch()