faizr206 commited on
Commit
77d95dc
·
1 Parent(s): cb5e24d

gemini backup if openai unavailable

Browse files
Files changed (3) hide show
  1. .dockerignore +7 -0
  2. .gitignore +2 -0
  3. app/main.py +21 -9
.dockerignore ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ __pycache__/
2
+ *.pyc
3
+ .env
4
+ .git
5
+ .gitignore
6
+ .vscode/
7
+ notes.txt
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ .env
2
+ notes.txt
app/main.py CHANGED
@@ -27,13 +27,15 @@ except ImportError:
27
 
28
  # We keep the GEMINI_* env vars for compatibility.
29
  API_KEY = os.getenv("GEMINI_API_KEY", "")
30
- MODEL = os.getenv("GEMINI_MODEL", "gemini-2.0-pro")
31
- SMALL_MODEL = os.getenv("GEMINI_SMALL_MODEL")
32
- OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") or API_KEY
 
 
33
  PORT = int(os.getenv("PORT", "7860"))
34
 
35
  gemini_client = genai.Client(api_key=API_KEY) if API_KEY else None
36
- gpt_client = OpenAI(api_key=OPENAI_API_KEY) if (OPENAI_API_KEY and OpenAI) else None
37
 
38
  # -------- FastAPI app --------
39
  app = FastAPI(title="Manim Render API (error + visual refine)")
@@ -225,10 +227,20 @@ def gemini_call(*, system: str, contents):
225
 
226
 
227
  def gemini_small_call(*, system: str, contents: str) -> str:
228
- """Lightweight wrapper for the storyboard assistant (smaller model)."""
229
- target_model = SMALL_MODEL or "gpt-4o-mini"
230
  storyboard_limiter.acquire()
231
- return _invoke_gpt_model(target_model, system, contents)
 
 
 
 
 
 
 
 
 
 
 
232
 
233
  # ---------------- prompts ----------------
234
  SYSTEM_PROMPT = """You are a Manim CE (0.19.x) code generator/refiner.
@@ -1128,7 +1140,7 @@ def _auto_fix_render(
1128
  # ---------------- API ----------------
1129
  @app.post("/storyboard/chat")
1130
  def storyboard_chat(inp: StoryboardChatIn):
1131
- if not gpt_client:
1132
  raise HTTPException(500, "Storyboard model is not configured")
1133
  if not inp.message.strip() and not inp.plan:
1134
  raise HTTPException(400, "Message or plan updates are required.")
@@ -1167,7 +1179,7 @@ def storyboard_chat(inp: StoryboardChatIn):
1167
 
1168
  @app.post("/storyboard/confirm")
1169
  def storyboard_confirm(inp: StoryboardConfirmIn):
1170
- if not gpt_client:
1171
  raise HTTPException(500, "Storyboard model is not configured")
1172
 
1173
  session = _get_or_create_session(inp.session_id, inp.settings or {})
 
27
 
28
  # We keep the GEMINI_* env vars for compatibility.
29
  API_KEY = os.getenv("GEMINI_API_KEY", "")
30
+ MODEL = os.getenv("GEMINI_MODEL", "gemini-2.5-pro")
31
+ GEMINI_SMALL_MODEL = os.getenv("GEMINI_SMALL_MODEL")
32
+ OPENAI_SMALL_MODEL = os.getenv("OPENAI_SMALL_MODEL") or "gpt-4o-mini"
33
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
34
+ USE_OPENAI = os.getenv("USE_OPENAI", "").lower() == "true"
35
  PORT = int(os.getenv("PORT", "7860"))
36
 
37
  gemini_client = genai.Client(api_key=API_KEY) if API_KEY else None
38
+ gpt_client = OpenAI(api_key=OPENAI_API_KEY) if (OPENAI_API_KEY and OpenAI and USE_OPENAI) else None
39
 
40
  # -------- FastAPI app --------
41
  app = FastAPI(title="Manim Render API (error + visual refine)")
 
227
 
228
 
229
  def gemini_small_call(*, system: str, contents: str) -> str:
230
+ """Lightweight wrapper for the storyboard assistant using a smaller model with Gemini fallback."""
 
231
  storyboard_limiter.acquire()
232
+ if gpt_client:
233
+ target_model = OPENAI_SMALL_MODEL
234
+ return _invoke_gpt_model(target_model, system, contents)
235
+ if not gemini_client:
236
+ raise RuntimeError("Gemini client is not configured")
237
+ fallback_model = GEMINI_SMALL_MODEL or MODEL
238
+ resp = gemini_client.models.generate_content(
239
+ model=fallback_model,
240
+ config=types.GenerateContentConfig(system_instruction=system),
241
+ contents=contents,
242
+ )
243
+ return getattr(resp, "text", str(resp))
244
 
245
  # ---------------- prompts ----------------
246
  SYSTEM_PROMPT = """You are a Manim CE (0.19.x) code generator/refiner.
 
1140
  # ---------------- API ----------------
1141
  @app.post("/storyboard/chat")
1142
  def storyboard_chat(inp: StoryboardChatIn):
1143
+ if not (gpt_client or gemini_client):
1144
  raise HTTPException(500, "Storyboard model is not configured")
1145
  if not inp.message.strip() and not inp.plan:
1146
  raise HTTPException(400, "Message or plan updates are required.")
 
1179
 
1180
  @app.post("/storyboard/confirm")
1181
  def storyboard_confirm(inp: StoryboardConfirmIn):
1182
+ if not (gpt_client or gemini_client):
1183
  raise HTTPException(500, "Storyboard model is not configured")
1184
 
1185
  session = _get_or_create_session(inp.session_id, inp.settings or {})