Mariamexpo / app.py
Docfile's picture
Update app.py
98d450e verified
raw
history blame
4.85 kB
# presentation_generator.py
import os
from crewai import Crew, LLM, Agent
from crewai_tools import Tool, SerperDevTool
import streamlit as st
import base64
# import pypdfium2 as pdfium # Removed
# from fpdf import FPDF # Removed
# Replace with your actual API keys
GEMINI_API_KEY = "AIzaSyD6yZxfVOnh63GXBJjakAupk9aP4CZrgrQ"
SERPER_API_KEY = "9b90a274d9e704ff5b21c0367f9ae1161779b573"
llm = LLM(
model="gemini/gemini-1.5-flash",
temperature=0.7,
timeout=120,
max_tokens=8000,
api_key=GEMINI_API_KEY
)
serper_tool = SerperDevTool(api_key=SERPER_API_KEY)
# Plan Generator Agent
class PlanGeneratorAgent(Agent):
def __init__(self):
super().__init__(
name="PlanGenerator",
role="Presentation Planner",
goal="Create detailed and well-structured presentation outlines",
backstory="""You are an experienced presentation strategist who excels at
creating logical and impactful presentation structures. You understand how
to organize information for maximum audience engagement.""",
llm=llm,
tools=[serper_tool]
)
def run(self, theme):
research = serper_tool.search(query=f"Best practices for {theme} presentations")
plan = "Introduction\nMain Points\nConclusion"
return plan
# Content Generator Agent
class ContentGeneratorAgent(Agent):
def __init__(self):
super().__init__(
name="ContentGenerator",
role="Content Creator",
goal="Generate engaging and informative presentation content",
backstory="""You are a creative content specialist with expertise in
transforming ideas into compelling presentation material. You know how
to balance information with engagement.""",
llm=llm
)
def run(self, section, theme):
prompt = f"Generate {section} for a presentation on {theme}."
content = self.llm.generate(prompt)
return content
# PDF Compiler Agent # Removed
# class PDFCompilerAgent(Agent):
# def __init__(self):
# super().__init__(
# name="PDFCompiler",
# role="Document Formatter",
# goal="Create professional and well-formatted PDF presentations",
# backstory="""You are a document formatting expert who knows how to
# transform content into visually appealing and professional PDFs. You
# understand typography, layout, and document structure.""",
# llm=llm
# )
# def run(self, content_dict):
# pdf = FPDF()
# pdf.add_page()
# pdf.set_font("Arial", size=12)
# for section, content in content_dict.items():
# pdf.cell(0, 10, f"{section}", ln=True)
# pdf.multi_cell(0, 10, f"{content}", ln=True)
# pdf.output("presentation.pdf")
# return "presentation.pdf"
# Main Crew
class PresentationCrew(Crew):
def __init__(self):
super().__init__(
name="PresentationGenerator",
description="Generates a high-quality presentation based on a given theme.",
llm=llm,
agents=[PlanGeneratorAgent(), ContentGeneratorAgent()] # Removed PDFCompilerAgent
)
def run(self, theme):
try:
plan_agent = self.agents[0]
content_agent = self.agents[1]
plan = plan_agent.run(theme)
sections = plan.split("\n")
content_dict = {}
for section in sections:
if section:
content = content_agent.run(section, theme)
content_dict[section] = content
print(content_dict)
return content_dict
except Exception as e:
print(f"An error occurred: {e}")
return None
# Streamlit App Functions
# def get_pdf_download_link(pdf_path): # Removed
# with open(pdf_path, "rb") as f:
# pdf_bytes = f.read()
# encoded = base64.b64encode(pdf_bytes).decode()
# return f'<a href="data:application/pdf;base64,{encoded}" download="presentation.pdf">Download PDF</a>'
def main():
st.title("Advanced Presentation Generator")
theme = st.text_input("Enter the presentation theme:")
if st.button("Generate Presentation"):
crew = PresentationCrew()
presentation_content = crew.run(theme)
if presentation_content:
st.success("Presentation generated successfully!")
for section, content in presentation_content.items():
st.subheader(section)
st.write(content)
st.markdown("---") # Separator between sections
else:
st.error("Failed to generate presentation.")
if __name__ == "__main__":
main()