Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# Charger le modèle et le tokenizer
|
6 |
+
language = "french"
|
7 |
+
model_name = "Moustapha91/bart_large_poetique-v02"
|
8 |
+
|
9 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
+
|
12 |
+
# Fonction de génération ajustée
|
13 |
+
def generate_summary(input_text):
|
14 |
+
inputs = tokenizer(
|
15 |
+
input_text,
|
16 |
+
padding="max_length",
|
17 |
+
max_length=512, # Ajustez si nécessaire
|
18 |
+
return_tensors="pt",
|
19 |
+
truncation=True,
|
20 |
+
)
|
21 |
+
input_ids = inputs.input_ids.to(model.device)
|
22 |
+
attention_mask = inputs.attention_mask.to(model.device)
|
23 |
+
|
24 |
+
# Génération avec des paramètres personnalisés
|
25 |
+
outputs = model.generate(
|
26 |
+
input_ids=input_ids,
|
27 |
+
attention_mask=attention_mask,
|
28 |
+
max_length=512,
|
29 |
+
num_beams=4,
|
30 |
+
temperature=1.0,
|
31 |
+
early_stopping=True,
|
32 |
+
repetition_penalty=6.0,
|
33 |
+
no_repeat_ngram_size=5,
|
34 |
+
)
|
35 |
+
output_str = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
36 |
+
return output_str
|
37 |
+
|
38 |
+
# Exemple d'utilisation (test rapide)
|
39 |
+
example_text = "L'amour, cet élan mystérieux qui unit les âmes, est une lumière douce dans les ténèbres du quotidien..."
|
40 |
+
|
41 |
+
# Interface Gradio
|
42 |
+
interface = gr.Interface(
|
43 |
+
fn=generate_summary,
|
44 |
+
inputs=gr.Textbox(
|
45 |
+
lines=10,
|
46 |
+
placeholder="Entrez un texte à résumer ou transformer en poème..."
|
47 |
+
),
|
48 |
+
outputs=gr.Textbox(
|
49 |
+
label="Texte généré"
|
50 |
+
),
|
51 |
+
examples=[[example_text]],
|
52 |
+
title="Génération de texte poétique ou résumé",
|
53 |
+
description=(
|
54 |
+
"Cette application utilise un modèle pré-entraîné BART pour transformer "
|
55 |
+
"un texte d'entrée en une sortie poétique ou résumé. Entrez une phrase ou un paragraphe pour tester !"
|
56 |
+
),
|
57 |
+
)
|
58 |
+
|
59 |
+
# Lancer l'application
|
60 |
+
interface.launch()
|