Commit ·
2f20cef
1
Parent(s): d74a592
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 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
|
|
@@ -11,153 +11,85 @@ warnings.filterwarnings("ignore")
|
|
| 11 |
# Set your OpenAI API key
|
| 12 |
openai.api_key = "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"
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
st.
|
| 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 |
-
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 |
-
# Input for presentation topic with improved styling
|
| 81 |
-
presentation_title = st.text_input("📝 Enter your presentation topic:")
|
| 82 |
-
no_of_pages = st.number_input("📊 Number of slides you want (3-8)", min_value=3, max_value=8, value=3)
|
| 83 |
-
least_c = st.number_input("📊 Minimum points in each slide (3-7)", min_value=3, max_value=7, value=3)
|
| 84 |
-
max_c = st.number_input("📊 Maximum points in each slide (4-7)", min_value=4, max_value=7, value=7)
|
| 85 |
-
|
| 86 |
-
# Check if any input values are out of range
|
| 87 |
-
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:
|
| 88 |
-
st.warning("Invalid input values. Please ensure that your inputs are within the specified limits.")
|
| 89 |
-
else:
|
| 90 |
-
if st.button("Generate Presentation"):
|
| 91 |
-
# Check if the user has entered a topic
|
| 92 |
-
if presentation_title:
|
| 93 |
-
question = (
|
| 94 |
-
f"generate a {no_of_pages} slide presentation for the topic {presentation_title}."
|
| 95 |
-
f" Each slide should have {{header}}, {{content}}, should have at least {least_c} points and max {max_c} points in content. Return as JSON"
|
| 96 |
-
)
|
| 97 |
-
|
| 98 |
-
query_json = {
|
| 99 |
-
"input_text": question,
|
| 100 |
-
"output_format": "json",
|
| 101 |
-
"json_structure": {"slides": "{{presentation_slides}}"},
|
| 102 |
-
}
|
| 103 |
-
|
| 104 |
-
# Send the query to OpenAI
|
| 105 |
-
completion = openai.ChatCompletion.create(
|
| 106 |
-
model="gpt-3.5-turbo",
|
| 107 |
-
messages=[{"role": "user", "content": json.dumps(query_json)}],
|
| 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 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
else:
|
| 158 |
-
st.error("Error: Presentation file not found.")
|
| 159 |
-
|
| 160 |
-
except json.JSONDecodeError as e:
|
| 161 |
-
st.error("Error parsing JSON response from OpenAI.")
|
| 162 |
-
else:
|
| 163 |
-
st.warning("Please enter a presentation topic.")
|
|
|
|
| 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
|
|
|
|
| 11 |
# Set your OpenAI API key
|
| 12 |
openai.api_key = "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"
|
| 13 |
|
| 14 |
+
# Streamlit app
|
| 15 |
+
st.title("PowerPoint Presentation Generator")
|
| 16 |
+
|
| 17 |
+
# Display a warning message
|
| 18 |
+
st.warning("Warning: If multiple times the file is downloaded, it may return the PowerPoint content in vertical format.")
|
| 19 |
+
|
| 20 |
+
# Input for presentation topic
|
| 21 |
+
presentation_title = st.text_input("Enter your presentation topic:")
|
| 22 |
+
no_of_pages = st.number_input("Number of slides you want", min_value=1, value=1) # Accept and display float values
|
| 23 |
+
least_c = st.number_input("Minimum points in each slide", min_value=1, value=1) # Accept and display float values
|
| 24 |
+
max_c = st.number_input("Maximum points in each slide", min_value=1, value=1) # Accept and display float values
|
| 25 |
+
|
| 26 |
+
if st.button("Generate Presentation"):
|
| 27 |
+
# Check if the user has entered a topic
|
| 28 |
+
if presentation_title:
|
| 29 |
+
question = (
|
| 30 |
+
f"generate a {no_of_pages} slide presentation for the topic {presentation_title}."
|
| 31 |
+
f" Each slide should have {{header}}, {{content}}, should have at least {least_c} points and max {max_c} points in content. Return as JSON"
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
query_json = {
|
| 35 |
+
"input_text": question,
|
| 36 |
+
"output_format": "json",
|
| 37 |
+
"json_structure": {"slides": "{{presentation_slides}}"},
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
# Send the query to OpenAI
|
| 41 |
+
completion = openai.ChatCompletion.create(
|
| 42 |
+
model="gpt-3.5-turbo",
|
| 43 |
+
messages=[{"role": "user", "content": json.dumps(query_json)}],
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
response = json.loads(completion.choices[0].message.content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
slide_data = response["slides"]
|
| 50 |
+
|
| 51 |
+
prs = Presentation()
|
| 52 |
+
|
| 53 |
+
from pptx.util import Pt # Import the Pt class from pptx.util
|
| 54 |
+
from pptx.enum.text import PP_ALIGN # Import text alignment options
|
| 55 |
+
|
| 56 |
+
# Iterate through slide data and populate the presentation
|
| 57 |
+
for slide in slide_data:
|
| 58 |
+
slide_layout = prs.slide_layouts[1]
|
| 59 |
+
new_slide = prs.slides.add_slide(slide_layout)
|
| 60 |
+
|
| 61 |
+
if slide["header"]:
|
| 62 |
+
title = new_slide.shapes.title
|
| 63 |
+
title.text = slide["header"]
|
| 64 |
+
|
| 65 |
+
if slide["content"]:
|
| 66 |
+
shapes = new_slide.shapes
|
| 67 |
+
body_shape = shapes.placeholders[1]
|
| 68 |
+
tf = body_shape.text_frame
|
| 69 |
+
|
| 70 |
+
# Join the list of content into a single string with line breaks
|
| 71 |
+
content_text = "\n".join(slide["content"])
|
| 72 |
+
|
| 73 |
+
p = tf.add_paragraph()
|
| 74 |
+
p.text = content_text
|
| 75 |
+
|
| 76 |
+
# Set the spacing between lines using Pt (point size)
|
| 77 |
+
p.space_after = Pt(14) # Adjust the spacing between lines
|
| 78 |
+
|
| 79 |
+
# Set text direction to horizontal (left-to-right)
|
| 80 |
+
p.alignment = PP_ALIGN.LEFT
|
| 81 |
+
|
| 82 |
+
# Save the presentation in PPTX format with the topic name as the file name
|
| 83 |
+
presentation_filename = f"{presentation_title}.pptx"
|
| 84 |
+
prs.save(presentation_filename)
|
| 85 |
+
|
| 86 |
+
# Provide a direct download link
|
| 87 |
+
with open(presentation_filename, "rb") as file:
|
| 88 |
+
st.markdown(
|
| 89 |
+
f"### [Download {presentation_title}]"
|
| 90 |
+
f"(data:application/octet-stream;base64,{base64.b64encode(file.read()).decode()})"
|
| 91 |
+
)
|
| 92 |
+
except json.JSONDecodeError as e:
|
| 93 |
+
st.error("Error parsing JSON response from OpenAI.")
|
| 94 |
+
else:
|
| 95 |
+
st.warning("Please enter a presentation topic.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|