| | 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_content_and_pptx(text): |
| | """Fonction en deux étapes : génération du contenu puis création du PPTX""" |
| | |
| | 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 |
| | ) |
| | |
| | |
| | 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 generated_content, f"Présentation générée avec succès ! Vous pouvez la télécharger ici : {os.path.abspath(output_path)}" |
| |
|
| | |
| | css = """ |
| | /* Styles globaux */ |
| | .gradio-container { |
| | background-color: #000000 !important; |
| | } |
| | |
| | /* Titre et description */ |
| | h1, h2, h3, p, label, .label-text { |
| | color: #ffffff !important; |
| | } |
| | |
| | /* Zones de texte */ |
| | .gr-box, .gr-input, .gr-textarea, .gr-textbox, .gr-text-input { |
| | background-color: #000000 !important; |
| | border: 1px solid #666666 !important; |
| | color: #ffffff !important; |
| | } |
| | |
| | /* Boutons */ |
| | .gr-button { |
| | background-color: #333333 !important; |
| | color: #ffffff !important; |
| | border: 1px solid #666666 !important; |
| | } |
| | |
| | .gr-button:hover { |
| | background-color: #444444 !important; |
| | } |
| | |
| | /* Progress bar et éléments de statut */ |
| | .gr-progress, .gr-status { |
| | color: #ffffff !important; |
| | background-color: #000000 !important; |
| | } |
| | |
| | /* Conteneurs et boîtes */ |
| | .gr-panel, .gr-box { |
| | background-color: #000000 !important; |
| | border: 1px solid #666666 !important; |
| | } |
| | |
| | /* Texte de placeholder */ |
| | ::placeholder { |
| | color: #999999 !important; |
| | } |
| | |
| | /* Scrollbar personnalisée */ |
| | ::-webkit-scrollbar { |
| | width: 10px; |
| | background-color: #000000; |
| | } |
| | |
| | ::-webkit-scrollbar-thumb { |
| | background-color: #666666; |
| | border-radius: 5px; |
| | } |
| | |
| | ::-webkit-scrollbar-track { |
| | background-color: #333333; |
| | } |
| | """ |
| |
|
| | |
| | with gr.Blocks(theme=gr.themes.Default(), css=css) as demo: |
| | gr.Markdown( |
| | """ |
| | # Générateur de Présentations PowerPoint |
| | |
| | Entrez votre texte et obtenez une présentation PowerPoint générée automatiquement. |
| | """ |
| | ) |
| | |
| | with gr.Row(): |
| | with gr.Column(): |
| | input_text = gr.Textbox( |
| | lines=10, |
| | label="Entrez votre texte", |
| | placeholder="Décrivez le contenu que vous souhaitez pour votre présentation...", |
| | ) |
| | |
| | with gr.Row(): |
| | generate_btn = gr.Button("Générer la présentation") |
| | |
| | with gr.Row(): |
| | with gr.Column(): |
| | output_content = gr.Textbox( |
| | label="Contenu généré par l'IA", |
| | lines=10, |
| | show_copy_button=True |
| | ) |
| | output_status = gr.Textbox( |
| | label="Statut de la génération" |
| | ) |
| | |
| | generate_btn.click( |
| | fn=generate_content_and_pptx, |
| | inputs=input_text, |
| | outputs=[output_content, output_status] |
| | ) |
| |
|
| | if __name__ == "__main__": |
| | demo.launch() |