File size: 4,368 Bytes
32c5da4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "37de3483",
   "metadata": {},
   "source": [
    "# PixelForge with Z-Image Turbo (Colab Test)\n",
    "Test stärkere Modelle mit kostenloser GPU"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0d963b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. Install dependencies\n",
    "!pip install -q torch diffusers transformers accelerate numpy pillow requests -U\n",
    "!pip install -q flask flask-cors python-dotenv\n",
    "print(\"✓ Dependencies installed\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f93fdb6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2. Check GPU\n",
    "import torch\n",
    "print(f\"GPU Available: {torch.cuda.is_available()}\")\n",
    "if torch.cuda.is_available():\n",
    "    print(f\"GPU: {torch.cuda.get_device_name(0)}\")\n",
    "    print(f\"VRAM: {torch.cuda.get_device_properties(0).total_memory / 1e9:.2f} GB\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e1d69f6e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3. Download & Load Model (beispiel: FLUX oder ähnlich)\n",
    "# Optional: Ersetze mit deinem Z-Image Turbo Modell\n",
    "\n",
    "from diffusers import StableDiffusionPipeline\n",
    "import torch\n",
    "\n",
    "MODEL_ID = \"segmind/tiny-sd\"  # oder dein Z-Image Turbo\n",
    "print(f\"Loading {MODEL_ID}...\")\n",
    "\n",
    "pipe = StableDiffusionPipeline.from_pretrained(\n",
    "    MODEL_ID,\n",
    "    torch_dtype=torch.float16,\n",
    "    safety_checker=None,\n",
    "    requires_safety_checker=False\n",
    ")\n",
    "pipe = pipe.to(\"cuda\")\n",
    "print(\"✓ Model loaded\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7575df6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4. Test Generation\n",
    "prompt = \"A beautiful futuristic city at sunset\"\n",
    "print(f\"Generating: {prompt}\")\n",
    "\n",
    "image = pipe(\n",
    "    prompt,\n",
    "    height=512,\n",
    "    width=512,\n",
    "    num_inference_steps=20,\n",
    "    guidance_scale=7.5\n",
    ").images[0]\n",
    "\n",
    "image.save(\"/tmp/test_output.png\")\n",
    "print(\"✓ Image generated: /tmp/test_output.png\")\n",
    "image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c84cc93",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 5. Start API Server (optional: expose to PixelForge)\n",
    "from flask import Flask, request, jsonify\n",
    "import json\n",
    "import base64\n",
    "from io import BytesIO\n",
    "\n",
    "app = Flask(__name__)\n",
    "\n",
    "@app.route('/health', methods=['GET'])\n",
    "def health():\n",
    "    return jsonify({\"status\": \"ok\", \"model\": MODEL_ID, \"gpu\": torch.cuda.get_device_name(0)})\n",
    "\n",
    "@app.route('/generate', methods=['POST'])\n",
    "def generate():\n",
    "    data = request.json\n",
    "    prompt = data.get('prompt', 'a test image')\n",
    "    steps = data.get('steps', 20)\n",
    "    guidance = data.get('guidance', 7.5)\n",
    "    \n",
    "    print(f\"Generating: {prompt}\")\n",
    "    image = pipe(\n",
    "        prompt,\n",
    "        height=512,\n",
    "        width=512,\n",
    "        num_inference_steps=steps,\n",
    "        guidance_scale=guidance\n",
    "    ).images[0]\n",
    "    \n",
    "    # Convert to base64\n",
    "    buffered = BytesIO()\n",
    "    image.save(buffered, format=\"PNG\")\n",
    "    img_base64 = base64.b64encode(buffered.getvalue()).decode()\n",
    "    \n",
    "    return jsonify({\n",
    "        \"success\": True,\n",
    "        \"image\": img_base64,\n",
    "        \"prompt\": prompt\n",
    "    })\n",
    "\n",
    "# Ngrok tunnel (kostenlos)\n",
    "from pyngrok import ngrok\n",
    "ngrok.set_auth_token(\"DEIN_NGROK_TOKEN\")  # Gratis auf ngrok.com registrieren\n",
    "\n",
    "print(\"Starting API server...\")\n",
    "public_url = ngrok.connect(5000)\n",
    "print(f\"Public URL: {public_url}\")\n",
    "print(\"Use this URL in PixelForge config!\")\n",
    "\n",
    "app.run(port=5000)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}