File size: 14,371 Bytes
2bdb7ce
 
 
 
 
 
 
 
 
 
 
 
 
 
72d949d
2bdb7ce
 
 
 
 
189b68e
2bdb7ce
0ef6cca
2bdb7ce
 
0ef6cca
72d949d
2bdb7ce
 
 
0ef6cca
 
 
 
 
 
 
 
 
 
 
 
b9621c6
0ef6cca
 
 
 
 
 
 
b9621c6
 
0ef6cca
6181a36
 
 
 
 
0ef6cca
02a25f1
97cd083
 
 
0ef6cca
97cd083
6181a36
b041735
0ef6cca
 
97cd083
b041735
0ef6cca
 
 
 
 
 
 
6181a36
b041735
 
6181a36
b041735
 
 
2bdb7ce
0ef6cca
 
 
 
 
 
 
 
 
 
 
 
2bdb7ce
6181a36
0ef6cca
02a25f1
0ef6cca
02a25f1
6181a36
 
 
97cd083
 
6181a36
97cd083
6181a36
 
97cd083
6181a36
0ef6cca
97cd083
6181a36
 
0ef6cca
 
 
02a25f1
72d949d
0ef6cca
 
 
 
 
 
 
72d949d
0ef6cca
 
 
 
6181a36
 
 
0ef6cca
97cd083
72d949d
 
 
97cd083
72d949d
 
0ef6cca
 
 
97cd083
0ef6cca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72d949d
97cd083
72d949d
 
97cd083
 
0ef6cca
 
97cd083
0ef6cca
97cd083
2bdb7ce
0ef6cca
 
97cd083
0ef6cca
 
 
 
 
 
 
 
97cd083
72d949d
0ef6cca
6181a36
0ef6cca
6181a36
97cd083
 
0ef6cca
 
 
 
 
 
 
 
 
 
 
 
 
97cd083
0ef6cca
2bdb7ce
0ef6cca
 
6181a36
 
0ef6cca
 
 
 
 
 
72d949d
2bdb7ce
0ef6cca
 
 
 
72d949d
97cd083
0ef6cca
6181a36
 
72d949d
97cd083
0ef6cca
 
 
97cd083
0ef6cca
 
 
6181a36
0ef6cca
6181a36
0ef6cca
 
 
 
97cd083
0ef6cca
97cd083
 
 
0ef6cca
6181a36
97cd083
0ef6cca
 
6181a36
0ef6cca
97cd083
 
 
 
0ef6cca
2bdb7ce
0ef6cca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6181a36
0ef6cca
2bdb7ce
0ef6cca
 
 
 
 
 
 
 
 
 
 
 
 
6181a36
 
 
0ef6cca
 
 
 
 
 
 
 
 
 
 
02a25f1
0ef6cca
72d949d
97cd083
0ef6cca
2bdb7ce
0ef6cca
72d949d
0ef6cca
 
2bdb7ce
0ef6cca
72d949d
0ef6cca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bdb7ce
 
02a25f1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import streamlit as st
import tempfile
import os
import logging
from pathlib import Path
from PIL import Image
import io
import numpy as np
import sys
import subprocess
import json
import re
import shutil
import time
from datetime import datetime
import streamlit.components.v1 as components
import uuid
import pandas as pd
import plotly.express as px
import zipfile
import traceback

# Set up logging
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)s %(name)s – %(message)s",
    handlers=[logging.StreamHandler()]
)
logger = logging.getLogger(__name__)

# Available quality presets and formats
QUALITY_PRESETS = ["480p", "720p", "1080p", "4K", "8K"]
FPS_OPTIONS = [24, 30, 60, 120]
OUTPUT_FORMATS = {
    "MP4 Video": "mp4",
    "GIF Animation": "gif",
    "WebM Video": "webm",
    "PNG Sequence (ZIP)": "png_sequence",
    "SVG Image": "svg"
}

# Model configurations
MODEL_CONFIGS = {
    "gpt-4o": {},
    "gpt-4o-mini": {},
    "gpt-4.1": {},
    "gpt-4.1-mini": {},
    "o1": {},
    "o1-mini": {},
    "default": {}
}

# Try to import st_ace
try:
    from streamlit_ace import st_ace
    ACE_EDITOR_AVAILABLE = True
except ImportError:
    ACE_EDITOR_AVAILABLE = False
    logger.warning("streamlit-ace not available, using text_area")

def get_secret(env_var):
    val = os.environ.get(env_var)
    if not val:
        logger.warning(f"Secret '{env_var}' not set")
    return val

def check_password():
    pwd = get_secret("password")
    if not pwd:
        st.error("Admin password not configured")
        return False
    if "pwd_ok" not in st.session_state:
        st.session_state.pwd_ok = False
    if not st.session_state.pwd_ok:
        entry = st.text_input("Enter password", type="password")
        if entry:
            if entry == pwd:
                st.session_state.pwd_ok = True
                return True
            else:
                st.error("Incorrect password")
                return False
        return False
    return True

def ensure_packages():
    reqs = {
        "manim": "0.17.3",
        "Pillow": "9.0.0",
        "numpy": "1.22.0",
        "plotly": "5.14.0",
        "pandas": "2.0.0",
        "python-pptx": "0.6.21",
        "fpdf": "1.7.2",
        "matplotlib": "3.5.0",
        "seaborn": "0.11.2",
        "scipy": "1.7.3",
        "streamlit-ace": "0.1.1"
    }
    missing = {}
    for pkg, ver in reqs.items():
        try:
            __import__(pkg if pkg != "Pillow" else "PIL")
        except ImportError:
            missing[pkg] = ver
    if not missing:
        return True
    bar = st.progress(0)
    txt = st.empty()
    for i, (pkg, ver) in enumerate(missing.items()):
        txt.text(f"Installing {pkg}...")
        res = subprocess.run([sys.executable, "-m", "pip", "install", f"{pkg}>={ver}"], capture_output=True, text=True)
        if res.returncode != 0:
            st.error(f"Failed to install {pkg}")
            return False
        bar.progress((i + 1) / len(missing))
    txt.empty()
    return True

def extract_scene_class_name(code):
    m = re.findall(r"class\s+(\w+)\s*\([^)]*Scene", code)
    return m[0] if m else "MyScene"

def generate_manim_preview(code):
    icons = []
    if "Circle" in code: icons.append("β­•")
    if "Square" in code: icons.append("πŸ”²")
    if "MathTex" in code or "Tex" in code: icons.append("πŸ“Š")
    if "Text" in code: icons.append("πŸ“")
    if "Axes" in code: icons.append("πŸ“ˆ")
    preview_icons = "".join(icons) or "🎬"
    return f"""
    <div style="background:#000;color:#fff;padding:1rem;border-radius:8px;text-align:center;">
      <h3>Preview</h3>
      <div style="font-size:2rem;">{preview_icons}</div>
      <p>Full render for accurate output</p>
    </div>
    """

def generate_manim_video(code, fmt, quality, fps, audio_path=None):
    temp_dir = tempfile.mkdtemp(prefix="manim_")
    scene = extract_scene_class_name(code)
    scene_file = os.path.join(temp_dir, "scene.py")
    with open(scene_file, "w") as f:
        f.write(code)
    qflags = {"480p":"-ql","720p":"-qm","1080p":"-qh","4K":"-qk","8K":"-qp"}
    qf = qflags.get(quality, "-qm")
    cmd = ["manim", scene_file, scene, qf, f"--format={fmt}", f"--fps={fps}"]
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
    progress = st.progress(0)
    log = st.empty()
    total = None
    for line in process.stdout:
        log.code(line)
        m = re.search(r"(\d+)\s*/\s*(\d+)", line)
        if m:
            cur, tot = map(int, m.groups())
            total = tot
            progress.progress(min(0.99, cur / tot))
        else:
            p = re.search(r"(\d+)%", line)
            if p:
                progress.progress(min(0.99, int(p.group(1)) / 100))
    process.wait()
    progress.progress(1.0)
    # locate output
    out_path = None
    for root, _, files in os.walk(temp_dir):
        for f in files:
            if f.endswith(f".{fmt}") or (fmt=="png_sequence" and f.endswith(".zip")):
                out_path = os.path.join(root, f)
                break
    data = None
    if out_path and os.path.exists(out_path):
        with open(out_path, "rb") as f:
            data = f.read()
    shutil.rmtree(temp_dir)
    if data:
        size_mb = len(data) / (1024 * 1024)
        return data, f"βœ… Done ({size_mb:.1f} MB)"
    else:
        return None, "❌ No output generated"

def main():
    st.set_page_config("Manim Studio", "🎬", layout="wide")
    if "inited" not in st.session_state:
        st.session_state.update({
            "inited": True,
            "code": "",
            "video_data": None,
            "status": "",
            "settings": {"quality":"720p","fps":30,"format":"mp4"},
            "pwd_ok": False,
            "custom_model": "gpt-4o",
            "generated_code": ""
        })

    if not st.session_state.get("packages_checked", False):
        if ensure_packages():
            st.session_state.packages_checked = True
        else:
            return

    tabs = st.tabs([
        "✨ Editor", "πŸ€– AI Assistant", "🎨 Assets",
        "🎞️ Timeline", "πŸŽ“ Export", "🐍 Python Runner"
    ])

    # Sidebar settings
    with st.sidebar:
        st.markdown("## βš™οΈ Render Settings")
        q = st.selectbox("Quality", QUALITY_PRESETS, index=QUALITY_PRESETS.index(st.session_state["settings"]["quality"]))
        f = st.selectbox("FPS", FPS_OPTIONS, index=FPS_OPTIONS.index(st.session_state["settings"]["fps"]))
        fmt_disp = st.selectbox("Format", list(OUTPUT_FORMATS.keys()),
                                 index=list(OUTPUT_FORMATS.values()).index(st.session_state["settings"]["format"]))
        st.session_state["settings"] = {"quality": q, "fps": f, "format": OUTPUT_FORMATS[fmt_disp]}

    # Editor tab
    with tabs[0]:
        c1, c2 = st.columns([3,2])
        with c1:
            st.markdown("### πŸ“ Animation Editor")
            if ACE_EDITOR_AVAILABLE:
                st.session_state.code = st_ace(
                    value=st.session_state.code,
                    language="python",
                    theme="monokai",
                    min_lines=20,
                    key="ace"
                )
            else:
                st.session_state.code = st.text_area(
                    "Manim Code",
                    value=st.session_state.code,
                    height=400
                )
            if st.button("πŸš€ Generate Animation"):
                if not st.session_state.code.strip():
                    st.error("Enter code first")
                else:
                    data, msg = generate_manim_video(
                        st.session_state.code,
                        st.session_state["settings"]["format"],
                        st.session_state["settings"]["quality"],
                        st.session_state["settings"]["fps"]
                    )
                    st.session_state.video_data = data
                    st.session_state.status = msg
        with c2:
            if st.session_state.code:
                components.html(generate_manim_preview(st.session_state.code), height=250)
            if st.session_state.video_data:
                fmt = st.session_state["settings"]["format"]
                if fmt == "png_sequence":
                    st.download_button("⬇️ Download ZIP", st.session_state.video_data, "animation.zip", "application/zip")
                elif fmt == "svg":
                    try:
                        svg = st.session_state.video_data.decode("utf-8")
                        components.html(svg, height=400)
                    except:
                        st.error("Cannot display SVG")
                    st.download_button("⬇️ Download SVG", st.session_state.video_data, "animation.svg", "image/svg+xml")
                else:
                    st.video(st.session_state.video_data, format=fmt)
                    st.download_button(f"⬇️ Download {fmt.upper()}", st.session_state.video_data,
                                       f"animation.{fmt}", f"video/{fmt}")
            if st.session_state.status:
                if st.session_state.status.startswith("❌"):
                    st.error(st.session_state.status)
                else:
                    st.success(st.session_state.status)

    # AI Assistant tab
    with tabs[1]:
        st.markdown("### πŸ€– AI Assistant")
        if not check_password():
            return
        model = st.selectbox("Select AI Model", list(MODEL_CONFIGS.keys()),
                             index=list(MODEL_CONFIGS.keys()).index(st.session_state.custom_model))
        st.session_state.custom_model = model
        token = get_secret("github_token_api")
        if st.button("Test Connection"):
            from azure.ai.inference import ChatCompletionsClient
            from azure.core.credentials import AzureKeyCredential
            client = ChatCompletionsClient(
                endpoint="https://models.inference.ai.azure.com",
                credential=AzureKeyCredential(token)
            )
            from azure.ai.inference.models import UserMessage
            resp = client.complete(**{"messages":[UserMessage("Hello")], "model":model, "max_tokens":1000})
            if resp.choices:
                st.success("βœ… Connected")
                st.session_state.ai_client = client
            else:
                st.error("❌ No response")
        if "ai_client" in st.session_state:
            prompt = st.text_area("Describe animation or provide code")
            if st.button("Generate Code"):
                from azure.ai.inference.models import UserMessage
                msgs = [UserMessage(f"Write a complete Manim scene:\n{prompt}")]
                resp = st.session_state.ai_client.complete(**{"messages":msgs, "model":model, "max_tokens":8000})
                if resp.choices:
                    code = resp.choices[0].message.content
                    if "```python" in code:
                        code = code.split("```python")[1].split("```")[0]
                    st.session_state.generated_code = code
                else:
                    st.error("No code returned")
            if st.session_state.generated_code:
                st.code(st.session_state.generated_code, language="python")
                if st.button("Use This Code"):
                    st.session_state.code = st.session_state.generated_code
                    st.experimental_rerun()

    # Assets tab
    with tabs[2]:
        st.markdown("### 🎨 Assets")
        imgs = st.file_uploader("Upload Images", type=["png","jpg","jpeg","svg"], accept_multiple_files=True)
        if imgs:
            adir = os.path.join(os.getcwd(),"manim_assets","images")
            os.makedirs(adir, exist_ok=True)
            for up in imgs:
                ext = up.name.split(".")[-1]
                fn = f"img_{int(time.time())}_{uuid.uuid4().hex[:6]}.{ext}"
                path = os.path.join(adir, fn)
                with open(path,"wb") as f: f.write(up.getvalue())
                st.image(path, caption=up.name, width=150)
                if st.button(f"Use {up.name}"):
                    snippet = f"""image = ImageMobject(r"{path}")
self.play(FadeIn(image))
self.wait(1)
"""
                    st.session_state.code += "\n" + snippet
                    st.experimental_rerun()
        aud = st.file_uploader("Upload Audio", type=["mp3","wav","ogg"])
        if aud:
            adir = os.path.join(os.getcwd(),"manim_assets","audio")
            os.makedirs(adir, exist_ok=True)
            fn = f"audio_{int(time.time())}.{aud.name.split('.')[-1]}"
            path = os.path.join(adir, fn)
            with open(path,"wb") as f: f.write(aud.getvalue())
            st.audio(aud)
            st.success("Audio uploaded")

    # Timeline tab
    with tabs[3]:
        st.markdown("### 🎞️ Timeline Editor")
        st.info("Adjust code manually – timeline UI coming soon.")

    # Export tab
    with tabs[4]:
        st.markdown("### πŸŽ“ Export")
        st.warning("Export features coming soon.")

    # Python Runner tab
    with tabs[5]:
        st.markdown("### 🐍 Python Runner")
        code = st.text_area("Script", height=300, key="py_code")
        inputs = []
        for i, line in enumerate(code.split("\n"), 1):
            if "input(" in line:
                prompt = re.search(r'input\(["\'](.+?)["\']\)', line)
                label = prompt.group(1) if prompt else f"Input line {i}"
                inputs.append(st.text_input(label, key=f"in_{i}"))
        timeout = st.slider("Timeout (s)", 5, 300, 30)
        if st.button("Run"):
            temp = tempfile.NamedTemporaryFile(delete=False, suffix=".py")
            temp.write(code.encode())
            temp.flush()
            proc = subprocess.Popen([sys.executable, temp.name],
                                    stdin=subprocess.PIPE,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE,
                                    text=True)
            # feed inputs
            out, err = proc.communicate(input="\n".join(inputs), timeout=timeout)
            if err:
                st.error(err)
            if out:
                st.code(out)
            temp.close()

if __name__ == "__main__":
    main()