|
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): |
|
try: |
|
print(f"Generating 3D icon with prompt: {prompt}") |
|
|
|
|
|
try: |
|
print("Trying to load goofyai/3d_render_style_xl...") |
|
|
|
|
|
model = gr.load("goofyai/3d_render_style_xl", src="models") |
|
result = model(prompt) |
|
print("Model loaded and generated successfully") |
|
return result |
|
|
|
except Exception as e1: |
|
print(f"Method 1 failed: {str(e1)}") |
|
|
|
try: |
|
|
|
print("Trying spaces source...") |
|
model = gr.load("goofyai/3d_render_style_xl", src="spaces") |
|
result = model(prompt) |
|
print("Spaces model loaded successfully") |
|
return result |
|
|
|
except Exception as e2: |
|
print(f"Method 2 failed: {str(e2)}") |
|
|
|
try: |
|
|
|
print("Trying huggingface source...") |
|
model = gr.load("goofyai/3d_render_style_xl", src="huggingface") |
|
result = model(prompt) |
|
print("HuggingFace model loaded successfully") |
|
return result |
|
|
|
except Exception as e3: |
|
print(f"Method 3 failed: {str(e3)}") |
|
|
|
try: |
|
|
|
print("Trying Interface.load...") |
|
model = gr.Interface.load("models/goofyai/3d_render_style_xl") |
|
result = model(prompt) |
|
print("Interface.load successful") |
|
return result |
|
|
|
except Exception as e4: |
|
print(f"Method 4 failed: {str(e4)}") |
|
|
|
|
|
from PIL import Image, ImageDraw, ImageFont |
|
|
|
image = Image.new('RGB', (512, 512), color='red') |
|
draw = ImageDraw.Draw(image) |
|
|
|
error_text = "Model loading failed" |
|
draw.text((200, 250), error_text, fill=(255, 255, 255)) |
|
|
|
print("All methods failed, returning error image") |
|
return image |
|
|
|
except Exception as e: |
|
print(f"General error: {str(e)}") |
|
|
|
from PIL import Image, ImageDraw |
|
|
|
image = Image.new('RGB', (512, 512), color='gray') |
|
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") |
|
], |
|
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)}") |