dreamcodee commited on
Commit
876754a
·
verified ·
1 Parent(s): c0e007f
Files changed (1) hide show
  1. ppt.py +76 -0
ppt.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ from pptx import Presentation
4
+ import json
5
+ import re
6
+
7
+ # 模型加载
8
+ @st.cache_resource
9
+ def load_model():
10
+ model_name = "deepseek-ai/deepseek-coder-6.7b-instruct"
11
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
12
+ model = AutoModelForCausalLM.from_pretrained(
13
+ model_name, device_map="auto", torch_dtype="auto")
14
+ return tokenizer, model
15
+
16
+ tokenizer, model = load_model()
17
+
18
+ # 生成提示词
19
+ def generate_prompt(topic):
20
+ return f"""You are a helpful assistant. Your task is to create a PowerPoint outline based on the following topic:
21
+ Topic: {topic}
22
+ Generate a title and 5-6 slides. Each slide should include a title and 3-5 bullet points of content.
23
+ Return in structured JSON format like this:
24
+ {{\n \"title\": \"Presentation Title\",\n \"slides\": [\n {{\n \"title\": \"Slide Title\",\n \"bullets\": [\"point1\", \"point2\"]\n }}\n ]\n}}"""
25
+
26
+ # 调用模型生成
27
+ @st.cache_data(show_spinner=False)
28
+ def generate_output(prompt):
29
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
30
+ outputs = model.generate(**inputs, max_new_tokens=1024)
31
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
32
+
33
+ # 解析模型返回的 JSON 字符串
34
+ def parse_json_from_output(output):
35
+ match = re.search(r'\{.*\}', output, re.DOTALL)
36
+ if match:
37
+ try:
38
+ return json.loads(match.group(0))
39
+ except json.JSONDecodeError:
40
+ return None
41
+ return None
42
+
43
+ # 生成PPT
44
+ def create_ppt(parsed_json, filename):
45
+ prs = Presentation()
46
+ title_slide_layout = prs.slide_layouts[0]
47
+ slide = prs.slides.add_slide(title_slide_layout)
48
+ slide.shapes.title.text = parsed_json.get("title", "Untitled Presentation")
49
+ slide.placeholders[1].text = "Generated by DeepSeek"
50
+
51
+ content_layout = prs.slide_layouts[1]
52
+ for s in parsed_json.get("slides", []):
53
+ slide = prs.slides.add_slide(content_layout)
54
+ slide.shapes.title.text = s["title"]
55
+ slide.placeholders[1].text = "\n".join(s["bullets"])
56
+
57
+ prs.save(filename)
58
+
59
+ # Streamlit 页面
60
+ st.set_page_config(page_title="PPT生成器", layout="centered")
61
+ st.title("📊 基于 DeepSeek 的 PPT 内容生成器")
62
+ topic = st.text_input("请输入PPT主题:", "人工智能在教育中的应用")
63
+ if st.button("生成PPT"):
64
+ with st.spinner("正在生成内容..."):
65
+ prompt = generate_prompt(topic)
66
+ output = generate_output(prompt)
67
+ parsed = parse_json_from_output(output)
68
+
69
+ if parsed:
70
+ ppt_file = "generated_ppt.pptx"
71
+ create_ppt(parsed, ppt_file)
72
+ with open(ppt_file, "rb") as f:
73
+ st.success("✅ 生成成功!点击下方按钮下载。")
74
+ st.download_button("📥 下载PPT", f, file_name=ppt_file)
75
+ else:
76
+ st.error("❌ 无法解析模型输出,请重试或检查模型结构。")