Spaces:
Sleeping
Sleeping
update models (use gpt and gemini)
Browse files14 unmodified lines15161718192021222324252627282930313250 unmodified lines8384858687888990919293943 unmodified lines989910010110210310410510610710810911011111211314 unmodified lines128129130131132133134562 unmodified lines6976986997007017027037047057067077087097104 unmodified lines71571671771871972072111 unmodified lines73373473573673773810 unmodified lines74975075175275375475575612 unmodified lines76977077177277377436 unmodified lines81181281381481581681711 unmodified lines82983083183283383427 unmodified lines8628638648658668678688698708718721 unmodified line87487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093122 unmodified lines95495595695795895996022 unmodified lines98398498598698798898999031 unmodified lines1022102310241025102610271028102992 unmodified lines112211231124112511261127112814 unmodified linesfrom dotenv import load_dotenvload_dotenv()# -------- Gemini (same SDK style as your Flask app) --------from google import genaifrom google.genai import types# somethingAPI_KEY = os.getenv("GEMINI_API_KEY", "")# Switch to 2.5 Flash as requestedMODEL = os.getenv("GEMINI_MODEL")SMALL_MODEL = os.getenv("GEMINI_SMALL_MODEL") or MODELPORT = int(os.getenv("PORT", "7860"))client = genai.Client(api_key=API_KEY) if API_KEY else None# -------- FastAPI app --------app = FastAPI(title="Manim Render API (error + visual refine)")50 unmodified lineslimiter = RateLimiter(10)storyboard_limiter = RateLimiter(30)def gemini_call(*, system: str, contents):"""Wrapper to: enforce RPM and standardize text extraction."""if not client:raise RuntimeError("Gemini client is not configured")limiter.acquire()resp = client.models.generate_content(model=MODEL,config=types.GenerateContentConfig(system_instruction=system),contents=contents,3 unmodified linesdef gemini_small_call(*, system: str, contents: str) -> str:"""Lightweight wrapper for the storyboard assistant (smaller model)."""if not client:raise RuntimeError("Gemini client is not configured")target_model = SMALL_MODEL or MODELstoryboard_limiter.acquire()resp = client.models.generate_content(model=target_model,config=types.GenerateContentConfig(system_instruction=system),contents=contents,)return getattr(resp, "text", str(resp))# ---------------- prompts ----------------SYSTEM_PROMPT = """You are a Manim CE (0.19.x) code generator/refiner.14 unmodified lines# Common Manim CE 0.19 API constraints (must follow)- Do NOT use `vertex=` with RightAngle(...). Choose the corner by line ordering or set quadrant=(±1, ±1).- Do NOT call `.to_center()` (not a valid method). Use `.center()` or `.move_to(ORIGIN)`.- Prefer `.move_to()`, `.align_to()`, `.to_edge()`, `.scale()`, `.next_to()` for layout/placement."""DEFAULT_SCENE = """from manim import *562 unmodified linesreturn mp4.read_bytes(), png_pathdef _upload_image_to_gemini(png_path: Path):"""Upload an image to Gemini Files API and return the file reference."""if not client or not png_path or not png_path.exists():return Nonelimiter.acquire()with open(png_path, "rb") as f:file_ref = client.files.upload(file=f, # <-- keyword arg, not positionalconfig={"mime_type": "image/png"} # helpful but optional)return file_ref4 unmodified linesprevious_code: Optional[str] = None,) -> str:"""First-pass generation (capture-aware)."""if not client:return DEFAULT_SCENEtry:contents = f"Create AutoScene for: {prompt}\nRemember the CAPTURE POLICY and Common API constraints."11 unmodified linesresolution = settings.get("resolution")if resolution:contents += f"\n- Design visuals that read clearly at {resolution}."response_text = gemini_call(system=SYSTEM_PROMPT, contents=contents)code = _clean_code(response_text)if "class AutoScene" not in code:10 unmodified linesoriginal_user_prompt: str,settings: Optional[Dict[str, Any]] = None,) -> str:"""When Manim fails; send the *real* CLI log/trace to Gemini."""if not client:return previous_code or DEFAULT_SCENEtry:trimmed = error_message[-4000:] if error_message else ""12 unmodified lines- Fix the bug while preserving the math logic and planned animations.- Keep exactly one class AutoScene(Scene).- Keep the CAPTURE POLICY and ensure # CAPTURE_POINT is at the final steady layout.- Scan for nonexistent methods (e.g., `.to_center`) or invalid kwargs (e.g., `vertex=` on RightAngle) and replace with valid Manim CE 0.19 API.- Prefer `.center()`/`.move_to(ORIGIN)`, and `.move_to()`, `.align_to()`, `.to_edge()`, `.next_to()` for layout.- Apply the smallest change necessary to resolve the failure; do not overhaul structure, pacing, or stylistic choices the user made.36 unmodified linesUse the screenshot to request layout/legibility/placement fixes.Includes the original prompt and current code, and asks for minimal edits."""if not client or not png_path or not png_path.exists():return previous_codetry:file_ref = _upload_image_to_gemini(png_path)11 unmodified lines- Fix layout issues (overlaps, cramped margins, alignment, consistent scaling).- Improve text legibility (minimum size ~32 px at 854x480, adequate contrast).- Ensure all intended elements are visible at the capture point.- Keep animation semantics as-is unless they're obviously broken.- Keep exactly one class AutoScene(Scene).- Preserve the CAPTURE POLICY and place `# CAPTURE_POINT` at the final steady layout with self.wait(0.75) and NO outro after that.27 unmodified linestraceback.print_exc()return previous_codedef refine_loop(user_prompt: str,settings: Optional[Dict[str, Any]] = None,max_error_refines: int = 3,do_visual_refine: bool = True,) -> bytes:"""Generate → render; on error, refine up to N times from Manim traceback → re-render.1 unmodified lineusing the saved steady-state PNG, then re-render. Fallback to the best successful MP4."""# 1) initial generation (capture-aware)code = llm_generate_manim_code(user_prompt, settings=settings)quality = _quality_from_settings(settings)# 2) render attempttry:mp4_bytes, png_path = _run_manim(code, run_id="iter0", quality=quality)except RenderError as e:print("Render failed (iter0), attempting error-based refinement...", file=sys.stderr)if max_error_refines <= 0:raiseattempts = 0last_err = e.log or ""while attempts < max_error_refines:attempts += 1refined = llm_refine_from_error(previous_code=code,error_message=last_err,original_user_prompt=user_prompt,settings=settings,)try:mp4_bytes, png_path = _run_manim(refined, run_id=f"iter_err_{attempts}", quality=quality)code = refinedbreakexcept RenderError as e2:last_err = e2.log or last_errif attempts >= max_error_refines:raiseexcept Exception:last_err = traceback.format_exc()if attempts >= max_error_refines:raiseexcept Exception:print("Unexpected error path; refining from Python traceback...", file=sys.stderr)attempts = 0last_err = traceback.format_exc()while attempts < max_error_refines:attempts += 1refined = llm_refine_from_error(previous_code=code,error_message=last_err,original_user_prompt=user_prompt,settings=settings,)try:mp4_bytes, png_path = _run_manim(refined, run_id=f"iter_err_{attempts}", quality=quality)code = refinedbreakexcept Exception:last_err = traceback.format_exc()if attempts >= max_error_refines:raise# 3) optional visual refinement loopif do_visual_refine and png_path and png_path.exists():22 unmodified linesmax_attempts: int = 3,) -> Tuple[Optional[str], Optional[bytes], str]:"""Attempt to auto-fix user code via LLM refinement if available."""if not client:return None, None, initial_logquality = _quality_from_settings(settings)attempt_code = code22 unmodified lines# ---------------- API ----------------@app.post("/storyboard/chat")def storyboard_chat(inp: StoryboardChatIn):if not client:raise HTTPException(500, "Gemini client is not configured")if not inp.message.strip() and not inp.plan:raise HTTPException(400, "Message or plan updates are required.")31 unmodified lines@app.post("/storyboard/confirm")def storyboard_confirm(inp: StoryboardConfirmIn):if not client:raise HTTPException(500, "Gemini client is not configured")session = _get_or_create_session(inp.session_id, inp.settings or {})if inp.settings:92 unmodified lines@app.post("/generate-and-render")def generate_and_render(inp: PromptIn):try:mp4 = refine_loop(inp.prompt, settings=inp.settings, max_error_refines=3, do_visual_refine=True)except Exception:raise HTTPException(500, "Failed to produce video after refinement")return Response(14 unmodified lines15161718192021222324252627282930313233343536373850 unmodified lines89909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012023 unmodified lines20620720820921021121221321414 unmodified lines229230231232233234235236562 unmodified lines7998008018028038048058068078088098108118124 unmodified lines81781881982082182282311 unmodified lines83583683783883984084184210 unmodified lines85385485585685785885986012 unmodified lines87387487587687787887988036 unmodified lines91791891992092192292311 unmodified lines93593693793893994094194227 unmodified lines9709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231 unmodified line1025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105622 unmodified lines107910801081108210831084108522 unmodified lines1108110911101111111211131114111531 unmodified lines1147114811491150115111521153115492 unmodified lines124712481249125012511252125314 unmodified linesfrom dotenv import load_dotenvload_dotenv()# -------- Gemini + GPT client setup --------from google import genaifrom google.genai import typestry:from openai import OpenAIexcept ImportError:OpenAI = None# We keep the GEMINI_* env vars for compatibility.API_KEY = os.getenv("GEMINI_API_KEY", "")MODEL = os.getenv("GEMINI_MODEL", "gemini-2.0-pro")SMALL_MODEL = os.getenv("GEMINI_SMALL_MODEL")OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") or API_KEYPORT = int(os.getenv("PORT", "7860"))gemini_client = genai.Client(api_key=API_KEY) if API_KEY else Nonegpt_client = OpenAI(api_key=OPENAI_API_KEY) if (OPENAI_API_KEY and OpenAI) else None# -------- FastAPI app --------app = FastAPI(title="Manim Render API (error + visual refine)")50 unmodified lineslimiter = RateLimiter(10)storyboard_limiter = RateLimiter(30)def _to_chat_content_item(item: Any) -> Any:if isinstance(item, str):return {"type": "text", "text": item}if isinstance(item, dict):return itemreturn {"type": "text", "text": str(item)}def _to_response_content_item(item: Any) -> Dict[str, Any]:if isinstance(item, str):return {"type": "input_text", "text": item}if isinstance(item, dict):itype = item.get("type")if itype == "text":return {"type": "input_text", "text": item.get("text", "")}if itype == "image_url":image_url = item.get("image_url", {})if isinstance(image_url, dict):return {"type": "input_image", "image_url": image_url}return {"type": "input_image", "image_url": {"url": str(image_url)}}if itype in {"input_text", "input_image", "input_file"}:return itemreturn {"type": "input_text", "text": str(item)}def _build_openai_content(contents: Any, *, for_chat: bool) -> Any:"""Normalize content payloads for chat (strings or multimodal lists) and responses API (typed blocks)."""if isinstance(contents, str):return contents if for_chat else [_to_response_content_item(contents)]if isinstance(contents, (list, tuple)):if for_chat:return [_to_chat_content_item(item) for item in contents]return [_to_response_content_item(item) for item in contents]return contents if for_chat else [_to_response_content_item(contents)]def _build_chat_messages(system: str, contents: Any) -> List[Dict[str, Any]]:return [{"role": "system", "content": system},{"role": "user", "content": _build_openai_content(contents, for_chat=True)},]def _build_responses_input(system: str, contents: Any) -> List[Dict[str, Any]]:return [{"role": "system", "content": _build_openai_content(system, for_chat=False)},{"role": "user", "content": _build_openai_content(contents, for_chat=False)},]def _extract_chat_content(resp: Any) -> str:content = resp.choices[0].message.contentif isinstance(content, str):return contentif isinstance(content, list):text_parts = []for chunk in content:if isinstance(chunk, dict) and chunk.get("type") == "text":text_parts.append(chunk.get("text", ""))else:text_parts.append(str(chunk))return "\n".join(filter(None, text_parts))return str(content)def _extract_responses_content(resp: Any) -> str:text = getattr(resp, "output_text", None)if text:return textoutput = getattr(resp, "output", None)if output:chunks = []for item in output:for elem in getattr(item, "content", []) or []:chunk_text = getattr(elem, "text", None) or getattr(elem, "content", None)if chunk_text:chunks.append(chunk_text)if chunks:return "\n".join(map(str, chunks))return str(resp)def _invoke_gpt_model(model: str, system: str, contents: Any) -> str:if not gpt_client:raise RuntimeError("GPT client is not configured")messages = _build_chat_messages(system, contents)try:resp = gpt_client.chat.completions.create(model=model, messages=messages)return _extract_chat_content(resp)except Exception as err:message = str(err)if "only supported in v1/responses" not in message:raiseresp = gpt_client.responses.create(model=model,input=_build_responses_input(system, contents),)return _extract_responses_content(resp)def gemini_call(*, system: str, contents):"""Wrapper to: enforce RPM and standardize text extraction."""if not gemini_client:raise RuntimeError("Gemini client is not configured")limiter.acquire()resp = gemini_client.models.generate_content(model=MODEL,config=types.GenerateContentConfig(system_instruction=system),contents=contents,3 unmodified linesdef gemini_small_call(*, system: str, contents: str) -> str:"""Lightweight wrapper for the storyboard assistant (smaller model)."""target_model = SMALL_MODEL or "gpt-4o-mini"storyboard_limiter.acquire()return _invoke_gpt_model(target_model, system, contents)# ---------------- prompts ----------------SYSTEM_PROMPT = """You are a Manim CE (0.19.x) code generator/refiner.14 unmodified lines# Common Manim CE 0.19 API constraints (must follow)- Do NOT use `vertex=` with RightAngle(...). Choose the corner by line ordering or set quadrant=(±1, ±1).- Do NOT call `.to_center()` (not a valid method). Use `.center()` or `.move_to(ORIGIN)`.- Prefer `.move_to()`, `.align_to()`, `.to_edge()`, `.scale()`, `.next_to()` for layout/placement, keeping generous spacing (buff ≥ 0.6) so nothing overlaps.- Only introduce objects that directly support the user's request. Avoid decorative or redundant elements that clutter the scene."""DEFAULT_SCENE = """from manim import *562 unmodified linesreturn mp4.read_bytes(), png_pathdef _upload_image_to_gemini(png_path: Path):"""Prepare an inline data URI that the OpenAI vision API accepts."""if not gemini_client or not png_path or not png_path.exists():return Nonelimiter.acquire()with open(png_path, "rb") as f:file_ref = gemini_client.files.upload(file=f,config={"mime_type": "image/png"},)return file_ref4 unmodified linesprevious_code: Optional[str] = None,) -> str:"""First-pass generation (capture-aware)."""if not gemini_client:return DEFAULT_SCENEtry:contents = f"Create AutoScene for: {prompt}\nRemember the CAPTURE POLICY and Common API constraints."11 unmodified linesresolution = settings.get("resolution")if resolution:contents += f"\n- Design visuals that read clearly at {resolution}."contents += "\nLayout requirement: ensure every element has clear separation—absolutely no overlaps at the capture point."contents += "\nKeep the composition minimal: only include elements explicitly needed for the prompt."response_text = gemini_call(system=SYSTEM_PROMPT, contents=contents)code = _clean_code(response_text)if "class AutoScene" not in code:10 unmodified linesoriginal_user_prompt: str,settings: Optional[Dict[str, Any]] = None,) -> str:"""When Manim fails; send the *real* CLI log/trace to the LLM."""if not gemini_client:return previous_code or DEFAULT_SCENEtry:trimmed = error_message[-4000:] if error_message else ""12 unmodified lines- Fix the bug while preserving the math logic and planned animations.- Keep exactly one class AutoScene(Scene).- Keep the CAPTURE POLICY and ensure # CAPTURE_POINT is at the final steady layout.- Eliminate any overlapping elements; maintain clear spacing at the capture point.- Remove any objects that are not necessary for the prompt or storyboard; keep the scene concise.- Scan for nonexistent methods (e.g., `.to_center`) or invalid kwargs (e.g., `vertex=` on RightAngle) and replace with valid Manim CE 0.19 API.- Prefer `.center()`/`.move_to(ORIGIN)`, and `.move_to()`, `.align_to()`, `.to_edge()`, `.next_to()` for layout.- Apply the smallest change necessary to resolve the failure; do not overhaul structure, pacing, or stylistic choices the user made.36 unmodified linesUse the screenshot to request layout/legibility/placement fixes.Includes the original prompt and current code, and asks for minimal edits."""if not gemini_client or not png_path or not png_path.exists():return previous_codetry:file_ref = _upload_image_to_gemini(png_path)11 unmodified lines- Fix layout issues (overlaps, cramped margins, alignment, consistent scaling).- Improve text legibility (minimum size ~32 px at 854x480, adequate contrast).- Ensure all intended elements are visible at the capture point.- Remove any overlapping elements; keep generous spacing between visuals.- Remove decorative or redundant elements that are not required by the user's prompt or storyboard.- Keep animation semantics as-is unless they're obviously broken.- Keep exactly one class AutoScene(Scene).- Preserve the CAPTURE POLICY and place `# CAPTURE_POINT` at the final steady layout with self.wait(0.75) and NO outro after that.27 unmodified linestraceback.print_exc()return previous_codedef _attempt_render_with_refine(base_code: str,*,user_prompt: str,settings: Optional[Dict[str, Any]],quality: str,run_prefix: str,max_refines: int,) -> Tuple[Optional[str], Optional[bytes], Optional[Path], str]:"""Try to render `base_code`, refining up to `max_refines` times using Gemini on failure.Returns tuple: (final_code, video_bytes, png_path, last_error_log).If rendering still fails, code/video/png are None and last_error_log carries the last trace."""attempts = 0current_code = base_codelast_log = ""while True:try:mp4_bytes, png_path = _run_manim(current_code,run_id=f"{run_prefix}_try{attempts}",quality=quality,)return current_code, mp4_bytes, png_path, ""except RenderError as err:last_log = err.log or last_logexcept Exception:last_log = traceback.format_exc()if attempts >= max_refines:return None, None, None, last_logattempts += 1current_code = llm_refine_from_error(previous_code=current_code,error_message=last_log,original_user_prompt=user_prompt,settings=settings,)def refine_loop(user_prompt: str,settings: Optional[Dict[str, Any]] = None,max_error_refines: int = 3,do_visual_refine: bool = False,) -> bytes:"""Generate → render; on error, refine up to N times from Manim traceback → re-render.1 unmodified lineusing the saved steady-state PNG, then re-render. Fallback to the best successful MP4."""# 1) initial generation (capture-aware)initial_code = llm_generate_manim_code(user_prompt, settings=settings)quality = _quality_from_settings(settings)code, mp4_bytes, png_path, last_log = _attempt_render_with_refine(initial_code,user_prompt=user_prompt,settings=settings,quality=quality,run_prefix="primary",max_refines=max_error_refines,)if code is None:print("Primary render failed after refinements; generating fallback code...", file=sys.stderr)fallback_code = llm_generate_manim_code(user_prompt, settings=settings)code, mp4_bytes, png_path, last_log = _attempt_render_with_refine(fallback_code,user_prompt=user_prompt,settings=settings,quality=quality,run_prefix="fallback",max_refines=2,)if code is None:error_message = last_log or "Render failed after fallback attempts."raise RenderError(error_message)# 3) optional visual refinement loopif do_visual_refine and png_path and png_path.exists():22 unmodified linesmax_attempts: int = 3,) -> Tuple[Optional[str], Optional[bytes], str]:"""Attempt to auto-fix user code via LLM refinement if available."""if not gemini_client:return None, None, initial_logquality = _quality_from_settings(settings)attempt_code = code22 unmodified lines# ---------------- API ----------------@app.post("/storyboard/chat")def storyboard_chat(inp: StoryboardChatIn):if not gpt_client:raise HTTPException(500, "Storyboard model is not configured")if not inp.message.strip() and not inp.plan:raise HTTPException(400, "Message or plan updates are required.")31 unmodified lines@app.post("/storyboard/confirm")def storyboard_confirm(inp: StoryboardConfirmIn):if not gpt_client:raise HTTPException(500, "Storyboard model is not configured")session = _get_or_create_session(inp.session_id, inp.settings or {})if inp.settings:92 unmodified lines@app.post("/generate-and-render")def generate_and_render(inp: PromptIn):try:mp4 = refine_loop(inp.prompt, settings=inp.settings, max_error_refines=3, do_visual_refine=False)except Exception:raise HTTPException(500, "Failed to produce video after refinement")return Response(
app/main.py+209-84