Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from pptx import Presentation
|
| 5 |
+
import io
|
| 6 |
+
from pptx.enum.shapes import MSO_SHAPE_TYPE
|
| 7 |
+
import pandas as pd
|
| 8 |
+
import openai
|
| 9 |
+
import json
|
| 10 |
+
import re
|
| 11 |
+
import os
|
| 12 |
+
# import streamlit as st
|
| 13 |
+
|
| 14 |
+
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def get_filename(file_obj):
|
| 18 |
+
return get_text(file_obj.name)
|
| 19 |
+
|
| 20 |
+
def get_text(ppt_file):
|
| 21 |
+
|
| 22 |
+
msg_fb = os.environ.get("PROMPT")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
result = []
|
| 26 |
+
try:
|
| 27 |
+
presentation = Presentation(ppt_file)
|
| 28 |
+
|
| 29 |
+
for slide_idx, slide in enumerate(presentation.slides): # ์ฌ๋ผ์ด๋ ๋ง๋ค ์ฝ๊ธฐ
|
| 30 |
+
string = ''
|
| 31 |
+
|
| 32 |
+
# get all text in presentation
|
| 33 |
+
# It does not distinguish between the title and the content
|
| 34 |
+
for shape in slide.shapes: # ํ๋์ ์ฌ๋ผ์ด๋์ ๋ค์ด๊ฐ text ์ฐพ๊ธฐ
|
| 35 |
+
if hasattr(shape, "text"):
|
| 36 |
+
string += shape.text + "\n"
|
| 37 |
+
|
| 38 |
+
# distinguish title and content
|
| 39 |
+
'''
|
| 40 |
+
# title
|
| 41 |
+
title = slide.shapes.title
|
| 42 |
+
if title is None:
|
| 43 |
+
string += 'title is None'
|
| 44 |
+
else:
|
| 45 |
+
string += title.text
|
| 46 |
+
string += '\n'
|
| 47 |
+
|
| 48 |
+
# content
|
| 49 |
+
for idx, content in enumerate(slide.shapes.placeholders):
|
| 50 |
+
# content = slide.shapes.placeholders[1]
|
| 51 |
+
if idx == 0:
|
| 52 |
+
continue
|
| 53 |
+
if content is None:
|
| 54 |
+
string += 'content is None'
|
| 55 |
+
else:
|
| 56 |
+
string += content.text
|
| 57 |
+
'''
|
| 58 |
+
|
| 59 |
+
# table
|
| 60 |
+
for shape in slide.shapes:
|
| 61 |
+
if shape.shape_type == MSO_SHAPE_TYPE.TABLE:
|
| 62 |
+
table_data = []
|
| 63 |
+
|
| 64 |
+
row_count = len(shape.table.rows)
|
| 65 |
+
col_count = len(shape.table.columns)
|
| 66 |
+
for _r in range(0, row_count):
|
| 67 |
+
row = []
|
| 68 |
+
for _c in range(0, col_count):
|
| 69 |
+
cell = shape.table.cell(_r, _c)
|
| 70 |
+
# row ๋ณ ๋ฐ์ดํฐ๋ฅผ array๋ก ์ ์ฅ
|
| 71 |
+
row.append(cell.text)
|
| 72 |
+
# row ๋ฐ์ดํฐ๋ฅผ ์ ์ฒด ๋ฐ์ดํฐ ์ ์ฅ array์ ์ ์ฅ
|
| 73 |
+
table_data.append(row)
|
| 74 |
+
|
| 75 |
+
# ํ์์ ๋ฐ๋ผ์๋ pandas์ dataframe ๋ฑ์ ์ด์ฉํด์ ๋ฐ์ดํฐ ์ ์ฅ
|
| 76 |
+
df_temp = pd.DataFrame(columns=table_data[0], data=table_data[1:])
|
| 77 |
+
string += str(table_data)
|
| 78 |
+
|
| 79 |
+
# add delimiter
|
| 80 |
+
string += '\n---------------\n'
|
| 81 |
+
result.append(string)
|
| 82 |
+
|
| 83 |
+
except Exception as e:
|
| 84 |
+
result = f"Error: {str(e)}"
|
| 85 |
+
|
| 86 |
+
# make script based on slide title & contents
|
| 87 |
+
msg_fb = msg_fb + "- Input:\n" + ' '.join(result)
|
| 88 |
+
|
| 89 |
+
response = openai.ChatCompletion.create(
|
| 90 |
+
model="gpt-3.5-turbo",
|
| 91 |
+
messages=[
|
| 92 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 93 |
+
{"role": "user", "content": msg_fb},
|
| 94 |
+
]
|
| 95 |
+
)
|
| 96 |
+
response = response.choices[0].message.content
|
| 97 |
+
|
| 98 |
+
done_presentation = preprocessing_script(response, presentation)
|
| 99 |
+
|
| 100 |
+
# save pptx file
|
| 101 |
+
done_presentation.save('edited_ppt_file.pptx')
|
| 102 |
+
|
| 103 |
+
# return ' '.join(result), 'edited_ppt_file.pptx'
|
| 104 |
+
return response, 'edited_ppt_file.pptx'
|
| 105 |
+
|
| 106 |
+
def preprocessing_script(response, presentation):
|
| 107 |
+
# split chatgpt's response
|
| 108 |
+
pattern = r"- Slide (\d+) Script:"
|
| 109 |
+
segments = re.split(pattern, response)
|
| 110 |
+
|
| 111 |
+
# print(response)
|
| 112 |
+
|
| 113 |
+
for i in range(1, len(segments), 2): # ๋ช๋ฒ์งธ ์ฌ๋ผ์ด๋ ์ธ์ง๋ ๋์์ 2์นธ์ฉ ์ ํ
|
| 114 |
+
slide_num = int(segments[i])
|
| 115 |
+
script = segments[i+1].strip()
|
| 116 |
+
|
| 117 |
+
print(script)
|
| 118 |
+
|
| 119 |
+
slide = presentation.slides[slide_num - 1]
|
| 120 |
+
|
| 121 |
+
# add sclide note each of them
|
| 122 |
+
notes_slide = slide.notes_slide
|
| 123 |
+
text_frame = notes_slide.notes_text_frame
|
| 124 |
+
text_frame.text = script
|
| 125 |
+
|
| 126 |
+
return presentation
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
# with gr.Blocks() as demo:
|
| 130 |
+
# upload_button = gr.UploadButton("Click to Upload a File", file_types=[".pptx"], file_count="multiple")
|
| 131 |
+
# upload_button.upload(get_filename, upload_button, "text")
|
| 132 |
+
|
| 133 |
+
# output = gr.Textbox(label='output_box')
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
demo = gr.Interface(get_filename, "file", outputs=["text", "file"])
|
| 137 |
+
|
| 138 |
+
demo.launch()
|
| 139 |
+
|