Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
#
|
| 2 |
|
| 3 |
import os
|
| 4 |
import gradio as gr
|
|
@@ -11,20 +11,13 @@ import torch
|
|
| 11 |
from llama_cpp import Llama
|
| 12 |
import time
|
| 13 |
|
| 14 |
-
# Configuration des modèles
|
| 15 |
TEXT_MODELS = {
|
| 16 |
"Mistral Nemo 2407 (GGUF)": "MisterAI/Bartowski_MistralAI_Mistral-Nemo-Instruct-2407-IQ4_XS.gguf",
|
| 17 |
"Mixtral 8x7B": "mistralai/Mixtral-8x7B-v0.1",
|
| 18 |
"Lucie 7B": "OpenLLM-France/Lucie-7B"
|
| 19 |
}
|
| 20 |
|
| 21 |
-
# Commenté : Configuration des modèles d'images
|
| 22 |
-
# IMAGE_MODELS = {
|
| 23 |
-
# "FLUX.1": "black-forest-labs/FLUX.1-schnell",
|
| 24 |
-
# "ArtifyAI": "ImageInception/ArtifyAI-v1.1"
|
| 25 |
-
# }
|
| 26 |
-
|
| 27 |
-
# Préprompt modifié pour ne pas inclure la génération d'images
|
| 28 |
PREPROMPT = """Vous êtes un assistant IA expert en création de présentations PowerPoint professionnelles.
|
| 29 |
Générez une présentation structurée et détaillée en suivant ce format EXACT:
|
| 30 |
|
|
@@ -56,13 +49,11 @@ class PresentationGenerator:
|
|
| 56 |
login(self.token)
|
| 57 |
self.text_model = None
|
| 58 |
self.text_tokenizer = None
|
| 59 |
-
# Commenté : self.image_pipeline = None
|
| 60 |
|
| 61 |
def load_text_model(self, model_name):
|
| 62 |
"""Charge le modèle de génération de texte"""
|
| 63 |
model_id = TEXT_MODELS[model_name]
|
| 64 |
if model_id.endswith('.gguf'):
|
| 65 |
-
# Configuration pour les modèles GGUF
|
| 66 |
model_path = hf_hub_download(
|
| 67 |
repo_id=model_id.split('/')[0] + '/' + model_id.split('/')[1],
|
| 68 |
filename=model_id.split('/')[-1],
|
|
@@ -76,7 +67,6 @@ class PresentationGenerator:
|
|
| 76 |
)
|
| 77 |
print(f"Modèle GGUF {model_id} chargé avec succès!")
|
| 78 |
else:
|
| 79 |
-
# Configuration pour les modèles Transformers standards
|
| 80 |
self.text_tokenizer = AutoTokenizer.from_pretrained(model_id, token=self.token)
|
| 81 |
self.text_model = AutoModelForCausalLM.from_pretrained(
|
| 82 |
model_id,
|
|
@@ -86,13 +76,6 @@ class PresentationGenerator:
|
|
| 86 |
)
|
| 87 |
print(f"Modèle Transformers {model_id} chargé avec succès!")
|
| 88 |
|
| 89 |
-
# Commenté : Fonction de chargement du modèle d'images
|
| 90 |
-
# def load_image_model(self, model_name):
|
| 91 |
-
# """Charge le modèle de génération d'images"""
|
| 92 |
-
# model_id = IMAGE_MODELS[model_name]
|
| 93 |
-
# self.image_pipeline = gr.load("models/black-forest-labs/FLUX.1-schnell")
|
| 94 |
-
# print(f"Modèle d'image FLUX chargé : {model_id}")
|
| 95 |
-
|
| 96 |
def generate_text(self, prompt, temperature=0.7, max_tokens=4096):
|
| 97 |
"""Génère le texte de la présentation"""
|
| 98 |
if isinstance(self.text_model, Llama):
|
|
@@ -119,20 +102,6 @@ class PresentationGenerator:
|
|
| 119 |
print("Texte généré par Transformers :", generated_text)
|
| 120 |
return generated_text
|
| 121 |
|
| 122 |
-
# Commenté : Fonction de génération d'images
|
| 123 |
-
# def generate_image(self, prompt, negative_prompt="", num_inference_steps=30):
|
| 124 |
-
# """Génère une image pour la diapositive"""
|
| 125 |
-
# try:
|
| 126 |
-
# image = self.image_pipeline(
|
| 127 |
-
# prompt=prompt,
|
| 128 |
-
# negative_prompt=negative_prompt,
|
| 129 |
-
# num_inference_steps=num_inference_steps
|
| 130 |
-
# )[0]
|
| 131 |
-
# return image
|
| 132 |
-
# except Exception as e:
|
| 133 |
-
# print(f"Erreur lors de la génération de l'image: {str(e)}")
|
| 134 |
-
# return None
|
| 135 |
-
|
| 136 |
def parse_presentation_content(self, content):
|
| 137 |
"""Parse le contenu généré en sections pour les diapositives"""
|
| 138 |
slides = []
|
|
@@ -150,9 +119,6 @@ class PresentationGenerator:
|
|
| 150 |
current_slide['title'] = line[6:].strip()
|
| 151 |
elif line.startswith('- ') and current_slide:
|
| 152 |
current_slide['points'].append(line[2:].strip())
|
| 153 |
-
# Commenté : Parsing des prompts d'images
|
| 154 |
-
# elif line.startswith('Image:') and current_slide:
|
| 155 |
-
# current_slide['image_prompt'] = line[6:].strip()
|
| 156 |
|
| 157 |
if current_slide:
|
| 158 |
slides.append(current_slide)
|
|
@@ -181,17 +147,6 @@ class PresentationGenerator:
|
|
| 181 |
p.text = point
|
| 182 |
p.level = 0
|
| 183 |
|
| 184 |
-
# Commenté : Ajout des images
|
| 185 |
-
# if slide.get('image_prompt'):
|
| 186 |
-
# image = self.generate_image(slide['image_prompt'])
|
| 187 |
-
# if image:
|
| 188 |
-
# img_path = f"temp_slide_{slides.index(slide)}.png"
|
| 189 |
-
# image.save(img_path)
|
| 190 |
-
# left = Inches(1)
|
| 191 |
-
# top = Inches(2.5)
|
| 192 |
-
# content_slide.shapes.add_picture(img_path, left, top, height=Inches(4))
|
| 193 |
-
# os.remove(img_path)
|
| 194 |
-
|
| 195 |
return prs
|
| 196 |
|
| 197 |
def generate_presentation_with_progress(text, text_model_name, temperature, max_tokens):
|
|
@@ -214,19 +169,25 @@ def generate_presentation_with_progress(text, text_model_name, temperature, max_
|
|
| 214 |
slides = generator.parse_presentation_content(generated_content)
|
| 215 |
prs = generator.create_presentation(slides)
|
| 216 |
|
| 217 |
-
# Sauvegarde
|
| 218 |
-
output_path = "presentation.pptx"
|
| 219 |
prs.save(output_path)
|
| 220 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
execution_time = time.time() - start_time
|
| 222 |
status = f"Présentation générée avec succès en {execution_time:.2f} secondes!"
|
| 223 |
|
| 224 |
-
|
|
|
|
| 225 |
|
| 226 |
except Exception as e:
|
|
|
|
| 227 |
return f"Erreur: {str(e)}", None, None
|
| 228 |
|
| 229 |
-
# CSS
|
| 230 |
css = """
|
| 231 |
/* Thème sombre personnalisé */
|
| 232 |
.gradio-container {
|
|
@@ -322,12 +283,6 @@ with gr.Blocks(theme=gr.themes.Default(), css=css) as demo:
|
|
| 322 |
label="Votre texte",
|
| 323 |
placeholder="Décrivez le contenu que vous souhaitez pour votre présentation..."
|
| 324 |
)
|
| 325 |
-
# Commenté : Upload de fichiers
|
| 326 |
-
# file_upload = gr.File(
|
| 327 |
-
# label="Documents de référence (PDF, Images)",
|
| 328 |
-
# file_types=["pdf", "png", "jpg", "jpeg"],
|
| 329 |
-
# type="binary"
|
| 330 |
-
# )
|
| 331 |
|
| 332 |
with gr.Row():
|
| 333 |
generate_btn = gr.Button("🚀 Générer la présentation", variant="primary")
|
|
|
|
| 1 |
+
#142
|
| 2 |
|
| 3 |
import os
|
| 4 |
import gradio as gr
|
|
|
|
| 11 |
from llama_cpp import Llama
|
| 12 |
import time
|
| 13 |
|
| 14 |
+
# [Configuration des modèles et PREPROMPT restent identiques à C141]
|
| 15 |
TEXT_MODELS = {
|
| 16 |
"Mistral Nemo 2407 (GGUF)": "MisterAI/Bartowski_MistralAI_Mistral-Nemo-Instruct-2407-IQ4_XS.gguf",
|
| 17 |
"Mixtral 8x7B": "mistralai/Mixtral-8x7B-v0.1",
|
| 18 |
"Lucie 7B": "OpenLLM-France/Lucie-7B"
|
| 19 |
}
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
PREPROMPT = """Vous êtes un assistant IA expert en création de présentations PowerPoint professionnelles.
|
| 22 |
Générez une présentation structurée et détaillée en suivant ce format EXACT:
|
| 23 |
|
|
|
|
| 49 |
login(self.token)
|
| 50 |
self.text_model = None
|
| 51 |
self.text_tokenizer = None
|
|
|
|
| 52 |
|
| 53 |
def load_text_model(self, model_name):
|
| 54 |
"""Charge le modèle de génération de texte"""
|
| 55 |
model_id = TEXT_MODELS[model_name]
|
| 56 |
if model_id.endswith('.gguf'):
|
|
|
|
| 57 |
model_path = hf_hub_download(
|
| 58 |
repo_id=model_id.split('/')[0] + '/' + model_id.split('/')[1],
|
| 59 |
filename=model_id.split('/')[-1],
|
|
|
|
| 67 |
)
|
| 68 |
print(f"Modèle GGUF {model_id} chargé avec succès!")
|
| 69 |
else:
|
|
|
|
| 70 |
self.text_tokenizer = AutoTokenizer.from_pretrained(model_id, token=self.token)
|
| 71 |
self.text_model = AutoModelForCausalLM.from_pretrained(
|
| 72 |
model_id,
|
|
|
|
| 76 |
)
|
| 77 |
print(f"Modèle Transformers {model_id} chargé avec succès!")
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
def generate_text(self, prompt, temperature=0.7, max_tokens=4096):
|
| 80 |
"""Génère le texte de la présentation"""
|
| 81 |
if isinstance(self.text_model, Llama):
|
|
|
|
| 102 |
print("Texte généré par Transformers :", generated_text)
|
| 103 |
return generated_text
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
def parse_presentation_content(self, content):
|
| 106 |
"""Parse le contenu généré en sections pour les diapositives"""
|
| 107 |
slides = []
|
|
|
|
| 119 |
current_slide['title'] = line[6:].strip()
|
| 120 |
elif line.startswith('- ') and current_slide:
|
| 121 |
current_slide['points'].append(line[2:].strip())
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
if current_slide:
|
| 124 |
slides.append(current_slide)
|
|
|
|
| 147 |
p.text = point
|
| 148 |
p.level = 0
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
return prs
|
| 151 |
|
| 152 |
def generate_presentation_with_progress(text, text_model_name, temperature, max_tokens):
|
|
|
|
| 169 |
slides = generator.parse_presentation_content(generated_content)
|
| 170 |
prs = generator.create_presentation(slides)
|
| 171 |
|
| 172 |
+
# Sauvegarde avec chemin absolu
|
| 173 |
+
output_path = os.path.abspath("presentation.pptx")
|
| 174 |
prs.save(output_path)
|
| 175 |
|
| 176 |
+
# Vérification que le fichier existe
|
| 177 |
+
if not os.path.exists(output_path):
|
| 178 |
+
raise FileNotFoundError(f"Le fichier {output_path} n'a pas été créé correctement")
|
| 179 |
+
|
| 180 |
execution_time = time.time() - start_time
|
| 181 |
status = f"Présentation générée avec succès en {execution_time:.2f} secondes!"
|
| 182 |
|
| 183 |
+
# Retourne le statut, le contenu généré et le fichier
|
| 184 |
+
return status, generated_content, (output_path, "presentation.pptx")
|
| 185 |
|
| 186 |
except Exception as e:
|
| 187 |
+
print(f"Erreur lors de la génération: {str(e)}")
|
| 188 |
return f"Erreur: {str(e)}", None, None
|
| 189 |
|
| 190 |
+
# [Le reste du code (CSS et interface Gradio) reste identique à C141]
|
| 191 |
css = """
|
| 192 |
/* Thème sombre personnalisé */
|
| 193 |
.gradio-container {
|
|
|
|
| 283 |
label="Votre texte",
|
| 284 |
placeholder="Décrivez le contenu que vous souhaitez pour votre présentation..."
|
| 285 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
|
| 287 |
with gr.Row():
|
| 288 |
generate_btn = gr.Button("🚀 Générer la présentation", variant="primary")
|