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