import os import PyPDF2 from pptx import Presentation import openai import subprocess from io import BytesIO import sys sys.path.append("/home/user/app") # text2ppt 프롬프트 생성 함수 def generate_text2ppt_input_prompt(input_type, input_value, input_pages): header = """ 너가 markdown 문법으로 PPT를 제작하는 디자이너라고 가정하고, %s장의 PPT를 작성해. +++ 아래 내용 또는 링크를 요약해서 markdown 언어로 작성하는데, === 아래 규칙과 지키고, ~~~ 아래 슬라이드 예시를 참고해. +++ """ % input_pages summary_value = "" if input_type == "링크": summary_value += input_value summary_value += "\n" elif input_type == "텍스트": summary_value += input_value summary_value += "\n" elif input_type == "PDF": with open(input_value, 'rb') as pdf_file: pdf_reader = PyPDF2.PdfReader(pdf_file) num_pages = len(pdf_reader.pages) # 각 페이지의 내용을 문자열로 변환합니다. text = "" for page_num in range(num_pages): page = pdf_reader.pages[page_num] text += page.extract_text() summary_value += text summary_value += "\n" else: print("ERROR: 잘못된 입력") rule_value = """ === - 제시한 내용 또는 링크의 내용에 대해서만 사실적으로 작성해줘. - 슬라이드 구분자로 ---를 무조건 사용해줘. - 주제에 적절한 도형, 이미지(![이미지](이미지링크), https://unsplash.com/ko/images/stock/non-copyrighted 에서 실제로 사용 가능한), 표(|-|), 인용(>), 강조(bold, ``), 이모지(https://kr.piliapp.com/twitter-symbols/), 아이콘 (https://kr.piliapp.com/symbol/#popular) 등이 다양하게 슬라이드를 디자인하고 배치해줘. - 이모지는 최대 2 페이지에 한 번만 사용하고, 다른 디자인을 다양하게 사용해줘. - 이미지와 표를 사용할 때, 페이지 크기와 고려해서 글 내용이 모두 나타나도록 크기를 지정해줘. - Slide 1를 제목으로 해서 총 %s장이야. - PPT의 내용을 풍부하게 마크다운으로 작성해줘. - 슬라이드 별로 설명하지말고, 코드만 작성해줘. - 예시의 내용을 사용해서 작성하지말고, 형식만 참고해. ~~~ # 슬라이드 제목 ![이미지링크](https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo-with-title.png) - ChatGPT를 활용한 🤗**TEXT2PPT 서비스 PA!**입니다. - `링크`,`텍스트`, `PDF`를 입력 또는 업로드하면, PPT로 변환합니다. """ % input_pages return header + summary_value + rule_value # text2ppt 실행 함수 def text2ppt(token_key, input_prompt, input_theme): openai.api_key = token_key messages = [ {"role": "system", "content": "You are a kind helpful PPT designer."}, ] message = input_prompt if message: messages.append( {"role": "user", "content": message}, ) chat = openai.ChatCompletion.create( model="gpt-3.5-turbo-0301", messages=messages ) reply = chat.choices[0].message.content messages.append({"role": "assistant", "content": reply}) md_text = reply[4:] if reply[:3] == "---" else reply md_text_list = md_text.split('\n') f = open("text2ppt_input.md", 'w') for i in range(0, len(md_text_list)): data = md_text_list[i] + "\n" f.write(data) f.close() if input_theme == 'default': subprocess.run(["/home/user/app/pandoc-2.14.2/bin/pandoc", "/home/user/app/text2ppt_input.md", "-t", "pptx", "-o", "/home/user/app/text2ppt_output.pptx"], capture_output=True) else: ppt_theme = "--reference-doc=/home/user/app/template/"+input_theme+".pptx" subprocess.run(["/home/user/app/pandoc-2.14.2/bin/pandoc", "/home/user/app/text2ppt_input.md", "-t", "pptx", ppt_theme, "-o", "/home/user/app/text2ppt_output.pptx"], capture_output=True) def ppt2script(token_key, input_file, input_type): openai.api_key = token_key if input_type=="PDF": with open(input_file, 'rb') as pdf_file: pdf_reader = PyPDF2.PdfReader(pdf_file) num_pages = len(pdf_reader.pages) # 각 페이지의 내용을 문자열로 변환합니다. text = "" for page_num in range(num_pages): page = pdf_reader.pages[page_num] text += "[PAGE_NUM " + str(page_num + 1) + "]" text += page.extract_text() else: prs = Presentation(input_file) text = "" page_num = 0 for slide in prs.slides: text += "[PAGE_NUM " + str(page_num + 1) + "]" page_num += 1 for shape in slide.shapes: if not shape.has_text_frame: continue for paragraph in shape.text_frame.paragraphs: for run in paragraph.runs: text += run.text header = """ 너는는 PPT 발표에 도움을 주는 조력자야. ~~~아래 규칙을 지키고, --- 아래 PPT 내용에 대해 발표 스크립트를 한글로 작성해. ~~~ - [PAGE_NUM 1] 일 때, 여기서 1은 페이지 번호인데, 페이지 번호마다 발표 스크립트를 작성해. - 마크다운 언어를 쓰지않고, 텍스트로만 작성해. - PPT 내용에 추가적인 설명이나 사례를 덧붙여줘. --- """ input_prompt = header + text messages = [ {"role": "system", "content": "You are a kind helpful PPT Assistant."}, ] message = input_prompt if message: messages.append( {"role": "user", "content": message}, ) chat = openai.ChatCompletion.create( model="gpt-3.5-turbo-0301", messages=messages ) reply = chat.choices[0].message.content messages.append({"role": "assistant", "content": reply}) return reply