Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from huggingface_hub import hf_hub_download
|
4 |
+
from llama_cpp import Llama
|
5 |
+
from pptx import Presentation
|
6 |
+
from pptx.util import Inches, Pt
|
7 |
+
from pptx.enum.text import PP_ALIGN
|
8 |
+
|
9 |
+
# Préprompt amélioré pour une meilleure structuration
|
10 |
+
PREPROMPT = """Vous êtes un assistant IA chargé de générer une présentation PowerPoint. Générez une présentation structurée en suivant ce format EXACT:
|
11 |
+
|
12 |
+
TITRE: [Titre principal de la présentation]
|
13 |
+
|
14 |
+
DIAPO 1:
|
15 |
+
Titre: [Titre de la diapo]
|
16 |
+
Points:
|
17 |
+
- Point 1
|
18 |
+
- Point 2
|
19 |
+
- Point 3
|
20 |
+
|
21 |
+
DIAPO 2:
|
22 |
+
Titre: [Titre de la diapo]
|
23 |
+
Points:
|
24 |
+
- Point 1
|
25 |
+
- Point 2
|
26 |
+
- Point 3
|
27 |
+
|
28 |
+
[Continuez avec ce format pour chaque diapositive]
|
29 |
+
|
30 |
+
Analysez le texte suivant et créez une présentation claire et professionnelle :"""
|
31 |
+
|
32 |
+
# Téléchargement du modèle
|
33 |
+
model_file = "mistralai_Mistral-Small-24B-Base-2501-Q8_0.gguf"
|
34 |
+
model_path = hf_hub_download(
|
35 |
+
repo_id="MisterAI/Bartowski_MistralAI_Mistral-Small-24B-Base-2501-GGUF",
|
36 |
+
filename=model_file,
|
37 |
+
repo_type="model"
|
38 |
+
)
|
39 |
+
|
40 |
+
# Initialisation du modèle
|
41 |
+
text_to_presentation = Llama(model_path=model_path, verbose=True)
|
42 |
+
|
43 |
+
def parse_presentation_content(content):
|
44 |
+
"""Parse le contenu généré en sections pour les diapositives"""
|
45 |
+
slides = []
|
46 |
+
current_slide = None
|
47 |
+
|
48 |
+
for line in content.split('\n'):
|
49 |
+
line = line.strip()
|
50 |
+
if line.startswith('TITRE:'):
|
51 |
+
slides.append({'type': 'title', 'title': line[6:].strip()})
|
52 |
+
elif line.startswith('DIAPO'):
|
53 |
+
if current_slide:
|
54 |
+
slides.append(current_slide)
|
55 |
+
current_slide = {'type': 'content', 'title': '', 'points': []}
|
56 |
+
elif line.startswith('Titre:') and current_slide:
|
57 |
+
current_slide['title'] = line[6:].strip()
|
58 |
+
elif line.startswith('- ') and current_slide:
|
59 |
+
current_slide['points'].append(line[2:].strip())
|
60 |
+
|
61 |
+
if current_slide:
|
62 |
+
slides.append(current_slide)
|
63 |
+
|
64 |
+
return slides
|
65 |
+
|
66 |
+
def create_presentation(slides):
|
67 |
+
"""Crée la présentation PowerPoint à partir des sections parsées"""
|
68 |
+
prs = Presentation()
|
69 |
+
|
70 |
+
# Première diapo (titre)
|
71 |
+
title_slide = prs.slides.add_slide(prs.slide_layouts[0])
|
72 |
+
title_slide.shapes.title.text = slides[0]['title']
|
73 |
+
|
74 |
+
# Autres diapos
|
75 |
+
for slide in slides[1:]:
|
76 |
+
content_slide = prs.slides.add_slide(prs.slide_layouts[1])
|
77 |
+
content_slide.shapes.title.text = slide['title']
|
78 |
+
|
79 |
+
if slide['points']:
|
80 |
+
body = content_slide.shapes.placeholders[1].text_frame
|
81 |
+
body.clear()
|
82 |
+
for point in slide['points']:
|
83 |
+
p = body.add_paragraph()
|
84 |
+
p.text = point
|
85 |
+
p.level = 0
|
86 |
+
|
87 |
+
return prs
|
88 |
+
|
89 |
+
def generate_presentation(text):
|
90 |
+
# Ajout du préprompt au texte de l'utilisateur
|
91 |
+
full_prompt = PREPROMPT + "\n\n" + text
|
92 |
+
|
93 |
+
# Génération du contenu avec le modèle
|
94 |
+
response = text_to_presentation(
|
95 |
+
full_prompt,
|
96 |
+
max_tokens=2000,
|
97 |
+
temperature=0.7,
|
98 |
+
stop=["<end>"],
|
99 |
+
echo=False
|
100 |
+
)
|
101 |
+
|
102 |
+
# Extraction du texte généré
|
103 |
+
generated_content = response['choices'][0]['text']
|
104 |
+
|
105 |
+
# Parse le contenu et crée la présentation
|
106 |
+
slides = parse_presentation_content(generated_content)
|
107 |
+
prs = create_presentation(slides)
|
108 |
+
|
109 |
+
# Sauvegarde de la présentation
|
110 |
+
output_path = "presentation.pptx"
|
111 |
+
prs.save(output_path)
|
112 |
+
|
113 |
+
return f"Présentation générée avec succès ! Vous pouvez la télécharger ici : {os.path.abspath(output_path)}"
|
114 |
+
|
115 |
+
# Interface Gradio
|
116 |
+
demo = gr.Interface(
|
117 |
+
fn=generate_presentation,
|
118 |
+
inputs=gr.Textbox(lines=10, label="Entrez votre texte"),
|
119 |
+
outputs=gr.Textbox(label="Statut"),
|
120 |
+
title="Générateur de Présentations PowerPoint",
|
121 |
+
description="Entrez votre texte et obtenez une présentation PowerPoint générée automatiquement."
|
122 |
+
)
|
123 |
+
|
124 |
+
if __name__ == "__main__":
|
125 |
+
demo.launch()
|
126 |
+
|
127 |
+
|
128 |
+
|