Lorden Games commited on
Commit
9092255
·
verified ·
1 Parent(s): faef151

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, login
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
+ login(token=token)
16
+ return True
17
+ except Exception as e:
18
+ print(f"Token doğrulama hatası: {str(e)}")
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(
30
+ "openai/shap-e",
31
+ torch_dtype=torch.float32 if device == "cuda" else torch.float32,
32
+ token=token,
33
+ cache_dir="model_cache"
34
+ ).to(device)
35
+
36
+ # Çıktı klasörü kontrolü
37
+ os.makedirs("outputs", exist_ok=True)
38
+
39
+ # Dosya adı oluşturma
40
+ safe_prompt = "".join(x for x in prompt if x.isalnum() or x in (" ", "-", "_"))
41
+ base_filename = f"outputs/{safe_prompt}"
42
+
43
+ # 3D model oluşturma
44
+ images = pipe(
45
+ prompt,
46
+ guidance_scale=guidance_scale,
47
+ num_inference_steps=num_steps,
48
+ size=256,
49
+ ).images
50
+
51
+ # GIF kaydetme
52
+ gif_path = export_to_gif(images, f"{base_filename}.gif")
53
+
54
+ # 3D mesh oluşturma
55
+ mesh = pipe(
56
+ prompt,
57
+ guidance_scale=guidance_scale,
58
+ num_inference_steps=num_steps,
59
+ output_type="mesh",
60
+ ).meshes[0]
61
+
62
+ # Mesh'i kaydetme
63
+ output_path = f"{base_filename}.{export_format}"
64
+ mesh.export(output_path)
65
+
66
+ print(f"Üretim tamamlandı: {output_path}")
67
+ return "Üretim başarılı!", gif_path, output_path
68
+
69
+ except Exception as e:
70
+ print(f"Hata oluştu: {str(e)}")
71
+ return str(e), None, None
72
+
73
+ # Gradio arayüzü
74
+ interface = gr.Interface(
75
+ fn=generate_3d_model,
76
+ inputs=[
77
+ gr.Textbox(label="3D Model için açıklama giriniz (İngilizce)"),
78
+ gr.Textbox(label="Hugging Face Token", type="password"),
79
+ gr.Slider(minimum=1, maximum=20, value=15, label="Guidance Scale"),
80
+ gr.Slider(minimum=32, maximum=128, value=64, label="Adım Sayısı"),
81
+ gr.Radio(["obj", "glb"], label="Export Formatı", value="obj")
82
+ ],
83
+ outputs=[
84
+ gr.Textbox(label="Durum"),
85
+ gr.Image(label="3D Önizleme (GIF)"),
86
+ gr.File(label="3D Model Dosyası")
87
+ ],
88
+ title="DNM3D - 3D Model Üretici",
89
+ description="Metin girişiyle 3D model oluşturun. Kullanmak için Hugging Face token'ınızı girmeniz gerekiyor."
90
+ )
91
+
92
+ if __name__ == "__main__":
93
+ interface.launch()