Spaces:
Sleeping
Sleeping
| 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.") |