GenDoc_05 / app.py.NOK_2025.02.09
MisterAI's picture
Rename app.py to app.py.NOK_2025.02.09
1a5f0d1 verified
raw
history blame
3.85 kB
import os
import gradio as gr
from transformers import pipeline
from pptx import Presentation
from pptx.util import Inches
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, TextIteratorStreamer
#from llama_cpp import Llama LLMCppModel
from llama_cpp import Llama
from huggingface_hub import hf_hub_download
# Préprompt pour donner des instructions au modèle
PREPROMPT = """Vous êtes un assistant IA chargé de générer une présentation PowerPoint à partir d'un texte fourni par un utilisateur. Voici les instructions à suivre :
- Analysez attentivement le texte pour en comprendre les idées principales et la structure.
- Générez des titres et sous-titres pertinents pour chaque diapositive.
- Résumez les points clés sous forme de listes à puces.
- Ajoutez du texte explicatif pour chaque diapositive afin de compléter le contenu.
- Assurez-vous que la présentation soit cohérente, logique et visuellement attractive.
Voici le texte à transformer en présentation :"""
# Chargement du modèle IA depuis Hugging Face
#text_to_presentation = pipeline("text-generation", model="MisterAI/Bartowski_MistralAI_Mistral-Small-24B-Base-2501-GGUF")
#tokenizer = AutoTokenizer.from_pretrained("MisterAI/Bartowski_MistralAI_Mistral-Small-24B-Base-2501-GGUF")
# Chargement du modèle IA depuis Hugging Face
#from llama_cpp import LLMCppModel
#model_path = "MisterAI/Bartowski_MistralAI_Mistral-Small-24B-Base-2501-GGUF"
#model_path = "https://huggingface.co/MisterAI/Bartowski_MistralAI_Mistral-Small-24B-Base-2501-GGUF/tree/main/mistralai_Mistral-Small-24B-Base-2501-Q8_0.gguf"
#model_path = "https://huggingface.co/MisterAI/Bartowski_MistralAI_Mistral-Small-24B-Base-2501-GGUF/resolve/main/mistralai_Mistral-Small-24B-Base-2501-Q8_0.gguf"
#model_path = "https://huggingface.co/MisterAI/Bartowski_MistralAI_Mistral-Small-24B-Base-2501-GGUF/mistralai_Mistral-Small-24B-Base-2501-Q8_0.gguf"
#model_path = "MisterAI/Bartowski_MistralAI_Mistral-Small-24B-Base-2501-GGUF/mistralai_Mistral-Small-24B-Base-2501-Q8_0.gguf"
# Téléchargement du modèle depuis Hugging Face
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 = LLMCppModel(model_path)
#text_to_presentation = model_path
text_to_presentation = Llama(model_path, verbose=True)
# Chargement du tokenizer depuis Hugging Face
#from transformers import AutoTokenizer
#tokenizer = AutoTokenizer.from_pretrained("MisterAI/Bartowski_MistralAI_Mistral-Small-24B-Base-2501-GGUF")
tokenizer = AutoTokenizer.from_pretrained(model_path)
def generate_presentation(text):
# Ajout du préprompt au texte de l'utilisateur
full_prompt = PREPROMPT + "\n\n" + text
# Analyse du texte avec le modèle IA
presentation_content = text_to_presentation(full_prompt, max_length=1500, num_return_sequences=1)[0]["generated_text"]
# Génération de la présentation PowerPoint
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[0])
title = slide.shapes.title
title.text = "Présentation générée"
body = slide.placeholders[1].text_frame
body.text = presentation_content
# Enregistrement de la présentation
prs.save("presentation.pptx")
# Retourner le lien de téléchargement
return "Votre présentation est prête ! Vous pouvez la télécharger ici : " + os.path.abspath("presentation.pptx")
# Interface Gradio
demo = gr.Interface(
fn=generate_presentation,
inputs="text",
outputs="text",
title="Générateur de présentations",
description="Entrez du texte et obtenez une présentation PowerPoint générée automatiquement."
)
if __name__ == "__main__":
demo.launch()