kshitij10000 commited on
Commit
6050e1c
Β·
1 Parent(s): 0da69c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +157 -99
app.py CHANGED
@@ -1,112 +1,170 @@
1
  import openai
2
  import json
3
- import base64
4
  from pptx import Presentation
5
  import streamlit as st
 
6
 
7
  # Suppress all warnings
8
  import warnings
9
  warnings.filterwarnings("ignore")
10
 
11
  # Set your OpenAI API key
12
- openai.api_key = "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"
13
-
14
- # Streamlit app title and header
15
- st.title("PowerPoint Presentation Generator")
16
- st.header("Welcome to the Future of Presentation Creation!")
17
-
18
- # Introduction section
19
- st.write("Creating presentations has never been this easy and exciting! With our AI-powered model, you can quickly generate presentation content, while the design remains your creative canvas. 🎨")
20
- st.write("Here's what we do:")
21
- st.write("- **You provide the presentation topic**, and we add captivating content.")
22
- st.write("- Our AI ensures **each slide has the perfect balance of information**.")
23
- st.write("- Stay tuned for **exciting future features** that will enhance your presentation experience. πŸš€")
24
-
25
- # Presentation Limits section
26
- st.header("Presentation Limits")
27
- st.write("✨ You can create a minimum of 2 and a maximum of 8 slides per presentation.")
28
- st.write("✨ Each slide should have between 2 and 7 points of content.")
29
- st.write("✨ Minimum 2 points, maximum 7 points. ⚠️")
30
-
31
- # Important Notice
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  st.warning("⚠️ Important Notice: Occasional download issues? Try re-downloading or reach out to us for assistance. We apologize for any inconvenience.")
33
 
34
- # Contact and Feedback section
35
- st.header("Contact and Feedback")
36
- st.write("Have questions, ideas, or cool suggestions for future enhancements? We're all ears!")
37
- st.write("Your feedback is invaluable in making your experience the best it can be! 🌟")
38
-
39
- # Input for presentation topic
40
- presentation_title = st.text_input("Enter your presentation topic:")
41
- no_of_pages = st.number_input("Number of slides you want", min_value=2, max_value=8, value=2)
42
- least_c = st.number_input("Minimum points in each slide", min_value=2, max_value=7, value=2)
43
- max_c = st.number_input("Maximum points in each slide", min_value=2, max_value=7, value=7)
44
-
45
- if st.button("Generate Presentation"):
46
- # Check if the user has entered a topic
47
- if presentation_title:
48
- question = (
49
- f"generate a {no_of_pages} slide presentation for the topic {presentation_title}."
50
- f" Each slide should have {{header}}, {{content}}, should have at least {least_c} points and max {max_c} points in content. Return as JSON"
51
- )
52
-
53
- query_json = {
54
- "input_text": question,
55
- "output_format": "json",
56
- "json_structure": {"slides": "{{presentation_slides}}"},
57
- }
58
-
59
- # Send the query to OpenAI
60
- completion = openai.ChatCompletion.create(
61
- model="gpt-3.5-turbo",
62
- messages=[{"role": "user", "content": json.dumps(query_json)}],
63
- )
64
-
65
- try:
66
- response = json.loads(completion.choices[0].message.content)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
- slide_data = response["slides"]
69
-
70
- prs = Presentation()
71
-
72
- from pptx.util import Pt # Import the Pt class from pptx.util
73
- from pptx.enum.text import PP_ALIGN # Import text alignment options
74
-
75
- # Iterate through slide data and populate the presentation
76
- for slide in slide_data:
77
- slide_layout = prs.slide_layouts[1]
78
- new_slide = prs.slides.add_slide(slide_layout)
79
-
80
- if slide["header"]:
81
- title = new_slide.shapes.title
82
- title.text = slide["header"]
83
-
84
- if slide["content"]:
85
- shapes = new_slide.shapes
86
- body_shape = shapes.placeholders[1]
87
- tf = body_shape.text_frame
88
-
89
- # Join the list of content into a single string with line breaks
90
- content_text = "\n".join(slide["content"])
91
-
92
- p = tf.add_paragraph()
93
- p.text = content_text
94
-
95
- # Set the spacing between lines using Pt (point size)
96
- p.space_after = Pt(14) # Adjust the spacing between lines
97
-
98
- # Set text direction to horizontal (left-to-right)
99
- p.alignment = PP_ALIGN.LEFT
100
-
101
- # Save the presentation in PPTX format with the topic name as the file name
102
- presentation_filename = f"{presentation_title}.pptx"
103
- prs.save(presentation_filename)
104
-
105
- # Provide a styled download button
106
- with open(presentation_filename, "rb") as file:
107
- download_button = f'<a href="data:application/octet-stream;base64,{base64.b64encode(file.read()).decode()}" download="{presentation_filename}" class="st-eb stButton stButton-primary">Download {presentation_title}</a>'
108
- st.markdown(download_button, unsafe_allow_html=True)
109
- except json.JSONDecodeError as e:
110
- st.error("Error parsing JSON response from OpenAI.")
111
- else:
112
- st.warning("Please enter a presentation topic.")
 
 
 
 
 
1
  import openai
2
  import json
 
3
  from pptx import Presentation
4
  import streamlit as st
5
+ import os
6
 
7
  # Suppress all warnings
8
  import warnings
9
  warnings.filterwarnings("ignore")
10
 
11
  # Set your OpenAI API key
12
+ openai.api_key = "YOUR API"
13
+
14
+ # CSS styling for improved visuals
15
+ st.markdown(
16
+ """
17
+ <style>
18
+ .st-eb {
19
+ padding: 0.5rem;
20
+ background-color: #b1ddf1;
21
+ border-radius: 5px;
22
+ }
23
+ .st-br {
24
+ margin-top: 1rem;
25
+ margin-bottom: 1rem;
26
+ }
27
+ </style>
28
+ """,
29
+ unsafe_allow_html=True,
30
+ )
31
+
32
+ # Streamlit app title and header with improved styling
33
+ st.title("🌟 PowerPoint Presentation Generator 🌟")
34
+ st.markdown("<h1 style='text-align: center; color: white;'>Welcome to the Future of Presentation Creation!</h1>", unsafe_allow_html=True)
35
+
36
+ # Introduction section with improved styling and details
37
+ st.markdown(
38
+ """
39
+ <div class="st-eb">
40
+ <p style="color: black;">Creating presentations has never been this easy and exciting! With our AI-powered model, you can quickly generate presentation content, while the design remains your creative canvas. 🎨</p>
41
+ <p style="color: black;">Here's what we do:</p>
42
+ <ul>
43
+ <li style="color: black;"><strong>You provide the presentation topic</strong>, and we add captivating content.</li>
44
+ <li style="color: black;">Our AI ensures <strong>each slide has the perfect balance of information</strong>.</li>
45
+ <li style="color: black;">Stay tuned for <strong>exciting future features</strong> that will enhance your presentation experience. πŸš€</li>
46
+ </ul>
47
+ </div>
48
+ """,
49
+ unsafe_allow_html=True,
50
+ )
51
+
52
+ # Presentation Limits section with improved styling and details
53
+ st.markdown("<h2>Presentation Limits</h2>", unsafe_allow_html=True)
54
+ st.markdown(
55
+ """
56
+ <div class="st-eb">
57
+ <p style="color: black;">✨ You can create a minimum of 3 and a maximum of 7 slides per presentation.</p>
58
+ <p style="color: black;">✨ Each slide should have between 3 and 7 points of content.</p>
59
+ <p style="color: black;">✨ Minimum 3 points, maximum 7 points. ⚠️</p>
60
+ </div>
61
+ """,
62
+ unsafe_allow_html=True,
63
+ )
64
+
65
+ # Important Notice with improved styling
66
  st.warning("⚠️ Important Notice: Occasional download issues? Try re-downloading or reach out to us for assistance. We apologize for any inconvenience.")
67
 
68
+ # Contact and Feedback section with improved styling
69
+ st.markdown("<h2>Contact and Feedback</h2>", unsafe_allow_html=True)
70
+ st.markdown(
71
+ """
72
+ <div class="st-eb">
73
+ <p style="color: black;">Have questions, ideas, or cool suggestions for future enhancements? We're all ears!</p>
74
+ <p style="color: black;">Your feedback is invaluable in making your experience the best it can be! 🌟</p>
75
+ </div>
76
+ """,
77
+ unsafe_allow_html=True,
78
+ )
79
+
80
+ # User Input Section with clickable options
81
+ presentation_title = st.text_input("πŸ“ Enter your presentation topic:")
82
+
83
+ # Predefined topics
84
+ predefined_topics = ["Artificial Intelligence", "Blockchain Technology", "Space Exploration", "Climate Change", "Future of Work"]
85
+
86
+ # Display clickable options for predefined topics
87
+ selected_topic = st.radio("🌐 Choose a predefined topic or enter your own:", ["Select"] + predefined_topics)
88
+
89
+ # If a predefined topic is selected, set it as the input
90
+ if selected_topic != "Select":
91
+ presentation_title = selected_topic
92
+
93
+ custom_requirements = st.text_input("πŸ“ Enter your custom requirements you want:", value="informative")
94
+ no_of_pages = st.number_input("πŸ“Š Number of slides you want (3-8)", min_value=3, max_value=8, value=3)
95
+ least_c = st.number_input("πŸ“Š Minimum points in each slide (3-7)", min_value=3, max_value=7, value=3)
96
+ max_c = st.number_input("πŸ“Š Maximum points in each slide (4-7)", min_value=4, max_value=7, value=7)
97
+
98
+ # Check if any input values are out of range
99
+ if no_of_pages > 8 or no_of_pages < 3 or least_c > 7 or least_c < 3 or max_c > 7 or max_c < 4:
100
+ st.warning("Invalid input values. Please ensure that your inputs are within the specified limits.")
101
+ else:
102
+ if st.button("Generate Presentation"):
103
+ # Check if the user has entered a topic
104
+ if presentation_title:
105
+ question = (
106
+ f"Create a {no_of_pages}-slide PowerPoint presentation on the topic of {presentation_title}."
107
+ f" Each slide should have {{header}}, {{content}}, should have at least {least_c} points and max {max_c} points in content, be {custom_requirements}, and make content informative. Return as JSON."
108
+ )
109
+
110
+ query_json = {
111
+ "input_text": question,
112
+ "output_format": "json",
113
+ "json_structure": {"slides": "{{presentation_slides}}"},
114
+ }
115
+
116
+ # Send the query to OpenAI
117
+ completion = openai.ChatCompletion.create(
118
+ model="gpt-3.5-turbo",
119
+ messages=[{"role": "user", "content": json.dumps(query_json)}],
120
+ )
121
 
122
+ try:
123
+ response = json.loads(completion.choices[0].message.content)
124
+
125
+ if "slides" in response:
126
+ slide_data = response["slides"]
127
+ prs = Presentation()
128
+
129
+ from pptx.util import Pt
130
+ from pptx.enum.text import PP_ALIGN
131
+
132
+ for slide in slide_data:
133
+ slide_layout = prs.slide_layouts[1]
134
+ new_slide = prs.slides.add_slide(slide_layout)
135
+
136
+ if slide["header"]:
137
+ title = new_slide.shapes.title
138
+ title.text = slide["header"]
139
+
140
+ if slide["content"]:
141
+ shapes = new_slide.shapes
142
+ body_shape = shapes.placeholders[1]
143
+ tf = body_shape.text_frame
144
+
145
+ content_text = "\n".join(slide["content"])
146
+
147
+ p = tf.add_paragraph()
148
+ p.text = content_text
149
+ p.space_after = Pt(14)
150
+ p.alignment = PP_ALIGN.LEFT
151
+
152
+ presentation_filename = f"{presentation_title}.pptx"
153
+ prs.save(presentation_filename)
154
+
155
+ with open(presentation_filename, "rb") as file:
156
+ st.download_button(
157
+ label=f"πŸ“₯ Download {presentation_title}",
158
+ data=file.read(),
159
+ key=presentation_filename,
160
+ file_name=presentation_filename
161
+ )
162
+
163
+ os.remove(presentation_filename)
164
+ else:
165
+ st.warning("No slides were generated for this presentation.")
166
+
167
+ except json.JSONDecodeError as e:
168
+ st.error("Error parsing JSON response from OpenAI.")
169
+ else:
170
+ st.warning("Please enter a presentation topic.")