Spaces:
Running
Running
Create app-backup.py
Browse files- app-backup.py +124 -0
app-backup.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import shlex
|
| 2 |
+
import subprocess
|
| 3 |
+
|
| 4 |
+
subprocess.run(shlex.split("pip install pip==24.0"), check=True)
|
| 5 |
+
subprocess.run(
|
| 6 |
+
shlex.split(
|
| 7 |
+
"pip install package/onnxruntime_gpu-1.17.0-cp310-cp310-manylinux_2_28_x86_64.whl --force-reinstall --no-deps"
|
| 8 |
+
), check=True
|
| 9 |
+
)
|
| 10 |
+
subprocess.run(
|
| 11 |
+
shlex.split(
|
| 12 |
+
"pip install package/nvdiffrast-0.3.1.torch-cp310-cp310-linux_x86_64.whl --force-reinstall --no-deps"
|
| 13 |
+
), check=True
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# 모델 체크포인트 다운로드 및 torch 설정
|
| 17 |
+
if __name__ == "__main__":
|
| 18 |
+
from huggingface_hub import snapshot_download
|
| 19 |
+
|
| 20 |
+
snapshot_download("public-data/Unique3D", repo_type="model", local_dir="./ckpt")
|
| 21 |
+
|
| 22 |
+
import os
|
| 23 |
+
import sys
|
| 24 |
+
sys.path.append(os.curdir)
|
| 25 |
+
import torch
|
| 26 |
+
torch.set_float32_matmul_precision('medium')
|
| 27 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
| 28 |
+
torch.set_grad_enabled(False)
|
| 29 |
+
|
| 30 |
+
import fire
|
| 31 |
+
import gradio as gr
|
| 32 |
+
from gradio_app.gradio_3dgen import create_ui as create_3d_ui
|
| 33 |
+
from gradio_app.all_models import model_zoo
|
| 34 |
+
|
| 35 |
+
# ===============================
|
| 36 |
+
# Text-to-IMAGE 관련 API 함수 정의
|
| 37 |
+
# ===============================
|
| 38 |
+
def text_to_image(height, width, steps, scales, prompt, seed):
|
| 39 |
+
"""
|
| 40 |
+
주어진 파라미터를 이용해 외부 API (http://211.233.58.201:7971/)의
|
| 41 |
+
/process_and_save_image 엔드포인트를 호출하여 이미지를 생성한다.
|
| 42 |
+
"""
|
| 43 |
+
from gradio_client import Client
|
| 44 |
+
client = Client("http://211.233.58.201:7971/")
|
| 45 |
+
result = client.predict(
|
| 46 |
+
height,
|
| 47 |
+
width,
|
| 48 |
+
steps,
|
| 49 |
+
scales,
|
| 50 |
+
prompt,
|
| 51 |
+
seed,
|
| 52 |
+
api_name="/process_and_save_image"
|
| 53 |
+
)
|
| 54 |
+
# 결과가 dict이면 "url" 키의 값을, 아니라면 그대로 반환합니다.
|
| 55 |
+
if isinstance(result, dict):
|
| 56 |
+
return result.get("url", None)
|
| 57 |
+
else:
|
| 58 |
+
return result
|
| 59 |
+
|
| 60 |
+
def update_random_seed():
|
| 61 |
+
"""
|
| 62 |
+
외부 API의 /update_random_seed 엔드포인트를 호출하여
|
| 63 |
+
새로운 랜덤 시드 값을 가져온다.
|
| 64 |
+
"""
|
| 65 |
+
from gradio_client import Client
|
| 66 |
+
client = Client("http://211.233.58.201:7971/")
|
| 67 |
+
return client.predict(api_name="/update_random_seed")
|
| 68 |
+
|
| 69 |
+
# ===============================
|
| 70 |
+
# UI 타이틀 및 설명
|
| 71 |
+
# ===============================
|
| 72 |
+
_TITLE = '''3D Llama'''
|
| 73 |
+
_DESCRIPTION = '''
|
| 74 |
+
Text와 이미지를 이용하여 3D 모델을 생성할 수 있습니다.
|
| 75 |
+
|
| 76 |
+
'''
|
| 77 |
+
|
| 78 |
+
def launch():
|
| 79 |
+
# 3D 모델 초기화
|
| 80 |
+
model_zoo.init_models()
|
| 81 |
+
|
| 82 |
+
# Gradio Blocks 생성 (두 탭 포함)
|
| 83 |
+
with gr.Blocks(title=_TITLE) as demo:
|
| 84 |
+
with gr.Row():
|
| 85 |
+
gr.Markdown('# ' + _TITLE)
|
| 86 |
+
gr.Markdown(_DESCRIPTION)
|
| 87 |
+
|
| 88 |
+
# 탭 생성: 기존의 Text-to-3D와 새로 추가한 Text-to-IMAGE
|
| 89 |
+
with gr.Tabs():
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
with gr.Tab("Text to 3D Style IMAGE"):
|
| 93 |
+
# 이미지 생성을 위한 파라미터 입력 컴포넌트 구성
|
| 94 |
+
with gr.Row():
|
| 95 |
+
height_slider = gr.Slider(label="Height", minimum=256, maximum=2048, step=1, value=1024)
|
| 96 |
+
width_slider = gr.Slider(label="Width", minimum=256, maximum=2048, step=1, value=1024)
|
| 97 |
+
with gr.Row():
|
| 98 |
+
steps_slider = gr.Slider(label="Inference Steps", minimum=1, maximum=100, step=1, value=8)
|
| 99 |
+
scales_slider = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=3.5)
|
| 100 |
+
prompt_text = gr.Textbox(label="Image Description", placeholder="Enter prompt here", lines=2)
|
| 101 |
+
seed_number = gr.Number(label="Seed (optional, leave empty for random)", value=None)
|
| 102 |
+
|
| 103 |
+
# 'Update Random Seed' 버튼을 누르면 API를 통해 새로운 시드값을 받아 입력란 업데이트
|
| 104 |
+
update_seed_button = gr.Button("Update Random Seed")
|
| 105 |
+
update_seed_button.click(fn=update_random_seed, inputs=[], outputs=seed_number)
|
| 106 |
+
|
| 107 |
+
generate_button = gr.Button("Generate Image")
|
| 108 |
+
image_output = gr.Image(label="Generated Image")
|
| 109 |
+
|
| 110 |
+
# 'Generate Image' 버튼 클릭 시 text_to_image 함수를 호출하여 결과 이미지를 출력
|
| 111 |
+
generate_button.click(
|
| 112 |
+
fn=text_to_image,
|
| 113 |
+
inputs=[height_slider, width_slider, steps_slider, scales_slider, prompt_text, seed_number],
|
| 114 |
+
outputs=image_output
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
with gr.Tab("Image to 3D"):
|
| 118 |
+
create_3d_ui("wkl")
|
| 119 |
+
|
| 120 |
+
# 공유 링크를 생성하기 위해 share=True 설정
|
| 121 |
+
demo.queue().launch(share=True)
|
| 122 |
+
|
| 123 |
+
if __name__ == '__main__':
|
| 124 |
+
fire.Fire(launch)
|