Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,143 +1,90 @@
|
|
1 |
-
# presentation_generator.py
|
2 |
-
|
3 |
import os
|
4 |
-
from crewai import Crew, LLM, Agent
|
5 |
-
from crewai_tools import Tool, SerperDevTool
|
6 |
-
import streamlit as st
|
7 |
-
import base64
|
8 |
-
# import pypdfium2 as pdfium # Removed
|
9 |
-
# from fpdf import FPDF # Removed
|
10 |
-
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
)
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
goal="Create detailed and well-structured presentation outlines",
|
33 |
-
backstory="""You are an experienced presentation strategist who excels at
|
34 |
-
creating logical and impactful presentation structures. You understand how
|
35 |
-
to organize information for maximum audience engagement.""",
|
36 |
-
llm=llm,
|
37 |
-
tools=[serper_tool]
|
38 |
-
)
|
39 |
-
|
40 |
-
def run(self, theme):
|
41 |
-
research = serper_tool.search(query=f"Best practices for {theme} presentations")
|
42 |
-
plan = "Introduction\nMain Points\nConclusion"
|
43 |
-
return plan
|
44 |
-
|
45 |
-
# Content Generator Agent
|
46 |
-
class ContentGeneratorAgent(Agent):
|
47 |
-
def __init__(self):
|
48 |
-
super().__init__(
|
49 |
-
name="ContentGenerator",
|
50 |
-
role="Content Creator",
|
51 |
-
goal="Generate engaging and informative presentation content",
|
52 |
-
backstory="""You are a creative content specialist with expertise in
|
53 |
-
transforming ideas into compelling presentation material. You know how
|
54 |
-
to balance information with engagement.""",
|
55 |
-
llm=llm
|
56 |
-
)
|
57 |
-
|
58 |
-
def run(self, section, theme):
|
59 |
-
prompt = f"Generate {section} for a presentation on {theme}."
|
60 |
-
content = self.llm.generate(prompt)
|
61 |
-
return content
|
62 |
-
|
63 |
-
# PDF Compiler Agent # Removed
|
64 |
-
# class PDFCompilerAgent(Agent):
|
65 |
-
# def __init__(self):
|
66 |
-
# super().__init__(
|
67 |
-
# name="PDFCompiler",
|
68 |
-
# role="Document Formatter",
|
69 |
-
# goal="Create professional and well-formatted PDF presentations",
|
70 |
-
# backstory="""You are a document formatting expert who knows how to
|
71 |
-
# transform content into visually appealing and professional PDFs. You
|
72 |
-
# understand typography, layout, and document structure.""",
|
73 |
-
# llm=llm
|
74 |
-
# )
|
75 |
-
|
76 |
-
# def run(self, content_dict):
|
77 |
-
# pdf = FPDF()
|
78 |
-
# pdf.add_page()
|
79 |
-
# pdf.set_font("Arial", size=12)
|
80 |
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
-
#
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
)
|
97 |
|
98 |
-
|
99 |
-
|
100 |
-
plan_agent = self.agents[0]
|
101 |
-
content_agent = self.agents[1]
|
102 |
-
|
103 |
-
plan = plan_agent.run(theme)
|
104 |
-
sections = plan.split("\n")
|
105 |
-
content_dict = {}
|
106 |
-
|
107 |
-
for section in sections:
|
108 |
-
if section:
|
109 |
-
content = content_agent.run(section, theme)
|
110 |
-
content_dict[section] = content
|
111 |
-
|
112 |
-
print(content_dict)
|
113 |
-
return content_dict
|
114 |
-
except Exception as e:
|
115 |
-
print(f"An error occurred: {e}")
|
116 |
-
return None
|
117 |
-
|
118 |
-
# Streamlit App Functions
|
119 |
-
# def get_pdf_download_link(pdf_path): # Removed
|
120 |
-
# with open(pdf_path, "rb") as f:
|
121 |
-
# pdf_bytes = f.read()
|
122 |
-
# encoded = base64.b64encode(pdf_bytes).decode()
|
123 |
-
# return f'<a href="data:application/pdf;base64,{encoded}" download="presentation.pdf">Download PDF</a>'
|
124 |
-
|
125 |
-
def main():
|
126 |
-
st.title("Advanced Presentation Generator")
|
127 |
-
|
128 |
-
theme = st.text_input("Enter the presentation theme:")
|
129 |
-
|
130 |
-
if st.button("Generate Presentation"):
|
131 |
-
crew = PresentationCrew()
|
132 |
-
presentation_content = crew.run(theme)
|
133 |
-
if presentation_content:
|
134 |
-
st.success("Presentation generated successfully!")
|
135 |
-
for section, content in presentation_content.items():
|
136 |
-
st.subheader(section)
|
137 |
-
st.write(content)
|
138 |
-
st.markdown("---") # Separator between sections
|
139 |
-
else:
|
140 |
-
st.error("Failed to generate presentation.")
|
141 |
|
142 |
-
|
143 |
-
|
|
|
|
|
|
|
1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
import streamlit as st
|
4 |
+
from crewai import Agent, Crew, Process, Task
|
5 |
+
from crewai.tools import SerperDevTool
|
6 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
7 |
+
|
8 |
+
# Assurez-vous que vos clés API sont définies dans les variables d'environnement
|
9 |
+
os.environ["OPENAI_API_KEY"] = st.secrets["OPENAI_API_KEY"]
|
10 |
+
os.environ["SERPER_API_KEY"] = st.secrets["SERPER_API_KEY"]
|
11 |
+
|
12 |
+
# Définition des agents
|
13 |
+
project_manager = Agent(
|
14 |
+
role="Chef de Projet",
|
15 |
+
goal="Coordonner les autres agents pour assurer la cohérence et la qualité de l'exposé.",
|
16 |
+
backstory="Un chef de projet expérimenté avec une expertise dans la gestion d'équipes et la coordination de projets complexes.",
|
17 |
+
llm=ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=os.environ["GOOGLE_API_KEY"]),
|
18 |
+
verbose=True,
|
19 |
+
allow_delegation=True,
|
20 |
)
|
21 |
|
22 |
+
topic_analyst = Agent(
|
23 |
+
role="Analyste de Thème",
|
24 |
+
goal="Analyser le thème de l'exposé et générer un plan détaillé.",
|
25 |
+
backstory="Un expert en recherche et en structuration de contenu, capable de décomposer des sujets complexes en plans clairs et concis.",
|
26 |
+
llm=ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=os.environ["GOOGLE_API_KEY"]),
|
27 |
+
verbose=True,
|
28 |
+
tools=[SerperDevTool()],
|
29 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
content_writer = Agent(
|
32 |
+
role="Rédacteur de Contenu",
|
33 |
+
goal="Rédiger les sections de l'exposé en se basant sur le plan et les recherches.",
|
34 |
+
backstory="Un rédacteur spécialisé dans la création de contenu clair, concis et engageant.",
|
35 |
+
llm=ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=os.environ["GOOGLE_API_KEY"]),
|
36 |
+
verbose=True,
|
37 |
+
tools=[SerperDevTool()],
|
38 |
+
)
|
39 |
|
40 |
+
editor = Agent(
|
41 |
+
role="Éditeur/Réviseur",
|
42 |
+
goal="Réviser et peaufiner le contenu, assurer la cohérence du style et du ton, corriger les erreurs.",
|
43 |
+
backstory="Un éditeur expérimenté avec un souci du détail et une excellente maîtrise de la langue.",
|
44 |
+
llm=ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=os.environ["GOOGLE_API_KEY"]),
|
45 |
+
verbose=True,
|
46 |
+
)
|
47 |
|
48 |
+
# Définition des tâches
|
49 |
+
def assign_tasks(topic):
|
50 |
+
analyze_topic_task = Task(
|
51 |
+
description=f"Analyser le thème '{topic}' et générer un plan détaillé pour l'exposé, divisé en sections claires.",
|
52 |
+
expected_output="Un plan détaillé de l'exposé au format markdown.",
|
53 |
+
agent=topic_analyst,
|
54 |
+
)
|
55 |
+
|
56 |
+
write_content_task = Task(
|
57 |
+
description=f"Rédiger les sections de l'exposé en se basant sur le plan généré par l'analyste de thème et sur des recherches approfondies.",
|
58 |
+
expected_output="Contenu des sections au format markdown.",
|
59 |
+
agent=content_writer,
|
60 |
+
context=[analyze_topic_task],
|
61 |
+
)
|
62 |
+
|
63 |
+
edit_content_task = Task(
|
64 |
+
description="Réviser et corriger l'ensemble du contenu de l'exposé, en s'assurant de la cohérence, de la clarté et de l'absence d'erreurs.",
|
65 |
+
expected_output="Contenu de l'exposé révisé et corrigé au format markdown.",
|
66 |
+
agent=editor,
|
67 |
+
context=[write_content_task],
|
68 |
+
)
|
69 |
+
|
70 |
+
return [analyze_topic_task, write_content_task, edit_content_task]
|
71 |
+
|
72 |
+
# Configuration de l'interface Streamlit
|
73 |
+
st.title("🤖 Générateur d'Exposés avec CrewAI")
|
74 |
+
|
75 |
+
topic = st.text_input("Entrez le thème de l'exposé:", "L'Intelligence Artificielle en 2024")
|
76 |
+
|
77 |
+
if st.button("Générer l'exposé"):
|
78 |
+
with st.spinner("Création de l'équipe d'agents..."):
|
79 |
+
crew = Crew(
|
80 |
+
agents=[project_manager, topic_analyst, content_writer, editor],
|
81 |
+
tasks=assign_tasks(topic),
|
82 |
+
process=Process.hierarchical,
|
83 |
+
verbose=True,
|
84 |
)
|
85 |
|
86 |
+
with st.spinner("Génération de l'exposé..."):
|
87 |
+
result = crew.kickoff()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
+
st.success("Exposé généré avec succès!")
|
90 |
+
st.markdown(result)
|