Spaces:
Running on Zero
Running on Zero
Download server.py from Pradeepccc/a: direct link, hf CLI and curl.
- Browser
- Download file 4.43 kB
-
https://huggingface.co/spaces/Pradeepccc/a/resolve/main/server.py
- Command line
-
hf download hf://spaces/Pradeepccc/a/server.py
-
curl -L -o server.py https://huggingface.co/spaces/Pradeepccc/a/resolve/main/server.py
4.43 kB
| """FastAPI Server for Qwen-Image-2.1 Viggle Turbo. | |
| Serves the modern Shadcn/Tailwind pure HTML frontend at '/' | |
| and connects to Qwen AI diffusion logic via clean REST APIs. | |
| """ | |
| import os | |
| import io | |
| import json | |
| import base64 | |
| import time | |
| import random | |
| from fastapi import FastAPI, Request | |
| from fastapi.responses import HTMLResponse, JSONResponse, FileResponse | |
| from fastapi.middleware.cors import CORSMiddleware | |
| import uvicorn | |
| from PIL import Image | |
| # Ensure preview mode environment is preserved if set | |
| import app | |
| fastapi_app = FastAPI(title="Qwen Studio 2.1 API") | |
| # Enable CORS | |
| fastapi_app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) | |
| INDEX_HTML_PATH = os.path.join(CURRENT_DIR, "index.html") | |
| async def serve_index(): | |
| if os.path.exists(INDEX_HTML_PATH): | |
| with open(INDEX_HTML_PATH, "r", encoding="utf-8") as f: | |
| return HTMLResponse(content=f.read()) | |
| return HTMLResponse("<h1>index.html not found</h1>", status_code=404) | |
| async def health_check(): | |
| return { | |
| "status": "ok", | |
| "preview_mode": bool(app.pipe is None or app.IS_PREVIEW), | |
| "model_id": app.MODEL_ID, | |
| "default_lora": app.DEFAULT_LORA | |
| } | |
| async def api_search_loras(q: str = ""): | |
| choices = app.search_hf_loras(q) | |
| models = [] | |
| for item in choices: | |
| if isinstance(item, tuple): | |
| models.append({"repo_id": item[1], "name": item[0]}) | |
| elif isinstance(item, str): | |
| models.append({"repo_id": item, "name": item}) | |
| return {"models": models} | |
| async def api_repo_files(repo: str = ""): | |
| files = app.fetch_repo_safetensors(repo) | |
| return {"files": files} | |
| async def api_generate(request: Request): | |
| try: | |
| data = await request.json() | |
| prompt = data.get("prompt", "") | |
| mode = data.get("mode", "Text to Image") | |
| lora_adapter = data.get("lora_adapter", app.DEFAULT_LORA) | |
| lora_strength = float(data.get("lora_strength", 1.0)) | |
| custom_repo = data.get("custom_repo", "") | |
| custom_file = data.get("custom_file", "") | |
| aspect_ratio = data.get("aspect_ratio", app.AUTO) | |
| steps = int(data.get("steps", 6)) | |
| seed = int(data.get("seed", 42)) | |
| randomize_seed = bool(data.get("randomize_seed", True)) | |
| ref_image_1 = None | |
| ref_image_2 = None | |
| if data.get("ref_image_1_b64"): | |
| b64_data = data["ref_image_1_b64"].split(",")[-1] | |
| ref_image_1 = Image.open(io.BytesIO(base64.b64decode(b64_data))) | |
| if data.get("ref_image_2_b64"): | |
| b64_data = data["ref_image_2_b64"].split(",")[-1] | |
| ref_image_2 = Image.open(io.BytesIO(base64.b64decode(b64_data))) | |
| # Invoke core generate function | |
| result_img, dl_path, status_str, used_prompt, idb_payload = app.generate( | |
| prompt=prompt, | |
| mode=mode, | |
| ref_image_1=ref_image_1, | |
| ref_image_2=ref_image_2, | |
| lora_adapter=lora_adapter, | |
| lora_strength=lora_strength, | |
| custom_repo=custom_repo, | |
| custom_file=custom_file, | |
| aspect_ratio=aspect_ratio, | |
| steps=steps, | |
| seed=seed, | |
| randomize_seed=randomize_seed, | |
| ) | |
| # Convert image to base64 PNG data URL | |
| buf = io.BytesIO() | |
| result_img.save(buf, format="PNG") | |
| img_b64 = "data:image/png;base64," + base64.b64encode(buf.getvalue()).decode("utf-8") | |
| parsed_idb = json.loads(idb_payload) | |
| return { | |
| "image_b64": img_b64, | |
| "status": status_str, | |
| "prompt": used_prompt, | |
| "seed": parsed_idb.get("seed"), | |
| "steps": steps, | |
| "width": result_img.width, | |
| "height": result_img.height, | |
| } | |
| except Exception as e: | |
| return JSONResponse(status_code=400, content={"error": str(e)}) | |
| if __name__ == "__main__": | |
| print("=" * 70) | |
| print(" QWEN STUDIO 2.1 - PURE HTML / FASTAPI SERVER") | |
| print(" Serving Pure HTML Frontend at http://127.0.0.1:7860") | |
| print("=" * 70) | |
| uvicorn.run(fastapi_app, host="127.0.0.1", port=7860) | |