geyik1 commited on
Commit
cea284c
·
verified ·
1 Parent(s): 9b02c82

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ try:
44
+ # 3D model oluşturma
45
+ output = pipe(
46
+ prompt_embeds=None,
47
+ text=prompt,
48
+ guidance_scale=guidance_scale,
49
+ num_inference_steps=num_steps
50
+ )
51
+
52
+ images = output.images
53
+
54
+ # GIF kaydetme
55
+ gif_path = export_to_gif(images, f"{base_filename}.gif")
56
+
57
+ # 3D mesh oluşturma
58
+ mesh_output = pipe(
59
+ prompt_embeds=None,
60
+ text=prompt,
61
+ guidance_scale=guidance_scale,
62
+ num_inference_steps=num_steps,
63
+ output_type="mesh"
64
+ )
65
+
66
+ mesh = mesh_output.meshes[0]
67
+
68
+ # Mesh'i kaydetme
69
+ output_path = f"{base_filename}.{export_format}"
70
+ mesh.export(output_path)
71
+
72
+ print(f"Üretim tamamlandı: {output_path}")
73
+ return "Üretim başarılı!", gif_path, output_path
74
+
75
+ except Exception as model_error:
76
+ error_msg = f"Model çalıştırma hatası: {str(model_error)}"
77
+ print(error_msg)
78
+ return error_msg, None, None
79
+
80
+ except Exception as e:
81
+ error_msg = f"Genel hata: {str(e)}"
82
+ print(error_msg)
83
+ return error_msg, None, None
84
+
85
+ # Gradio arayüzü
86
+ interface = gr.Interface(
87
+ fn=generate_3d_model,
88
+ inputs=[
89
+ gr.Textbox(label="3D Model için açıklama giriniz (İngilizce)"),
90
+ gr.Textbox(label="Hugging Face Token", type="password"),
91
+ gr.Slider(minimum=1, maximum=20, value=15, label="Guidance Scale"),
92
+ gr.Slider(minimum=32, maximum=128, value=64, label="Adım Sayısı"),
93
+ gr.Radio(["obj", "glb"], label="Export Formatı", value="obj")
94
+ ],
95
+ outputs=[
96
+ gr.Textbox(label="Durum"),
97
+ gr.Image(label="3D Önizleme (GIF)"),
98
+ gr.File(label="3D Model Dosyası")
99
+ ],
100
+ title="DNM3D - 3D Model Üretici",
101
+ description="Metin girişiyle 3D model oluşturun. Kullanmak için Hugging Face token'ınızı girmeniz gerekiyor."
102
+ )
103
+
104
+ if __name__ == "__main__":
105
+ interface.launch()