Commit ·
4aeda2f
1
Parent(s): d7e28c8
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import json
|
| 3 |
+
import base64
|
| 4 |
+
from pptx import Presentation
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import os # Import the os module to handle file renaming
|
| 7 |
+
|
| 8 |
+
# Suppress all warnings
|
| 9 |
+
import warnings
|
| 10 |
+
warnings.filterwarnings("ignore")
|
| 11 |
+
|
| 12 |
+
# Set your OpenAI API key
|
| 13 |
+
openai.api_key = "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"
|
| 14 |
+
|
| 15 |
+
import streamlit as st
|
| 16 |
+
|
| 17 |
+
# Page title and header
|
| 18 |
+
st.title("PowerPoint Presentation Generator")
|
| 19 |
+
st.header("Welcome! Create Engaging Presentations with Ease!")
|
| 20 |
+
# What we do
|
| 21 |
+
st.write("Provide your topic, and we'll add engaging content.")
|
| 22 |
+
st.write("Our AI ensures balanced slides.")
|
| 23 |
+
st.write("Exciting features coming soon. 🚀")
|
| 24 |
+
# Limits
|
| 25 |
+
st.write("Max 10 slides with 3-8 points each. ⚠️")
|
| 26 |
+
# Important Notice
|
| 27 |
+
st.warning("Vertical format issue? Try downloading again. Contact us for help.")
|
| 28 |
+
# Contact and Feedback
|
| 29 |
+
st.subheader("Contact and Feedback")
|
| 30 |
+
st.write("Questions or ideas? Contact us anytime!")
|
| 31 |
+
st.write("Your feedback makes us better. 🌟")
|
| 32 |
+
|
| 33 |
+
# Input for presentation topic
|
| 34 |
+
presentation_title = st.text_input("Enter your presentation topic:")
|
| 35 |
+
no_of_pages = st.number_input("Number of slides you want(max 10)", min_value=2, value=2) # Accept and display float values
|
| 36 |
+
least_c = st.number_input("Minimum points in each slide(min 2)", min_value=2, value=2) # Accept and display float values
|
| 37 |
+
max_c = st.number_input("Maximum points in each slide(max 8)", max_value=8, value=8) # Accept and display float values
|
| 38 |
+
|
| 39 |
+
if st.button("Generate Presentation"):
|
| 40 |
+
# Check if the user has entered a topic
|
| 41 |
+
if presentation_title:
|
| 42 |
+
question = (
|
| 43 |
+
f"generate a {no_of_pages} slide presentation for the topic {presentation_title}."
|
| 44 |
+
f" Each slide should have {{header}}, {{content}}, should have at least {least_c} points and max {max_c} points in content. Return as JSON"
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
query_json = {
|
| 48 |
+
"input_text": question,
|
| 49 |
+
"output_format": "json",
|
| 50 |
+
"json_structure": {"slides": "{{presentation_slides}}"},
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
# Send the query to OpenAI
|
| 54 |
+
completion = openai.ChatCompletion.create(
|
| 55 |
+
model="gpt-3.5-turbo",
|
| 56 |
+
messages=[{"role": "user", "content": json.dumps(query_json)}],
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
response = json.loads(completion.choices[0].message.content)
|
| 61 |
+
|
| 62 |
+
slide_data = response["slides"]
|
| 63 |
+
|
| 64 |
+
prs = Presentation()
|
| 65 |
+
|
| 66 |
+
from pptx.util import Pt # Import the Pt class from pptx.util
|
| 67 |
+
from pptx.enum.text import PP_ALIGN # Import text alignment options
|
| 68 |
+
|
| 69 |
+
# Iterate through slide data and populate the presentation
|
| 70 |
+
for slide in slide_data:
|
| 71 |
+
slide_layout = prs.slide_layouts[1]
|
| 72 |
+
new_slide = prs.slides.add_slide(slide_layout)
|
| 73 |
+
|
| 74 |
+
if slide["header"]:
|
| 75 |
+
title = new_slide.shapes.title
|
| 76 |
+
title.text = slide["header"]
|
| 77 |
+
|
| 78 |
+
if slide["content"]:
|
| 79 |
+
shapes = new_slide.shapes
|
| 80 |
+
body_shape = shapes.placeholders[1]
|
| 81 |
+
tf = body_shape.text_frame
|
| 82 |
+
|
| 83 |
+
# Join the list of content into a single string with line breaks
|
| 84 |
+
content_text = "\n".join(slide["content"])
|
| 85 |
+
|
| 86 |
+
p = tf.add_paragraph()
|
| 87 |
+
p.text = content_text
|
| 88 |
+
|
| 89 |
+
# Set the spacing between lines using Pt (point size)
|
| 90 |
+
p.space_after = Pt(14) # Adjust the spacing between lines
|
| 91 |
+
|
| 92 |
+
# Set text direction to horizontal (left-to-right)
|
| 93 |
+
p.alignment = PP_ALIGN.LEFT
|
| 94 |
+
|
| 95 |
+
# Save the presentation in PPTX format with the topic name as the file name
|
| 96 |
+
presentation_filename = f"{presentation_title}.pptx"
|
| 97 |
+
prs.save(presentation_filename)
|
| 98 |
+
|
| 99 |
+
# Provide a direct download link with a custom filename
|
| 100 |
+
with open(presentation_filename, "rb") as file:
|
| 101 |
+
# Specify the custom filename using the "key" parameter
|
| 102 |
+
st.download_button(
|
| 103 |
+
label=f"Download {presentation_title}",
|
| 104 |
+
data=file.read(),
|
| 105 |
+
key=presentation_filename, # Use the filename as the key
|
| 106 |
+
file_name=presentation_filename # Use the same filename for the downloaded file
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
# Delete the temporary presentation file
|
| 110 |
+
os.remove(presentation_filename)
|
| 111 |
+
except json.JSONDecodeError as e:
|
| 112 |
+
st.error("Error parsing JSON response from OpenAI.")
|
| 113 |
+
else:
|
| 114 |
+
st.warning("Please enter a presentation topic.")
|