Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from huggingface_hub import hf_hub_download
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
+
from pptx import Presentation
|
6 |
+
from pptx.util import Inches, Pt
|
7 |
+
from pptx.enum.text import PP_ALIGN
|
8 |
+
from huggingface_hub import login
|
9 |
+
import torch
|
10 |
+
|
11 |
+
# Préprompt amélioré pour une meilleure structuration
|
12 |
+
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:
|
13 |
+
|
14 |
+
TITRE: [Titre principal de la présentation]
|
15 |
+
DIAPO 1:
|
16 |
+
Titre: [Titre de la diapo]
|
17 |
+
Points:
|
18 |
+
- Point 1
|
19 |
+
- Point 2
|
20 |
+
- Point 3
|
21 |
+
DIAPO 2:
|
22 |
+
Titre: [Titre de la diapo]
|
23 |
+
Points:
|
24 |
+
- Point 1
|
25 |
+
- Point 2
|
26 |
+
- Point 3
|
27 |
+
[Continuez avec ce format pour chaque diapositive]
|
28 |
+
|
29 |
+
Analysez le texte suivant et créez une présentation claire et professionnelle :"""
|
30 |
+
|
31 |
+
# Authentification HuggingFace
|
32 |
+
token = os.getenv('Authentification_HF')
|
33 |
+
login(token)
|
34 |
+
model_id = "mistralai/Mistral-Nemo-Instruct-2407"
|
35 |
+
|
36 |
+
# Initialisation du modèle avec des paramètres de contexte plus grands
|
37 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
38 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto")
|
39 |
+
|
40 |
+
def parse_presentation_content(content):
|
41 |
+
"""Parse le contenu généré en sections pour les diapositives"""
|
42 |
+
slides = []
|
43 |
+
current_slide = None
|
44 |
+
|
45 |
+
for line in content.split('\n'):
|
46 |
+
line = line.strip()
|
47 |
+
if line.startswith('TITRE:'):
|
48 |
+
slides.append({'type': 'title', 'title': line[6:].strip()})
|
49 |
+
elif line.startswith('DIAPO'):
|
50 |
+
if current_slide:
|
51 |
+
slides.append(current_slide)
|
52 |
+
current_slide = {'type': 'content', 'title': '', 'points': []}
|
53 |
+
elif line.startswith('Titre:') and current_slide:
|
54 |
+
current_slide['title'] = line[6:].strip()
|
55 |
+
elif line.startswith('- ') and current_slide:
|
56 |
+
current_slide['points'].append(line[2:].strip())
|
57 |
+
|
58 |
+
if current_slide:
|
59 |
+
slides.append(current_slide)
|
60 |
+
|
61 |
+
return slides
|
62 |
+
|
63 |
+
def create_presentation(slides):
|
64 |
+
"""Crée la présentation PowerPoint à partir des sections parsées"""
|
65 |
+
prs = Presentation()
|
66 |
+
|
67 |
+
# Première diapo (titre)
|
68 |
+
title_slide = prs.slides.add_slide(prs.slide_layouts[0])
|
69 |
+
title_slide.shapes.title.text = slides[0]['title']
|
70 |
+
|
71 |
+
# Autres diapos
|
72 |
+
for slide in slides[1:]:
|
73 |
+
content_slide = prs.slides.add_slide(prs.slide_layouts[1])
|
74 |
+
content_slide.shapes.title.text = slide['title']
|
75 |
+
|
76 |
+
if slide['points']:
|
77 |
+
body = content_slide.shapes.placeholders[1].text_frame
|
78 |
+
body.clear()
|
79 |
+
for point in slide['points']:
|
80 |
+
p = body.add_paragraph()
|
81 |
+
p.text = point
|
82 |
+
p.level = 0
|
83 |
+
|
84 |
+
return prs
|
85 |
+
|
86 |
+
def generate_presentation(text):
|
87 |
+
# Ajout du préprompt au texte de l'utilisateur
|
88 |
+
full_prompt = PREPROMPT + "\n\n" + text
|
89 |
+
|
90 |
+
# Génération du contenu avec le modèle
|
91 |
+
inputs = tokenizer.apply_chat_template(
|
92 |
+
[{"role": "user", "content": full_prompt}],
|
93 |
+
return_tensors="pt",
|
94 |
+
return_dict=True
|
95 |
+
)
|
96 |
+
|
97 |
+
# Suppression du paramètre 'stop' qui n'est pas supporté
|
98 |
+
outputs = model.generate(
|
99 |
+
**inputs,
|
100 |
+
max_new_tokens=4096,
|
101 |
+
temperature=0.3
|
102 |
+
)
|
103 |
+
|
104 |
+
# Extraction du texte généré
|
105 |
+
generated_content = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
106 |
+
|
107 |
+
# Parse le contenu et crée la présentation
|
108 |
+
slides = parse_presentation_content(generated_content)
|
109 |
+
prs = create_presentation(slides)
|
110 |
+
|
111 |
+
# Sauvegarde de la présentation
|
112 |
+
output_path = "presentation.pptx"
|
113 |
+
prs.save(output_path)
|
114 |
+
|
115 |
+
return f"Présentation générée avec succès ! Vous pouvez la télécharger ici : {os.path.abspath(output_path)}"
|
116 |
+
|
117 |
+
# Interface Gradio avec thème sombre personnalisé
|
118 |
+
css = """
|
119 |
+
.gradio-container {
|
120 |
+
background-color: #1a1a1a !important;
|
121 |
+
color: #ffffff !important;
|
122 |
+
}
|
123 |
+
.input-textbox, .output-textbox {
|
124 |
+
background-color: #2d2d2d !important;
|
125 |
+
color: #ffffff !important;
|
126 |
+
border: 1px solid #404040 !important;
|
127 |
+
}
|
128 |
+
.label-text {
|
129 |
+
color: #ffffff !important;
|
130 |
+
}
|
131 |
+
"""
|
132 |
+
|
133 |
+
# Interface Gradio avec une zone de texte plus grande et thème sombre
|
134 |
+
demo = gr.Interface(
|
135 |
+
fn=generate_presentation,
|
136 |
+
inputs=gr.Textbox(
|
137 |
+
lines=10,
|
138 |
+
label="Entrez votre texte",
|
139 |
+
max_lines=50,
|
140 |
+
placeholder="Décrivez le contenu que vous souhaitez pour votre présentation...",
|
141 |
+
),
|
142 |
+
outputs=gr.Textbox(label="Statut"),
|
143 |
+
title="Générateur de Présentations PowerPoint",
|
144 |
+
description="Entrez votre texte et obtenez une présentation PowerPoint générée automatiquement.",
|
145 |
+
theme="darkhuggingface", # Utilisation du thème sombre de HuggingFace
|
146 |
+
css=css # Application du CSS personnalisé
|
147 |
+
)
|
148 |
+
|
149 |
+
if __name__ == "__main__":
|
150 |
+
demo.launch()
|