File size: 3,249 Bytes
de24ef7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import os
import streamlit as st
from pptx import Presentation
from pptx.util import Inches
from fpdf import FPDF
from io import BytesIO
from groq import Groq
from dotenv import load_dotenv

# Load environment variables
load_dotenv()
GROQ_API_KEY = os.getenv("GROQ_API_KEY")

# Initialize Groq Client
client = Groq(api_key=GROQ_API_KEY)

# Function to generate slide content using LLaMA 3 70B
def generate_slide_content(topic, num_slides):
    response = client.chat.completions.create(
        model="llama-3.3-70b-versatile",
        messages=[
            {"role": "system", "content": "You are an AI that creates structured PowerPoint slide content."},
            {"role": "user", "content": f"Generate {num_slides} slides about {topic} with proper headings and bullet points."}
        ]
    )
    return response.choices[0].message.content.split("\n\n")  # Splitting slides by paragraphs

# Function to create a PowerPoint presentation
def create_ppt(slide_contents):
    prs = Presentation()
    for slide_content in slide_contents:
        slide = prs.slides.add_slide(prs.slide_layouts[1])  # Title & Content layout
        title, *points = slide_content.split("\n")
        slide.shapes.title.text = title
        text_box = slide.shapes.placeholders[1].text_frame
        for point in points:
            text_box.add_paragraph().text = point
    ppt_io = BytesIO()
    prs.save(ppt_io)
    return ppt_io

# Function to create a PDF
def create_pdf(slide_contents):
    pdf = FPDF()
    pdf.set_auto_page_break(auto=True, margin=15)
    pdf.set_font("Arial", style="", size=12)

    for slide_content in slide_contents:
        pdf.add_page()
        title, *points = slide_content.split("\n")

        # Encode title properly
        title = title.encode("latin-1", "ignore").decode("latin-1")

        pdf.set_font("Arial", style="B", size=16)
        pdf.cell(0, 10, title, ln=True, align="C")

        pdf.set_font("Arial", style="", size=12)
        for point in points:
            # Replace unsupported characters before writing to PDF
            point = point.replace("•", "-").encode("latin-1", "ignore").decode("latin-1")
            pdf.multi_cell(0, 10, f"- {point}")

    pdf_io = BytesIO()
    pdf.output(pdf_io, "F")
    return pdf_io

# Streamlit UI
st.title("📊 AI Slide Generator: PPT & PDF")
st.write("Generate up to 50 slides using AI and download them as PowerPoint or PDF.")

# User input
topic = st.text_input("Enter the topic for slides")
num_slides = st.slider("Select number of slides", 1, 50, 5)

if st.button("Generate Slides"):
    if topic.strip():
        with st.spinner("Generating slides..."):
            slide_contents = generate_slide_content(topic, num_slides)
            ppt_io = create_ppt(slide_contents)
            pdf_io = create_pdf(slide_contents)

            # Provide Download Links
            st.success("Slides generated successfully! Download below:")
            
            st.download_button("Download PPT", ppt_io.getvalue(), "slides.pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation")
            st.download_button("Download PDF", pdf_io.getvalue(), "slides.pdf", "application/pdf")
    else:
        st.error("Please enter a valid topic.")