Spaces:
Runtime error
Runtime error
File size: 3,913 Bytes
6862bce db1b9d2 cb567b6 db1b9d2 6862bce 3c1fb44 6862bce 3c1fb44 6862bce 3c1fb44 6862bce 890b240 6862bce 3c1fb44 |
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# -*- coding: utf-8 -*-
import gradio as gr
from pptx import Presentation
import io
from pptx.enum.shapes import MSO_SHAPE_TYPE
import pandas as pd
import openai
import json
import re
import os
openai.api_key = os.environ.get("OPENAI_API_KEY")
def get_filename(file_obj):
return get_text(file_obj.name)
def get_text(ppt_file):
msg_fb = os.environ.get("PROMPT")
result = []
presentation = Presentation(ppt_file)
if len(presentation.slides) > 5:
raise Exception("Max slide length is 5. Please follow the guideline.")
try:
for slide_idx, slide in enumerate(presentation.slides): # ์ฌ๋ผ์ด๋ ๋ง๋ค ์ฝ๊ธฐ
string = ''
# get all text in presentation
# It does not distinguish between the title and the content
for shape in slide.shapes: # ํ๋์ ์ฌ๋ผ์ด๋์ ๋ค์ด๊ฐ text ์ฐพ๊ธฐ
if hasattr(shape, "text"):
string += shape.text + "\n"
# distinguish title and content
'''
# title
title = slide.shapes.title
if title is None:
string += 'title is None'
else:
string += title.text
string += '\n'
# content
for idx, content in enumerate(slide.shapes.placeholders):
# content = slide.shapes.placeholders[1]
if idx == 0:
continue
if content is None:
string += 'content is None'
else:
string += content.text
'''
# table
for shape in slide.shapes:
if shape.shape_type == MSO_SHAPE_TYPE.TABLE:
table_data = []
row_count = len(shape.table.rows)
col_count = len(shape.table.columns)
for _r in range(0, row_count):
row = []
for _c in range(0, col_count):
cell = shape.table.cell(_r, _c)
# row ๋ณ ๋ฐ์ดํฐ๋ฅผ array๋ก ์ ์ฅ
row.append(cell.text)
# row ๋ฐ์ดํฐ๋ฅผ ์ ์ฒด ๋ฐ์ดํฐ ์ ์ฅ array์ ์ ์ฅ
table_data.append(row)
# ํ์์ ๋ฐ๋ผ์๋ pandas์ dataframe ๋ฑ์ ์ด์ฉํด์ ๋ฐ์ดํฐ ์ ์ฅ
df_temp = pd.DataFrame(columns=table_data[0], data=table_data[1:])
string += str(table_data)
# add delimiter
string += '\n---------------\n'
result.append(string)
except Exception as e:
result = f"Error: {str(e)}"
# make script based on slide title & contents
msg_fb = msg_fb + "- Input:\n" + ' '.join(result)
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": msg_fb},
],
temperature=0.3,
frequency_penalty=0,
presence_penalty=0,
top_p=0.2
)
response = response.choices[0].message.content
done_presentation = preprocessing_script(response, presentation)
# save pptx file
done_presentation.save('edited_ppt_file.pptx')
# return ' '.join(result), 'edited_ppt_file.pptx'
return response, 'edited_ppt_file.pptx'
def preprocessing_script(response, presentation):
# split chatgpt's response
pattern = r"- Slide (\d+) Scripts:"
segments = re.split(pattern, response)
# print(response)
for i in range(1, len(segments), 2): # ๋ช๋ฒ์งธ ์ฌ๋ผ์ด๋ ์ธ์ง๋ ๋์์ 2์นธ์ฉ ์ ํ
slide_num = int(segments[i])
script = segments[i+1].strip()
# print(script)
slide = presentation.slides[slide_num - 1]
# add sclide note each of them
notes_slide = slide.notes_slide
text_frame = notes_slide.notes_text_frame
text_frame.text = script
return presentation
# with gr.Blocks() as demo:
# upload_button = gr.UploadButton("Click to Upload a File", file_types=[".pptx"], file_count="multiple")
# upload_button.upload(get_filename, upload_button, "text")
# output = gr.Textbox(label='output_box')
demo = gr.Interface(get_filename, "file", outputs=["text", "file"], examples=[["test_final.pptx"]])
demo.launch() |