Tanseer45203 commited on
Commit
6596dc8
·
verified ·
1 Parent(s): 1967cc5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -65
app.py CHANGED
@@ -1,80 +1,25 @@
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
  for slide_content in slide_contents:
48
  pdf.add_page()
49
  title, *points = slide_content.split("\n")
 
 
 
 
50
  pdf.set_font("Arial", style="B", size=16)
51
  pdf.cell(0, 10, title, ln=True, align="C")
 
52
  pdf.set_font("Arial", style="", size=12)
53
  for point in points:
 
 
54
  pdf.multi_cell(0, 10, f"- {point}")
 
55
  pdf_io = BytesIO()
56
  pdf.output(pdf_io, "F")
57
- return pdf_io
58
-
59
- # Streamlit UI
60
- st.title("📊 AI Slide Generator: PPT & PDF")
61
- st.write("Generate up to 50 slides using AI and download them as PowerPoint or PDF.")
62
-
63
- # User input
64
- topic = st.text_input("Enter the topic for slides")
65
- num_slides = st.slider("Select number of slides", 1, 50, 5)
66
-
67
- if st.button("Generate Slides"):
68
- if topic.strip():
69
- with st.spinner("Generating slides..."):
70
- slide_contents = generate_slide_content(topic, num_slides)
71
- ppt_io = create_ppt(slide_contents)
72
- pdf_io = create_pdf(slide_contents)
73
-
74
- # Provide Download Links
75
- st.success("Slides generated successfully! Download below:")
76
-
77
- st.download_button("Download PPT", ppt_io.getvalue(), "slides.pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation")
78
- st.download_button("Download PDF", pdf_io.getvalue(), "slides.pdf", "application/pdf")
79
- else:
80
- st.error("Please enter a valid topic.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # Function to create a PDF
2
  def create_pdf(slide_contents):
3
  pdf = FPDF()
4
  pdf.set_auto_page_break(auto=True, margin=15)
5
  pdf.set_font("Arial", style="", size=12)
6
+
7
  for slide_content in slide_contents:
8
  pdf.add_page()
9
  title, *points = slide_content.split("\n")
10
+
11
+ # Encode title properly
12
+ title = title.encode("latin-1", "ignore").decode("latin-1")
13
+
14
  pdf.set_font("Arial", style="B", size=16)
15
  pdf.cell(0, 10, title, ln=True, align="C")
16
+
17
  pdf.set_font("Arial", style="", size=12)
18
  for point in points:
19
+ # Replace unsupported characters before writing to PDF
20
+ point = point.replace("•", "-").encode("latin-1", "ignore").decode("latin-1")
21
  pdf.multi_cell(0, 10, f"- {point}")
22
+
23
  pdf_io = BytesIO()
24
  pdf.output(pdf_io, "F")
25
+ return pdf_iotopic.")