cheeeeese commited on
Commit
6862bce
ยท
1 Parent(s): 854e535

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +139 -0
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
+