File size: 1,477 Bytes
cf95ea9
2ead0a2
 
a7afad0
cf95ea9
2ead0a2
 
cf95ea9
2ead0a2
 
 
 
 
a7afad0
 
2ead0a2
a7afad0
 
 
 
 
 
 
 
 
 
2ead0a2
a7afad0
cf95ea9
2ead0a2
 
cf95ea9
2ead0a2
 
 
cf95ea9
2ead0a2
cf95ea9
 
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
import gradio as gr
import requests
import os
import zipfile

API_URL = "https://api-inference.huggingface.co/models/stabilityai/TripoSR"
HEADERS = {"Authorization": f"Bearer {os.environ.get('HF_TOKEN')}"}

def generate_3d_from_image(image):
    with open(image, "rb") as f:
        response = requests.post(API_URL, headers=HEADERS, files={"file": f})

    if response.status_code == 200:
        output_zip = "model_output.zip"
        with open(output_zip, "wb") as out_file:
            out_file.write(response.content)

        # Intentar extraer un archivo .glb o .ply desde el zip
        with zipfile.ZipFile(output_zip, 'r') as zip_ref:
            zip_ref.extractall("output_model")

        for filename in zip_ref.namelist():
            if filename.endswith(".glb") or filename.endswith(".ply"):
                return os.path.join("output_model", filename)

        raise gr.Error("El modelo no generó un archivo .glb o .ply compatible.")
    else:
        raise gr.Error(f"Error {response.status_code}: {response.text}")

with gr.Blocks() as demo:
    gr.Markdown("## 🧠 Genera un modelo 3D real desde una imagen usando TripoSR")
    with gr.Row():
        image_input = gr.Image(type="filepath", label="Sube una imagen (.png o .jpg)")
    generate_btn = gr.Button("Generar modelo 3D")
    output_model = gr.Model3D(label="Vista previa del modelo 3D")

    generate_btn.click(fn=generate_3d_from_image, inputs=image_input, outputs=output_model)

demo.launch()