|
import gradio as gr |
|
import torch |
|
import os |
|
import sys |
|
from huggingface_hub import login |
|
|
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
print(f"Using device: {device}") |
|
|
|
|
|
print(f"Gradio version: {gr.__version__}") |
|
print(f"Python version: {sys.version}") |
|
|
|
|
|
|
|
hf_token = os.environ.get("HUGGINGFACE_TOKEN") |
|
if hf_token: |
|
print("Found HUGGINGFACE_TOKEN in environment variables") |
|
|
|
login(token=hf_token) |
|
print("Logged in with Hugging Face token") |
|
else: |
|
print("HUGGINGFACE_TOKEN not found in environment variables") |
|
|
|
def generate_3d_icon(prompt, seed=0, guidance_scale=7.5, num_inference_steps=20): |
|
try: |
|
print(f"Generating 3D icon with prompt: {prompt}") |
|
|
|
|
|
try: |
|
|
|
model = gr.load("goofyai/3d_render_style_xl", src="models") |
|
result = model(prompt) |
|
return result |
|
except Exception as e: |
|
print(f"Error with gr.load: {str(e)}") |
|
|
|
|
|
from PIL import Image, ImageDraw, ImageFont |
|
import random |
|
|
|
|
|
colors = [(255, 87, 51), (255, 165, 0), (50, 205, 50), (30, 144, 255), (138, 43, 226)] |
|
bg_color = random.choice(colors) |
|
|
|
image = Image.new('RGB', (512, 512), color=bg_color) |
|
draw = ImageDraw.Draw(image) |
|
|
|
|
|
try: |
|
|
|
font = ImageFont.truetype("/System/Library/Fonts/Arial.ttf", 36) |
|
except: |
|
font = ImageFont.load_default() |
|
|
|
|
|
bbox = draw.textbbox((0, 0), prompt, font=font) |
|
text_width = bbox[2] - bbox[0] |
|
text_height = bbox[3] - bbox[1] |
|
x = (512 - text_width) // 2 |
|
y = (512 - text_height) // 2 |
|
|
|
draw.text((x, y), prompt, fill=(255, 255, 255), font=font) |
|
|
|
|
|
label = "3D Game Icon" |
|
try: |
|
label_font = ImageFont.truetype("/System/Library/Fonts/Arial.ttf", 24) |
|
except: |
|
label_font = ImageFont.load_default() |
|
|
|
draw.text((20, 20), label, fill=(255, 255, 255), font=label_font) |
|
|
|
return image |
|
|
|
except Exception as e: |
|
print(f"Error generating icon: {str(e)}") |
|
|
|
from PIL import Image, ImageDraw |
|
|
|
image = Image.new('RGB', (512, 512), color='lightgray') |
|
draw = ImageDraw.Draw(image) |
|
draw.text((200, 250), "Error", fill=(255, 0, 0)) |
|
return image |
|
|
|
|
|
def create_interface(): |
|
interface = gr.Interface( |
|
fn=generate_3d_icon, |
|
inputs=[ |
|
gr.Textbox(label="Prompt", placeholder="Describe your game icon", value="galatasaray"), |
|
gr.Slider(minimum=0, maximum=1000, value=0, step=1, label="Seed"), |
|
gr.Slider(minimum=1.0, maximum=20.0, value=7.5, step=0.5, label="Guidance Scale"), |
|
gr.Slider(minimum=10, maximum=50, value=20, step=1, label="Inference Steps") |
|
], |
|
outputs=gr.Image(type="pil", label="Generated Game Icon"), |
|
title="3D Game Icon Generator", |
|
description="Generate 3D-style game icons using AI" |
|
) |
|
|
|
return interface |
|
|
|
|
|
if __name__ == "__main__": |
|
try: |
|
interface = create_interface() |
|
print("Launching interface...") |
|
interface.launch( |
|
share=False, |
|
server_name="0.0.0.0", |
|
server_port=7860, |
|
show_error=True |
|
) |
|
except Exception as e: |
|
print(f"Error launching interface: {str(e)}") |