原标题: 要将ChatGPT接入到ppt中,您可以使用以下代码:
导读:
```pythonfrom pptx import Presentationfrom transformers import AutoModelForCausalLM, Aut...
```python
from pptx import Presentation
from transformers import AutoModelForCausalLM, AutoTokenizer
# 加载ChatGPT模型和tokenizer
model_name = "microsoft/DialoGPT-medium"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# 读取PPT文件
presentation = Presentation("your_ppt_file.pptx")
for slide in presentation.slides:
for shape in slide.shapes:
if shape.has_text_frame:
text_frame = shape.text_frame
# 提取文本内容
text_content = ""
for paragraph in text_frame.paragraphs:
for run in paragraph.runs:
text_content += run.text
# 使用ChatGPT生成回复
chat_input_ids = tokenizer.encode(text_content + tokenizer.eos_token, return_tensors="pt")
chat_output_ids = model.generate(chat_input_ids, max_length=1000)
# 将生成的回复添加到文本框中