Lorden Games
commited on
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from diffusers import ShapEPipeline
|
4 |
+
from diffusers.utils import export_to_gif
|
5 |
+
import os
|
6 |
+
from huggingface_hub import HfApi
|
7 |
+
import json
|
8 |
+
|
9 |
+
# GPU kontrolü
|
10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
+
print(f"Kullanılan cihaz: {device}")
|
12 |
+
|
13 |
+
def validate_token(token):
|
14 |
+
try:
|
15 |
+
api = HfApi(token=token)
|
16 |
+
api.whoami()
|
17 |
+
return True
|
18 |
+
except Exception:
|
19 |
+
return False
|
20 |
+
|
21 |
+
def generate_3d_model(prompt, token, guidance_scale=15.0, num_steps=64, export_format="obj"):
|
22 |
+
try:
|
23 |
+
if not validate_token(token):
|
24 |
+
return "Geçersiz Hugging Face token'ı", None, None
|
25 |
+
|
26 |
+
print(f"Üretim başlıyor: {prompt}")
|
27 |
+
|
28 |
+
# Model yükleme (token ile)
|
29 |
+
pipe = ShapEPipeline.from_pretrained("openai/shap-e", use_auth_token=token).to(device)
|
30 |
+
|
31 |
+
# Çıktı klasörü kontrolü
|
32 |
+
os.makedirs("outputs", exist_ok=True)
|
33 |
+
|
34 |
+
# Dosya adı oluşturma
|
35 |
+
safe_prompt = "".join(x for x in prompt if x.isalnum() or x in (" ", "-", "_"))
|
36 |
+
base_filename = f"outputs/{safe_prompt}"
|
37 |
+
|
38 |
+
# 3D model oluşturma
|
39 |
+
images = pipe(
|
40 |
+
prompt,
|
41 |
+
guidance_scale=guidance_scale,
|
42 |
+
num_inference_steps=num_steps,
|
43 |
+
size=256,
|
44 |
+
).images
|
45 |
+
|
46 |
+
# GIF kaydetme
|
47 |
+
gif_path = export_to_gif(images, f"{base_filename}.gif")
|
48 |
+
|
49 |
+
# 3D mesh oluşturma
|
50 |
+
mesh = pipe(
|
51 |
+
prompt,
|
52 |
+
guidance_scale=guidance_scale,
|
53 |
+
num_inference_steps=num_steps,
|
54 |
+
output_type="mesh",
|
55 |
+
).meshes[0]
|
56 |
+
|
57 |
+
# Mesh'i kaydetme
|
58 |
+
output_path = f"{base_filename}.{export_format}"
|
59 |
+
mesh.export(output_path)
|
60 |
+
|
61 |
+
print(f"Üretim tamamlandı: {output_path}")
|
62 |
+
return "Üretim başarılı!", gif_path, output_path
|
63 |
+
|
64 |
+
except Exception as e:
|
65 |
+
print(f"Hata oluştu: {str(e)}")
|
66 |
+
return str(e), None, None
|
67 |
+
|
68 |
+
# Gradio arayüzü
|
69 |
+
interface = gr.Interface(
|
70 |
+
fn=generate_3d_model,
|
71 |
+
inputs=[
|
72 |
+
gr.Textbox(label="3D Model için açıklama giriniz (İngilizce)"),
|
73 |
+
gr.Textbox(label="Hugging Face Token", type="password"),
|
74 |
+
gr.Slider(minimum=1, maximum=20, value=15, label="Guidance Scale"),
|
75 |
+
gr.Slider(minimum=32, maximum=128, value=64, label="Adım Sayısı"),
|
76 |
+
gr.Radio(["obj", "glb"], label="Export Formatı", value="obj")
|
77 |
+
],
|
78 |
+
outputs=[
|
79 |
+
gr.Textbox(label="Durum"),
|
80 |
+
gr.Image(label="3D Önizleme (GIF)"),
|
81 |
+
gr.File(label="3D Model Dosyası")
|
82 |
+
],
|
83 |
+
title="DNM3D - 3D Model Üretici",
|
84 |
+
description="Metin girişiyle 3D model oluşturun. Kullanmak için Hugging Face token'ınızı girmeniz gerekiyor."
|
85 |
+
)
|
86 |
+
|
87 |
+
if __name__ == "__main__":
|
88 |
+
interface.launch()
|