Download app.py from ovjstudio/README: direct link, hf CLI and curl.
- Browser
- Download file 4.94 kB
-
https://huggingface.co/spaces/ovjstudio/README/resolve/main/app.py
- Command line
-
hf download hf://spaces/ovjstudio/README/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ovjstudio/README/resolve/main/app.py
4.94 kB
| # ์์ ๋ ๋ฒ์ โ SyntaxError ํด๊ฒฐ (try/except ์ ๋ ฌ ์ค๋ฅ ์์ ) | |
| # ๊ธฐ์กด ์ฝ๋์์ line 133 ๊ทผ์ฒ์ try ๋ธ๋ก์ด ์๋ชป ๋ค์ฌ์ฐ๊ธฐ๋์ด ํ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค. | |
| # ์ด์ ์ฌ๋ฐ๋ฅธ try/except ๊ตฌ์กฐ๋ก ๊ต์ ๋์ด Hugging Face Space์์ ์คํ๋ฉ๋๋ค. | |
| import os | |
| import io | |
| import base64 | |
| import random | |
| from typing import Optional, Callable, Tuple | |
| import requests | |
| from PIL import Image | |
| try: | |
| import gradio as gr | |
| GRADIO_AVAILABLE = True | |
| except Exception: | |
| GRADIO_AVAILABLE = False | |
| class _GradioError(RuntimeError): | |
| pass | |
| class gr: | |
| Error = _GradioError | |
| GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") or os.getenv("BANANA_API_KEY") | |
| GEMINI_MODEL = os.getenv("GEMINI_MODEL") or os.getenv("BANANA_MODEL_KEY") or "gemini-2.5-flash-image" | |
| APP_TITLE = os.getenv("APP_TITLE", "OVJ STUDIO ร Gemini 2.5 Flash Image") | |
| GEMINI_RUN_URL = f"https://generativelanguage.googleapis.com/v1beta/models/{GEMINI_MODEL}:generateContent" | |
| def _ensure_secrets_for_runtime(): | |
| if not GEMINI_API_KEY: | |
| raise RuntimeError("GEMINI_API_KEY๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. HF Settings โ Variables & Secrets์์ ์ถ๊ฐํ์ธ์.") | |
| def _to_image_from_b64(b64_str: str) -> Image.Image: | |
| data = base64.b64decode(b64_str) | |
| return Image.open(io.BytesIO(data)).convert("RGB") | |
| def _compose_prompt(prompt: str, negative: str, w: int, h: int, steps: int, guidance: float, seed: int) -> str: | |
| meta = f"\n\n[meta] size={w}x{h}, steps={steps}, guidance={guidance}, seed={seed}" | |
| if negative: | |
| meta += f", negative={negative}" | |
| return f"{prompt}{meta}" | |
| def generate_image(prompt: str, negative_prompt: str, steps: int, guidance: float, width: int, height: int, seed: Optional[int], *, requests_post: Callable = requests.post) -> Tuple[Image.Image, str]: | |
| if not prompt or prompt.strip() == "": | |
| raise gr.Error("ํ๋กฌํํธ๋ฅผ ์ ๋ ฅํ์ธ์.") | |
| _ensure_secrets_for_runtime() | |
| if seed is None or int(seed) < 0: | |
| seed = random.randint(0, 2**31 - 1) | |
| contents = [{"parts": [{"text": _compose_prompt(prompt, negative_prompt, width, height, steps, guidance, seed)}]}] | |
| payload = {"contents": contents} | |
| headers = {"x-goog-api-key": GEMINI_API_KEY, "Content-Type": "application/json"} | |
| try: | |
| resp = requests_post(GEMINI_RUN_URL, json=payload, headers=headers, timeout=600) | |
| except requests.RequestException as e: | |
| raise gr.Error(f"Gemini API ์ฐ๊ฒฐ ์คํจ: {e}") | |
| except Exception as e: | |
| raise gr.Error(f"์์ฒญ ์ํ ์ค ์์ธ: {e}") | |
| if not getattr(resp, "ok", False): | |
| try: | |
| err = resp.json() | |
| except Exception: | |
| err = getattr(resp, "text", str(resp)) | |
| raise gr.Error(f"Gemini API ์ค๋ฅ: HTTP {getattr(resp, 'status_code', '??')}\n{err}") | |
| try: | |
| data = resp.json() | |
| except Exception as e: | |
| body = getattr(resp, "text", "") | |
| raise gr.Error(f"API ์๋ต ํ์ฑ ์คํจ: {e}\n๋ณธ๋ฌธ={body[:500]}") | |
| try: | |
| candidates = data.get("candidates", []) | |
| if not candidates: | |
| raise KeyError("candidates๊ฐ ๋น์ด ์์ต๋๋ค.") | |
| parts = candidates[0].get("content", {}).get("parts", []) | |
| b64 = None | |
| for p in parts: | |
| inline_data = p.get("inlineData") or p.get("inline_data") | |
| if inline_data: | |
| b64 = inline_data.get("data") or inline_data.get("bytes") | |
| if b64: | |
| break | |
| if not b64: | |
| raise KeyError("inlineData ์์ ์ด๋ฏธ์ง ๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค.") | |
| img = _to_image_from_b64(b64) | |
| return img, str(seed) | |
| except Exception as e: | |
| snippet = str(data)[:500] | |
| raise gr.Error(f"์๋ต ์ฒ๋ฆฌ ์ค ์ค๋ฅ: {e}\n์๋ต ๋ฏธ๋ฆฌ๋ณด๊ธฐ={snippet}") | |
| if GRADIO_AVAILABLE: | |
| with gr.Blocks(title=APP_TITLE) as demo: | |
| gr.Markdown(f"# {APP_TITLE}\n**OVJ STUDIO** ร Gemini 2.5 Flash Image") | |
| prompt = gr.Textbox(label="Prompt", lines=3) | |
| negative = gr.Textbox(label="Negative Prompt", lines=2) | |
| steps = gr.Slider(5, 60, value=28, step=1, label="Steps") | |
| guidance = gr.Slider(0.0, 20.0, value=7.5, step=0.1, label="Guidance") | |
| width = gr.Dropdown([512, 640, 768, 832, 1024], value=768, label="Width") | |
| height = gr.Dropdown([512, 640, 768, 832, 1024], value=768, label="Height") | |
| seed = gr.Number(value=-1, precision=0, label="Seed (-1=random)") | |
| out_img = gr.Image(label="Result", type="pil") | |
| out_seed = gr.Textbox(label="Used Seed", interactive=False) | |
| btn = gr.Button("Generate") | |
| btn.click(generate_image, [prompt, negative, steps, guidance, width, height, seed], [out_img, out_seed]) | |
| if __name__ == "__main__": | |
| if GRADIO_AVAILABLE: | |
| _ensure_secrets_for_runtime() | |
| demo.launch() | |
| else: | |
| print("[INFO] gradio ๋ฏธ์ค์น ํ๊ฒฝ โ ํ ์คํธ ๋ชจ๋ ์คํ ์๋ต") |