Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from pptx import Presentation
|
| 4 |
+
from pptx.util import Inches
|
| 5 |
+
from fpdf import FPDF
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
from groq import Groq
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
|
| 10 |
+
# Load environment variables
|
| 11 |
+
load_dotenv()
|
| 12 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 13 |
+
|
| 14 |
+
# Initialize Groq Client
|
| 15 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 16 |
+
|
| 17 |
+
# Function to generate slide content using LLaMA 3 70B
|
| 18 |
+
def generate_slide_content(topic, num_slides):
|
| 19 |
+
response = client.chat.completions.create(
|
| 20 |
+
model="llama-3.3-70b-versatile",
|
| 21 |
+
messages=[
|
| 22 |
+
{"role": "system", "content": "You are an AI that creates structured PowerPoint slide content."},
|
| 23 |
+
{"role": "user", "content": f"Generate {num_slides} slides about {topic} with proper headings and bullet points."}
|
| 24 |
+
]
|
| 25 |
+
)
|
| 26 |
+
return response.choices[0].message.content.split("\n\n") # Splitting slides by paragraphs
|
| 27 |
+
|
| 28 |
+
# Function to create a PowerPoint presentation
|
| 29 |
+
def create_ppt(slide_contents):
|
| 30 |
+
prs = Presentation()
|
| 31 |
+
for slide_content in slide_contents:
|
| 32 |
+
slide = prs.slides.add_slide(prs.slide_layouts[1]) # Title & Content layout
|
| 33 |
+
title, *points = slide_content.split("\n")
|
| 34 |
+
slide.shapes.title.text = title
|
| 35 |
+
text_box = slide.shapes.placeholders[1].text_frame
|
| 36 |
+
for point in points:
|
| 37 |
+
text_box.add_paragraph().text = point
|
| 38 |
+
ppt_io = BytesIO()
|
| 39 |
+
prs.save(ppt_io)
|
| 40 |
+
return ppt_io
|
| 41 |
+
|
| 42 |
+
# Function to create a PDF
|
| 43 |
+
def create_pdf(slide_contents):
|
| 44 |
+
pdf = FPDF()
|
| 45 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
| 46 |
+
pdf.set_font("Arial", style="", size=12)
|
| 47 |
+
|
| 48 |
+
for slide_content in slide_contents:
|
| 49 |
+
pdf.add_page()
|
| 50 |
+
title, *points = slide_content.split("\n")
|
| 51 |
+
|
| 52 |
+
# Encode title properly
|
| 53 |
+
title = title.encode("latin-1", "ignore").decode("latin-1")
|
| 54 |
+
|
| 55 |
+
pdf.set_font("Arial", style="B", size=16)
|
| 56 |
+
pdf.cell(0, 10, title, ln=True, align="C")
|
| 57 |
+
|
| 58 |
+
pdf.set_font("Arial", style="", size=12)
|
| 59 |
+
for point in points:
|
| 60 |
+
# Replace unsupported characters before writing to PDF
|
| 61 |
+
point = point.replace("•", "-").encode("latin-1", "ignore").decode("latin-1")
|
| 62 |
+
pdf.multi_cell(0, 10, f"- {point}")
|
| 63 |
+
|
| 64 |
+
pdf_io = BytesIO()
|
| 65 |
+
pdf.output(pdf_io, "F")
|
| 66 |
+
return pdf_io
|
| 67 |
+
|
| 68 |
+
# Streamlit UI
|
| 69 |
+
st.title("📊 AI Slide Generator: PPT & PDF")
|
| 70 |
+
st.write("Generate up to 50 slides using AI and download them as PowerPoint or PDF.")
|
| 71 |
+
|
| 72 |
+
# User input
|
| 73 |
+
topic = st.text_input("Enter the topic for slides")
|
| 74 |
+
num_slides = st.slider("Select number of slides", 1, 50, 5)
|
| 75 |
+
|
| 76 |
+
if st.button("Generate Slides"):
|
| 77 |
+
if topic.strip():
|
| 78 |
+
with st.spinner("Generating slides..."):
|
| 79 |
+
slide_contents = generate_slide_content(topic, num_slides)
|
| 80 |
+
ppt_io = create_ppt(slide_contents)
|
| 81 |
+
pdf_io = create_pdf(slide_contents)
|
| 82 |
+
|
| 83 |
+
# Provide Download Links
|
| 84 |
+
st.success("Slides generated successfully! Download below:")
|
| 85 |
+
|
| 86 |
+
st.download_button("Download PPT", ppt_io.getvalue(), "slides.pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation")
|
| 87 |
+
st.download_button("Download PDF", pdf_io.getvalue(), "slides.pdf", "application/pdf")
|
| 88 |
+
else:
|
| 89 |
+
st.error("Please enter a valid topic.")
|