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 = """
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.
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.
SD-T2I-360PanoImage Generate 360° Panorama Image Generation
[Github]
[Models]
"""
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"""
""" 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()