File size: 2,943 Bytes
876754a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import AutoTokenizer, AutoModelForCausalLM
from pptx import Presentation
import json
import re

# 模型加载
@st.cache_resource
def load_model():
    model_name = "deepseek-ai/deepseek-coder-6.7b-instruct"
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = AutoModelForCausalLM.from_pretrained(
        model_name, device_map="auto", torch_dtype="auto")
    return tokenizer, model

tokenizer, model = load_model()

# 生成提示词
def generate_prompt(topic):
    return f"""You are a helpful assistant. Your task is to create a PowerPoint outline based on the following topic:
Topic: {topic}
Generate a title and 5-6 slides. Each slide should include a title and 3-5 bullet points of content.
Return in structured JSON format like this:
{{\n  \"title\": \"Presentation Title\",\n  \"slides\": [\n    {{\n      \"title\": \"Slide Title\",\n      \"bullets\": [\"point1\", \"point2\"]\n    }}\n  ]\n}}"""

# 调用模型生成
@st.cache_data(show_spinner=False)
def generate_output(prompt):
    inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
    outputs = model.generate(**inputs, max_new_tokens=1024)
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

# 解析模型返回的 JSON 字符串
def parse_json_from_output(output):
    match = re.search(r'\{.*\}', output, re.DOTALL)
    if match:
        try:
            return json.loads(match.group(0))
        except json.JSONDecodeError:
            return None
    return None

# 生成PPT
def create_ppt(parsed_json, filename):
    prs = Presentation()
    title_slide_layout = prs.slide_layouts[0]
    slide = prs.slides.add_slide(title_slide_layout)
    slide.shapes.title.text = parsed_json.get("title", "Untitled Presentation")
    slide.placeholders[1].text = "Generated by DeepSeek"

    content_layout = prs.slide_layouts[1]
    for s in parsed_json.get("slides", []):
        slide = prs.slides.add_slide(content_layout)
        slide.shapes.title.text = s["title"]
        slide.placeholders[1].text = "\n".join(s["bullets"])

    prs.save(filename)

# Streamlit 页面
st.set_page_config(page_title="PPT生成器", layout="centered")
st.title("📊 基于 DeepSeek 的 PPT 内容生成器")
topic = st.text_input("请输入PPT主题:", "人工智能在教育中的应用")
if st.button("生成PPT"):
    with st.spinner("正在生成内容..."):
        prompt = generate_prompt(topic)
        output = generate_output(prompt)
        parsed = parse_json_from_output(output)

        if parsed:
            ppt_file = "generated_ppt.pptx"
            create_ppt(parsed, ppt_file)
            with open(ppt_file, "rb") as f:
                st.success("✅ 生成成功!点击下方按钮下载。")
                st.download_button("📥 下载PPT", f, file_name=ppt_file)
        else:
            st.error("❌ 无法解析模型输出,请重试或检查模型结构。")