geyik1 commited on
Commit
436008f
·
verified ·
1 Parent(s): 7ac60d9

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.log +6 -0
  2. app.py +127 -0
  3. requirements.txt +11 -0
app.log ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ Using device: cpu
2
+ Gradio version: 5.14.0
3
+ Python version: 3.12.8 | packaged by Anaconda, Inc. | (main, Dec 11 2024, 10:37:40) [Clang 14.0.6 ]
4
+ HUGGINGFACE_TOKEN not found in environment variables
5
+ Loading Stable Diffusion model for 3D-style rendering...
6
+
app.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import os
4
+ import sys
5
+ from huggingface_hub import login
6
+ from diffusers import StableDiffusionPipeline
7
+ import io
8
+ import base64
9
+ from PIL import Image
10
+ import numpy as np
11
+
12
+ # Force CPU usage if needed
13
+ device = "cuda" if torch.cuda.is_available() else "cpu"
14
+ print(f"Using device: {device}")
15
+
16
+ # More details about the environment
17
+ print(f"Gradio version: {gr.__version__}")
18
+ print(f"Python version: {sys.version}")
19
+
20
+ # Hugging Face API token'ı - önce environment variable olarak ara,
21
+ # sonra Hugging Face Secrets sisteminde ara
22
+ hf_token = os.environ.get("HUGGINGFACE_TOKEN")
23
+ if hf_token:
24
+ print("Found HUGGINGFACE_TOKEN in environment variables")
25
+ # Token ile giriş yap
26
+ login(token=hf_token)
27
+ print("Logged in with Hugging Face token")
28
+ else:
29
+ print("HUGGINGFACE_TOKEN not found in environment variables")
30
+
31
+ # Global model variable
32
+ pipe = None
33
+
34
+ def load_model():
35
+ global pipe
36
+ try:
37
+ print("Loading Stable Diffusion model for 3D-style rendering...")
38
+
39
+ # Use a lightweight model that can generate game icons
40
+ pipe = StableDiffusionPipeline.from_pretrained(
41
+ "runwayml/stable-diffusion-v1-5",
42
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32,
43
+ safety_checker=None,
44
+ requires_safety_checker=False
45
+ ).to(device)
46
+
47
+ print("Model loaded successfully!")
48
+ return True
49
+
50
+ except Exception as e:
51
+ print(f"Error loading model: {str(e)}")
52
+ # Fallback to a simple image generation
53
+ return False
54
+
55
+ def generate_3d_icon(prompt, seed=0, guidance_scale=7.5, num_inference_steps=20):
56
+ try:
57
+ print(f"Generating 3D icon with prompt: {prompt}")
58
+
59
+ if pipe is None:
60
+ raise Exception("Model not loaded")
61
+
62
+ # Enhance prompt for 3D game icon style
63
+ enhanced_prompt = f"3D rendered game icon, {prompt}, clean background, vibrant colors, high quality, digital art, professional game asset"
64
+
65
+ # Set seed for reproducibility
66
+ if seed > 0:
67
+ generator = torch.Generator(device=device).manual_seed(seed)
68
+ else:
69
+ generator = None
70
+
71
+ # Generate image
72
+ with torch.no_grad():
73
+ image = pipe(
74
+ enhanced_prompt,
75
+ guidance_scale=guidance_scale,
76
+ num_inference_steps=num_inference_steps,
77
+ generator=generator,
78
+ height=512,
79
+ width=512
80
+ ).images[0]
81
+
82
+ return image
83
+
84
+ except Exception as e:
85
+ print(f"Error generating icon: {str(e)}")
86
+ # Return a simple placeholder image
87
+ placeholder = Image.new('RGB', (512, 512), color='lightblue')
88
+ return placeholder
89
+
90
+ # Create Gradio interface
91
+ def create_interface():
92
+ # Load model first
93
+ model_loaded = load_model()
94
+
95
+ interface = gr.Interface(
96
+ fn=generate_3d_icon,
97
+ inputs=[
98
+ gr.Textbox(label="Prompt", placeholder="Describe your game icon", value="galatasaray"),
99
+ gr.Slider(minimum=0, maximum=1000, value=0, step=1, label="Seed"),
100
+ gr.Slider(minimum=1.0, maximum=20.0, value=7.5, step=0.5, label="Guidance Scale"),
101
+ gr.Slider(minimum=10, maximum=50, value=20, step=1, label="Inference Steps")
102
+ ],
103
+ outputs=gr.Image(type="pil", label="Generated Game Icon"),
104
+ title="3D Game Icon Generator",
105
+ description="Generate 3D-style game icons using AI",
106
+ examples=[
107
+ ["fantasy sword game icon", 42, 7.5, 20],
108
+ ["space shooter game icon", 123, 7.5, 20],
109
+ ["puzzle game icon with gems", 456, 7.5, 20]
110
+ ]
111
+ )
112
+
113
+ return interface
114
+
115
+ # Launch the interface
116
+ if __name__ == "__main__":
117
+ try:
118
+ interface = create_interface()
119
+ print("Launching interface...")
120
+ interface.launch(
121
+ share=False,
122
+ server_name="0.0.0.0",
123
+ server_port=7860,
124
+ show_error=True
125
+ )
126
+ except Exception as e:
127
+ print(f"Error launching interface: {str(e)}")
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ gradio==5.14.0
2
+ huggingface-hub>=0.19.0
3
+ torch>=2.0.0
4
+ transformers>=4.30.0
5
+ accelerate>=0.20.0
6
+ diffusers>=0.24.0
7
+ safetensors>=0.4.0
8
+ numpy>=1.24.0
9
+ Pillow>=10.0.0
10
+ uvicorn>=0.14.0
11
+ spaces