|
import os |
|
import gradio as gr |
|
from huggingface_hub import hf_hub_download |
|
from llama_cpp import Llama |
|
from pptx import Presentation |
|
from pptx.util import Inches, Pt |
|
from pptx.enum.text import PP_ALIGN |
|
|
|
|
|
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 :""" |
|
|
|
|
|
model_file = "mistralai_Mistral-Small-24B-Base-2501-Q8_0.gguf" |
|
model_path = hf_hub_download( |
|
repo_id="MisterAI/Bartowski_MistralAI_Mistral-Small-24B-Base-2501-GGUF", |
|
filename=model_file, |
|
repo_type="model" |
|
) |
|
|
|
|
|
text_to_presentation = Llama( |
|
model_path=model_path, |
|
verbose=True, |
|
n_ctx=4096, |
|
n_batch=256 |
|
) |
|
|
|
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 |
|
|
|
|
|
response = text_to_presentation( |
|
full_prompt, |
|
max_tokens=4096, |
|
temperature=0.7, |
|
stop=["<end>"], |
|
echo=False |
|
) |
|
|
|
|
|
generated_content = response['choices'][0]['text'] |
|
|
|
|
|
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() |