|
import openai |
|
import json |
|
from pptx import Presentation |
|
import streamlit as st |
|
import os |
|
|
|
|
|
import warnings |
|
warnings.filterwarnings("ignore") |
|
|
|
|
|
openai.api_key = "your api key" |
|
|
|
|
|
st.title("π PowerPoint Presentation Generator π") |
|
st.markdown("<h1 style='text-align: center; color: white;'>Welcome to the Future of Presentation Creation!</h1>", unsafe_allow_html=True) |
|
|
|
|
|
presentation_title = st.text_input("π Enter your presentation topic:") |
|
|
|
|
|
predefined_topics = ["Artificial Intelligence", "Blockchain Technology", "Space Exploration", "Climate Change", "Future of Work"] |
|
|
|
|
|
selected_topic = st.radio("π Choose a predefined topic or enter your own:", ["Select"] + predefined_topics, index=0) |
|
|
|
|
|
if selected_topic != "Select": |
|
presentation_title = selected_topic |
|
|
|
custom_requirements = st.text_input("π Enter your custom requirements you want:", value="informative") |
|
no_of_pages = st.number_input("π Number of slides you want (3-8)", min_value=3, max_value=8, value=3) |
|
least_c = st.number_input("π Minimum points in each slide (3-7)", min_value=3, max_value=7, value=3) |
|
max_c = st.number_input("π Maximum points in each slide (4-7)", min_value=4, max_value=7, value=7) |
|
|
|
|
|
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: |
|
st.warning("Invalid input values. Please ensure that your inputs are within the specified limits.") |
|
else: |
|
if st.button("Generate Presentation"): |
|
|
|
if presentation_title: |
|
question = ( |
|
f"Create a {no_of_pages}-slide PowerPoint presentation on the topic of {presentation_title}." |
|
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." |
|
) |
|
|
|
query_json = { |
|
"input_text": question, |
|
"output_format": "json", |
|
"json_structure": {"slides": "{{presentation_slides}}"}, |
|
} |
|
|
|
|
|
completion = openai.ChatCompletion.create( |
|
model="gpt-3.5-turbo", |
|
messages=[{"role": "user", "content": json.dumps(query_json)}], |
|
) |
|
|
|
try: |
|
response = json.loads(completion.choices[0].message.content) |
|
|
|
if "slides" in response: |
|
slide_data = response["slides"] |
|
prs = Presentation() |
|
|
|
from pptx.util import Pt |
|
from pptx.enum.text import PP_ALIGN |
|
|
|
for slide in slide_data: |
|
slide_layout = prs.slide_layouts[1] |
|
new_slide = prs.slides.add_slide(slide_layout) |
|
|
|
if slide["header"]: |
|
title = new_slide.shapes.title |
|
title.text = slide["header"] |
|
|
|
if slide["content"]: |
|
shapes = new_slide.shapes |
|
body_shape = shapes.placeholders[1] |
|
tf = body_shape.text_frame |
|
|
|
content_text = "\n".join(slide["content"]) |
|
|
|
p = tf.add_paragraph() |
|
p.text = content_text |
|
p.space_after = Pt(14) |
|
p.alignment = PP_ALIGN.LEFT |
|
|
|
presentation_filename = f"{presentation_title}.pptx" |
|
prs.save(presentation_filename) |
|
|
|
with open(presentation_filename, "rb") as file: |
|
st.download_button( |
|
label=f"π₯ Download {presentation_title}", |
|
data=file.read(), |
|
key=presentation_filename, |
|
file_name=presentation_filename |
|
) |
|
|
|
os.remove(presentation_filename) |
|
else: |
|
st.warning("No slides were generated for this presentation.") |
|
|
|
except json.JSONDecodeError as e: |
|
st.error("Error parsing JSON response from OpenAI.") |
|
else: |
|
st.warning("Please enter a presentation topic.") |
|
|