Spaces:
Build error
Build error
faizr206 commited on
Commit ·
95e889a
1
Parent(s): 193f4f7
code only request
Browse files12345645 unmodified lines5253545556575859606116 unmodified lines7879808182838470 unmodified lines1551561571581591601611621634 unmodified lines168169170171172173import os, re, uuid, subprocess, sys, tracebackfrom pathlib import Pathfrom typing import Optional, Tuplefrom fastapi import FastAPI, HTTPException, Responsefrom fastapi.middleware.cors import CORSMiddleware45 unmodified lines# ---------------- helpers ----------------def _clean_code(text: str) -> str:if not text:return ""# strip code fences if presentreturn re.sub(r"^```(?:python)?\\n|```$", "", text, flags=re.MULTILINE).strip()def _run_manim(scene_code: str, run_id: Optional[str] = None) -> bytes:"""Render with Manim (low quality, fast) and return MP4 bytes."""16 unmodified lines]proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, env=env)if proc.returncode != 0:print("Manim stdout/stderr:\\n", proc.stdout, file=sys.stderr)raise RuntimeError("Manim render failed")# Locate output mp470 unmodified linestraceback.print_exc()if max_error_refines <= 0:raiserefined = llm_refine_from_error(previous_code=code, error_message=traceback.format_exc())# Try a second renderreturn _run_manim(refined, run_id="iter1")# ---------------- API ----------------4 unmodified linesdef health():return {"ok": True, "model": MODEL, "has_gemini": bool(API_KEY)}@app.post("/generate-and-render")def generate_and_render(inp: PromptIn):if not inp.prompt or not inp.prompt.strip():12345645 unmodified lines5253545556575859606162636465666716 unmodified lines8485868788899070 unmodified lines1611621631641651661674 unmodified lines172173174175176177178179180181182183184185186187188import os, re, uuid, subprocess, sys, tracebackfrom pathlib import Pathfrom typing import Optionalfrom fastapi import FastAPI, HTTPException, Responsefrom fastapi.middleware.cors import CORSMiddleware45 unmodified lines# ---------------- helpers ----------------def _clean_code(text: str) -> str:"""Strip common Markdown fences like ```python ... ``` or ``` ...The older pattern used '\\n' literally, which can fail to remove fences."""if not text:return ""# Remove leading '```[python]' (with optional whitespace) and trailing '```'text = re.sub(r"^```(?:\s*python)?\s*", "", text.strip(), flags=re.IGNORECASE)text = re.sub(r"\s*```$", "", text)return text.strip()def _run_manim(scene_code: str, run_id: Optional[str] = None) -> bytes:"""Render with Manim (low quality, fast) and return MP4 bytes."""16 unmodified lines]proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, env=env)if proc.returncode != 0:print("Manim stdout/stderr:\n", proc.stdout, file=sys.stderr)raise RuntimeError("Manim render failed")# Locate output mp470 unmodified linestraceback.print_exc()if max_error_refines <= 0:raiserefined = llm_refine_from_error(previous_code=code, error_message=traceback.format_exc())return _run_manim(refined, run_id="iter1")# ---------------- API ----------------4 unmodified linesdef health():return {"ok": True, "model": MODEL, "has_gemini": bool(API_KEY)}@app.post("/generate-code")def generate_code(inp: PromptIn):"""Return ONLY the generated Manim Python code (no rendering).Body: {"prompt":"..."} -> {"code":"..."}"""if not inp.prompt or not inp.prompt.strip():raise HTTPException(400, "Missing prompt")code = llm_generate_manim_code(inp.prompt.strip())return {"code": code}@app.post("/generate-and-render")def generate_and_render(inp: PromptIn):if not inp.prompt or not inp.prompt.strip():
app/main.py+21-6