Spaces:
Build error
Build error
File size: 3,852 Bytes
e37fa99 2f19003 eb33817 a3d993c eb33817 a3d993c e37fa99 b47d74a fdd73d7 98076c6 b47d74a 98076c6 fdd73d7 98076c6 fdd73d7 98076c6 b47d74a fdd73d7 918e1e1 fdd73d7 918e1e1 fdd73d7 b47d74a 98076c6 b47d74a 9946212 f26108a 2fca36c 9946212 2f19003 9946212 2fca36c df647e6 9946212 df647e6 9946212 2f19003 98076c6 9946212 2f19003 9946212 eb33817 9946212 2f19003 6bb18db |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
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()
|