-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_ds.py
More file actions
73 lines (62 loc) · 2.13 KB
/
Copy pathmain_ds.py
File metadata and controls
73 lines (62 loc) · 2.13 KB
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
import os
import argparse
from dotenv import load_dotenv
from openai import OpenAI
# 强制加载.env(解决加载失败问题)
load_dotenv(override=True)
# 直接从环境变量获取,万无一失
api_key = os.getenv("OPENAI_API_KEY")
base_url = os.getenv("OPENAI_BASE_URL")
print("🔑 读取到 API_KEY:", api_key[:10] + "..." if api_key else "缺失")
print("🔗 读取到 BASE_URL:", base_url)
# 初始化 LLM 客户端
client = OpenAI(
api_key=api_key,
base_url=base_url
)
def generate_math_animation(problem: str):
print("\n✅ 开始处理题目:", problem)
sys_prompt = """
你是专业数学老师 + Manim 动画工程师。
输出格式:
1. 题目分析
2. 分步解题
3. 可直接运行的 Manim 0.20.1 代码(```python开头)
代码必须完整、可渲染、无报错。
"""
resp = client.chat.completions.create(
model="deepseek-chat",
messages=[
{"role": "system", "content": sys_prompt},
{"role": "user", "content": problem}
],
temperature=0.3
)
content = resp.choices[0].message.content
print("\n📝 生成结果:")
print(content)
# 提取并保存 Manim 代码
code_start = content.find("```python")
code_end = content.find("```", code_start + 10)
if code_start != -1 and code_end != -1:
manim_code = content[code_start+9:code_end].strip()
os.makedirs("temp", exist_ok=True)
with open("temp/math_scene.py", "w", encoding="utf-8") as f:
f.write(manim_code)
print("\n💾 Manim 代码已保存")
# 渲染视频
print("\n🎬 开始渲染视频...")
import subprocess
subprocess.run([
"manim", "-ql",
"temp/math_scene.py",
"-o", "math_video.mp4",
"--output_dir", "output/videos"
])
print("\n✅ 视频完成!output/videos/math_video.mp4")
if __name__ == "__main__":
print("Hello from mathlens!")
parser = argparse.ArgumentParser()
parser.add_argument("problem", type=str, help="输入数学题目")
args = parser.parse_args()
generate_math_animation(args.problem)