|
import os |
|
import gradio as gr |
|
from huggingface_hub import hf_hub_download |
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
from pptx import Presentation |
|
from pptx.util import Inches, Pt |
|
from pptx.enum.text import PP_ALIGN |
|
from huggingface_hub import login |
|
import torch |
|
|
|
|
|
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: |
|
|
|
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 claire et professionnelle :""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
token = os.getenv('Authentification_HF') |
|
login(token) |
|
model_id = "mistralai/Mistral-Nemo-Instruct-2407" |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained(model_id) |
|
|
|
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto") |
|
|
|
|
|
def parse_presentation_content(content): |
|
"""Parse le contenu généré en sections pour les diapositives""" |
|
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(slides): |
|
"""Crée la présentation PowerPoint à partir des sections parsées""" |
|
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 |
|
|
|
def generate_presentation(text): |
|
|
|
full_prompt = PREPROMPT + "\n\n" + text |
|
|
|
|
|
inputs = tokenizer.apply_chat_template( |
|
[{"role": "user", "content": full_prompt}], |
|
return_tensors="pt", |
|
return_dict=True |
|
) |
|
|
|
outputs = model.generate(**inputs, max_new_tokens=4096, temperature=0.3, stop=["<end>"]) |
|
|
|
|
|
generated_content = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
|
|
|
slides = parse_presentation_content(generated_content) |
|
prs = create_presentation(slides) |
|
|
|
|
|
output_path = "presentation.pptx" |
|
prs.save(output_path) |
|
|
|
return f"Présentation générée avec succès ! Vous pouvez la télécharger ici : {os.path.abspath(output_path)}" |
|
|
|
|
|
demo = gr.Interface( |
|
fn=generate_presentation, |
|
inputs=gr.Textbox( |
|
lines=10, |
|
label="Entrez votre texte", |
|
max_lines=50 |
|
), |
|
outputs=gr.Textbox(label="Statut"), |
|
title="Générateur de Présentations PowerPoint", |
|
description="Entrez votre texte et obtenez une présentation PowerPoint générée automatiquement." |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch(share=True) |
|
|
|
|
|
|
|
|