|
|
|
|
|
|
|
|
|
import os |
|
import gradio as gr |
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
from pptx import Presentation |
|
from pptx.util import Inches, Pt |
|
import torch |
|
import time |
|
|
|
|
|
MODEL_PATH = "ibm-granite/granite-3.1-3b-a800m-Instruct" |
|
|
|
PREPROMPT = """Vous êtes un assistant IA expert en création de présentations PowerPoint professionnelles. |
|
Générez une présentation structurée et détaillée au format Markdown en suivant ce format EXACT: |
|
|
|
TITRE: [Titre principal de la présentation] |
|
|
|
DIAPO 1: |
|
Titre: [Titre de la diapo] |
|
Points: |
|
- Point 1 |
|
- Point 2 |
|
- Point 3 |
|
|
|
DIAPO 2: |
|
Titre: [Titre de la diapo] |
|
Points: |
|
- Point 1 |
|
- Point 2 |
|
- Point 3 |
|
|
|
[Continuez avec ce format pour chaque diapositive] |
|
|
|
Analysez le texte suivant et créez une présentation professionnelle :""" |
|
|
|
class ExecutionTimer: |
|
def __init__(self): |
|
self.start_time = None |
|
self.last_duration = None |
|
|
|
def start(self): |
|
self.start_time = time.time() |
|
|
|
def get_elapsed(self): |
|
if self.start_time is None: |
|
return 0 |
|
return time.time() - self.start_time |
|
|
|
def stop(self): |
|
if self.start_time is not None: |
|
self.last_duration = self.get_elapsed() |
|
self.start_time = None |
|
return self.last_duration |
|
|
|
def get_status(self): |
|
if self.start_time is not None: |
|
current = self.get_elapsed() |
|
last = f" (précédent: {self.last_duration:.2f}s)" if self.last_duration else "" |
|
return f"En cours... {current:.2f}s{last}" |
|
elif self.last_duration: |
|
return f"Terminé en {self.last_duration:.2f}s" |
|
return "En attente..." |
|
|
|
class PresentationGenerator: |
|
def __init__(self): |
|
print("Initialisation du modèle Granite...") |
|
self.tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH) |
|
self.model = AutoModelForCausalLM.from_pretrained( |
|
MODEL_PATH, |
|
torch_dtype=torch.float32, |
|
device_map="auto" |
|
) |
|
self.model.eval() |
|
print("Modèle initialisé avec succès!") |
|
|
|
def generate_text(self, prompt, temperature=0.7, max_tokens=2048): |
|
try: |
|
chat = [{"role": "user", "content": prompt}] |
|
formatted_prompt = self.tokenizer.apply_chat_template( |
|
chat, |
|
tokenize=False, |
|
add_generation_prompt=True |
|
) |
|
|
|
inputs = self.tokenizer( |
|
formatted_prompt, |
|
return_tensors="pt", |
|
truncation=True, |
|
max_length=4096 |
|
).to(self.model.device) |
|
|
|
with torch.no_grad(): |
|
outputs = self.model.generate( |
|
**inputs, |
|
max_new_tokens=max_tokens, |
|
temperature=temperature, |
|
do_sample=True, |
|
pad_token_id=self.tokenizer.eos_token_id |
|
) |
|
|
|
return self.tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
|
except Exception as e: |
|
print(f"Erreur lors de la génération: {str(e)}") |
|
raise |
|
|
|
def parse_presentation_content(self, content): |
|
slides = [] |
|
current_slide = None |
|
|
|
for line in content.split('\n'): |
|
line = line.strip() |
|
if line.startswith('TITRE:'): |
|
slides.append({'type': 'title', 'title': line[6:].strip()}) |
|
elif line.startswith('DIAPO'): |
|
if current_slide: |
|
slides.append(current_slide) |
|
current_slide = {'type': 'content', 'title': '', 'points': []} |
|
elif line.startswith('Titre:') and current_slide: |
|
current_slide['title'] = line[6:].strip() |
|
elif line.startswith('- ') and current_slide: |
|
current_slide['points'].append(line[2:].strip()) |
|
|
|
if current_slide: |
|
slides.append(current_slide) |
|
|
|
return slides |
|
|
|
def create_presentation(self, slides): |
|
prs = Presentation() |
|
|
|
title_slide = prs.slides.add_slide(prs.slide_layouts[0]) |
|
title_slide.shapes.title.text = slides[0]['title'] |
|
|
|
for slide in slides[1:]: |
|
content_slide = prs.slides.add_slide(prs.slide_layouts[1]) |
|
content_slide.shapes.title.text = slide['title'] |
|
|
|
if slide['points']: |
|
body = content_slide.shapes.placeholders[1].text_frame |
|
body.clear() |
|
for point in slide['points']: |
|
p = body.add_paragraph() |
|
p.text = point |
|
p.level = 0 |
|
|
|
return prs |
|
|
|
|
|
timer = ExecutionTimer() |
|
|
|
def generate_skeleton(text, temperature, max_tokens): |
|
"""Génère le squelette de la présentation""" |
|
try: |
|
timer.start() |
|
generator = PresentationGenerator() |
|
|
|
full_prompt = PREPROMPT + "\n\n" + text |
|
generated_content = generator.generate_text(full_prompt, temperature, max_tokens) |
|
|
|
status = timer.get_status() |
|
timer.stop() |
|
|
|
return status, generated_content, gr.update(visible=True) |
|
|
|
except Exception as e: |
|
timer.stop() |
|
error_msg = f"Erreur: {str(e)}" |
|
print(error_msg) |
|
return error_msg, None, gr.update(visible=False) |
|
|
|
def create_presentation_file(generated_content): |
|
"""Crée le fichier PowerPoint à partir du contenu généré""" |
|
try: |
|
timer.start() |
|
generator = PresentationGenerator() |
|
|
|
slides = generator.parse_presentation_content(generated_content) |
|
prs = generator.create_presentation(slides) |
|
|
|
output_path = os.path.join(os.getcwd(), "presentation.pptx") |
|
prs.save(output_path) |
|
|
|
timer.stop() |
|
return output_path |
|
|
|
except Exception as e: |
|
timer.stop() |
|
print(f"Erreur lors de la création du fichier: {str(e)}") |
|
return None |
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Glass()) as demo: |
|
gr.Markdown( |
|
""" |
|
# Générateur de Présentations PowerPoint IA |
|
|
|
Créez des présentations professionnelles automatiquement avec l'aide de l'IA. |
|
""" |
|
) |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
temperature = gr.Slider( |
|
minimum=0.1, |
|
maximum=1.0, |
|
value=0.7, |
|
step=0.1, |
|
label="Température" |
|
) |
|
max_tokens = gr.Slider( |
|
minimum=1000, |
|
maximum=4096, |
|
value=2048, |
|
step=256, |
|
label="Tokens maximum" |
|
) |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=2): |
|
input_text = gr.Textbox( |
|
lines=10, |
|
label="Votre texte", |
|
placeholder="Décrivez le contenu que vous souhaitez pour votre présentation..." |
|
) |
|
|
|
with gr.Row(): |
|
generate_skeleton_btn = gr.Button("Générer le Squelette de la Présentation", variant="primary") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
status_output = gr.Textbox( |
|
label="Statut", |
|
lines=2, |
|
value="En attente..." |
|
) |
|
generated_content = gr.Textbox( |
|
label="Contenu généré", |
|
lines=10, |
|
show_copy_button=True |
|
) |
|
create_presentation_btn = gr.Button("Créer Présentation", visible=False) |
|
output_file = gr.File( |
|
label="Présentation PowerPoint", |
|
type="filepath" |
|
) |
|
|
|
generate_skeleton_btn.click( |
|
fn=generate_skeleton, |
|
inputs=[ |
|
input_text, |
|
temperature, |
|
max_tokens |
|
], |
|
outputs=[ |
|
status_output, |
|
generated_content, |
|
create_presentation_btn |
|
] |
|
) |
|
|
|
create_presentation_btn.click( |
|
fn=create_presentation_file, |
|
inputs=[generated_content], |
|
outputs=[output_file] |
|
) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|