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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#https://huggingface.co/spaces/MisterAI/GenDoc_05
|
| 2 |
+
#app.py_145
|
| 3 |
+
#Separation Du Code
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 8 |
+
import torch
|
| 9 |
+
import time
|
| 10 |
+
from llm.list_llm import TEXT_MODELS, IMAGE_MODELS
|
| 11 |
+
from llm.prompt_llm import PREPROMPT
|
| 12 |
+
from python_pptx.python_pptx import PresentationGenerator
|
| 13 |
+
|
| 14 |
+
# Configuration du modèle par défaut
|
| 15 |
+
DEFAULT_MODEL = "ibm-granite/granite-3.1-3b-a800m-Instruct"
|
| 16 |
+
|
| 17 |
+
class ExecutionTimer:
|
| 18 |
+
def __init__(self):
|
| 19 |
+
self.start_time = None
|
| 20 |
+
self.last_duration = None
|
| 21 |
+
|
| 22 |
+
def start(self):
|
| 23 |
+
self.start_time = time.time()
|
| 24 |
+
|
| 25 |
+
def get_elapsed(self):
|
| 26 |
+
if self.start_time is None:
|
| 27 |
+
return 0
|
| 28 |
+
return time.time() - self.start_time
|
| 29 |
+
|
| 30 |
+
def stop(self):
|
| 31 |
+
if self.start_time is not None:
|
| 32 |
+
self.last_duration = self.get_elapsed()
|
| 33 |
+
self.start_time = None
|
| 34 |
+
return self.last_duration
|
| 35 |
+
|
| 36 |
+
def get_status(self):
|
| 37 |
+
if self.start_time is not None:
|
| 38 |
+
current = self.get_elapsed()
|
| 39 |
+
last = f" (précédent: {self.last_duration:.2f}s)" if self.last_duration else ""
|
| 40 |
+
return f"En cours... {current:.2f}s{last}"
|
| 41 |
+
elif self.last_duration:
|
| 42 |
+
return f"Terminé en {self.last_duration:.2f}s"
|
| 43 |
+
return "En attente..."
|
| 44 |
+
|
| 45 |
+
def generate_text(model_path, prompt, temperature=0.7, max_tokens=2048):
|
| 46 |
+
try:
|
| 47 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 48 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 49 |
+
model_path,
|
| 50 |
+
torch_dtype=torch.float32,
|
| 51 |
+
device_map="auto"
|
| 52 |
+
)
|
| 53 |
+
model.eval()
|
| 54 |
+
|
| 55 |
+
chat = [{"role": "user", "content": prompt}]
|
| 56 |
+
formatted_prompt = tokenizer.apply_chat_template(
|
| 57 |
+
chat,
|
| 58 |
+
tokenize=False,
|
| 59 |
+
add_generation_prompt=True
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
inputs = tokenizer(
|
| 63 |
+
formatted_prompt,
|
| 64 |
+
return_tensors="pt",
|
| 65 |
+
truncation=True,
|
| 66 |
+
max_length=4096
|
| 67 |
+
).to(model.device)
|
| 68 |
+
|
| 69 |
+
with torch.no_grad():
|
| 70 |
+
outputs = model.generate(
|
| 71 |
+
**inputs,
|
| 72 |
+
max_new_tokens=max_tokens,
|
| 73 |
+
temperature=temperature,
|
| 74 |
+
do_sample=True,
|
| 75 |
+
pad_token_id=tokenizer.eos_token_id
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 79 |
+
|
| 80 |
+
except Exception as e:
|
| 81 |
+
print(f"Erreur lors de la génération: {str(e)}")
|
| 82 |
+
raise
|
| 83 |
+
|
| 84 |
+
def generate_skeleton(model_name, text, temperature, max_tokens):
|
| 85 |
+
"""Génère le squelette de la présentation"""
|
| 86 |
+
try:
|
| 87 |
+
timer.start()
|
| 88 |
+
|
| 89 |
+
model_path = TEXT_MODELS.get(model_name, DEFAULT_MODEL)
|
| 90 |
+
full_prompt = PREPROMPT + "\n\n" + text
|
| 91 |
+
generated_content = generate_text(model_path, full_prompt, temperature, max_tokens)
|
| 92 |
+
|
| 93 |
+
status = timer.get_status()
|
| 94 |
+
timer.stop()
|
| 95 |
+
|
| 96 |
+
return status, generated_content, gr.update(visible=True)
|
| 97 |
+
|
| 98 |
+
except Exception as e:
|
| 99 |
+
timer.stop()
|
| 100 |
+
error_msg = f"Erreur: {str(e)}"
|
| 101 |
+
print(error_msg)
|
| 102 |
+
return error_msg, None, gr.update(visible=False)
|
| 103 |
+
|
| 104 |
+
def create_presentation_file(generated_content):
|
| 105 |
+
"""Crée le fichier PowerPoint à partir du contenu généré"""
|
| 106 |
+
try:
|
| 107 |
+
timer.start()
|
| 108 |
+
generator = PresentationGenerator()
|
| 109 |
+
|
| 110 |
+
slides = generator.parse_presentation_content(generated_content)
|
| 111 |
+
prs = generator.create_presentation(slides)
|
| 112 |
+
|
| 113 |
+
output_path = os.path.join(os.getcwd(), "presentation.pptx")
|
| 114 |
+
prs.save(output_path)
|
| 115 |
+
|
| 116 |
+
timer.stop()
|
| 117 |
+
return output_path
|
| 118 |
+
|
| 119 |
+
except Exception as e:
|
| 120 |
+
timer.stop()
|
| 121 |
+
print(f"Erreur lors de la création du fichier: {str(e)}")
|
| 122 |
+
return None
|
| 123 |
+
|
| 124 |
+
# Timer global pour le suivi du temps
|
| 125 |
+
timer = ExecutionTimer()
|
| 126 |
+
|
| 127 |
+
# Interface Gradio
|
| 128 |
+
with gr.Blocks(theme=gr.themes.Glass()) as demo:
|
| 129 |
+
gr.Markdown(
|
| 130 |
+
"""
|
| 131 |
+
# Générateur de Présentations PowerPoint IA
|
| 132 |
+
|
| 133 |
+
Créez des présentations professionnelles automatiquement avec l'aide de l'IA.
|
| 134 |
+
"""
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
with gr.Row():
|
| 138 |
+
with gr.Column(scale=1):
|
| 139 |
+
model_selector = gr.Dropdown(
|
| 140 |
+
choices=list(TEXT_MODELS.keys()) if TEXT_MODELS else ["Granite"],
|
| 141 |
+
value="Granite" if not TEXT_MODELS else list(TEXT_MODELS.keys())[0],
|
| 142 |
+
label="Modèle de texte"
|
| 143 |
+
)
|
| 144 |
+
temperature = gr.Slider(
|
| 145 |
+
minimum=0.1,
|
| 146 |
+
maximum=1.0,
|
| 147 |
+
value=0.7,
|
| 148 |
+
step=0.1,
|
| 149 |
+
label="Température"
|
| 150 |
+
)
|
| 151 |
+
max_tokens = gr.Slider(
|
| 152 |
+
minimum=1000,
|
| 153 |
+
maximum=4096,
|
| 154 |
+
value=2048,
|
| 155 |
+
step=256,
|
| 156 |
+
label="Tokens maximum"
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
with gr.Row():
|
| 160 |
+
with gr.Column(scale=2):
|
| 161 |
+
input_text = gr.Textbox(
|
| 162 |
+
lines=10,
|
| 163 |
+
label="Votre texte",
|
| 164 |
+
placeholder="Décrivez le contenu que vous souhaitez pour votre présentation..."
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
with gr.Row():
|
| 168 |
+
generate_skeleton_btn = gr.Button("Générer le Squelette de la Présentation", variant="primary")
|
| 169 |
+
|
| 170 |
+
with gr.Row():
|
| 171 |
+
with gr.Column():
|
| 172 |
+
status_output = gr.Textbox(
|
| 173 |
+
label="Statut",
|
| 174 |
+
lines=2,
|
| 175 |
+
value="En attente..."
|
| 176 |
+
)
|
| 177 |
+
generated_content = gr.Textbox(
|
| 178 |
+
label="Contenu généré",
|
| 179 |
+
lines=10,
|
| 180 |
+
show_copy_button=True
|
| 181 |
+
)
|
| 182 |
+
create_presentation_btn = gr.Button("Créer Présentation", visible=False)
|
| 183 |
+
output_file = gr.File(
|
| 184 |
+
label="Présentation PowerPoint",
|
| 185 |
+
type="filepath"
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
generate_skeleton_btn.click(
|
| 189 |
+
fn=generate_skeleton,
|
| 190 |
+
inputs=[
|
| 191 |
+
model_selector,
|
| 192 |
+
input_text,
|
| 193 |
+
temperature,
|
| 194 |
+
max_tokens
|
| 195 |
+
],
|
| 196 |
+
outputs=[
|
| 197 |
+
status_output,
|
| 198 |
+
generated_content,
|
| 199 |
+
create_presentation_btn
|
| 200 |
+
]
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
create_presentation_btn.click(
|
| 204 |
+
fn=create_presentation_file,
|
| 205 |
+
inputs=[generated_content],
|
| 206 |
+
outputs=[output_file]
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
if __name__ == "__main__":
|
| 210 |
+
demo.launch()
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
|