kshitij10000's picture
Update app.py
e610d6e
import openai
import json
from pptx import Presentation
import streamlit as st
import os
# Suppress all warnings
import warnings
warnings.filterwarnings("ignore")
# Set your OpenAI API key
openai.api_key = "your api key"
# Streamlit app title and header with improved styling
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)
# User Input Section with clickable options
presentation_title = st.text_input("πŸ“ Enter your presentation topic:")
# Predefined topics (hidden from the user)
predefined_topics = ["Artificial Intelligence", "Blockchain Technology", "Space Exploration", "Climate Change", "Future of Work"]
# Display clickable options for predefined topics (hidden)
selected_topic = st.radio("🌐 Choose a predefined topic or enter your own:", ["Select"] + predefined_topics, index=0)
# If a predefined topic is selected, set it as the input
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)
# Check if any input values are out of range
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"):
# Check if the user has entered a topic
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}}"},
}
# Send the query to OpenAI
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.")