Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,49 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
def
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Chargement des pipelines (en lazy loading)
|
| 5 |
+
pipelines = {
|
| 6 |
+
"Génération de texte": pipeline("text-generation", model="openai-community/gpt2"),
|
| 7 |
+
"Traduction anglais → français": pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr"),
|
| 8 |
+
"Résumé de texte": pipeline("summarization", model="facebook/bart-large-cnn")
|
| 9 |
+
}
|
| 10 |
|
| 11 |
+
def run_model(task, text):
|
| 12 |
+
if not text.strip():
|
| 13 |
+
return "Veuillez entrer un texte."
|
| 14 |
|
| 15 |
+
if task == "Génération de texte":
|
| 16 |
+
result = pipelines[task](text, max_new_tokens=80)
|
| 17 |
+
return result[0]["generated_text"]
|
| 18 |
+
|
| 19 |
+
elif task == "Traduction anglais → français":
|
| 20 |
+
result = pipelines[task](text)
|
| 21 |
+
return result[0]["translation_text"]
|
| 22 |
+
|
| 23 |
+
elif task == "Résumé de texte":
|
| 24 |
+
result = pipelines[task](text, max_length=80, min_length=20, truncation=True)
|
| 25 |
+
return result[0]["summary_text"]
|
| 26 |
+
|
| 27 |
+
demo = gr.Interface(
|
| 28 |
+
fn=run_model,
|
| 29 |
+
inputs=[
|
| 30 |
+
gr.Dropdown(
|
| 31 |
+
choices=[
|
| 32 |
+
"Génération de texte",
|
| 33 |
+
"Traduction de texte en anglais en français",
|
| 34 |
+
"Résumé de texte"
|
| 35 |
+
],
|
| 36 |
+
label="Choisissez une tâche"
|
| 37 |
+
),
|
| 38 |
+
gr.Textbox(
|
| 39 |
+
lines=5,
|
| 40 |
+
placeholder="Entrez votre texte ou votre prompt ici..."
|
| 41 |
+
)
|
| 42 |
+
],
|
| 43 |
+
outputs=gr.Textbox(label="Résultat"),
|
| 44 |
+
title="Démo Hugging Face — GPT2, Traduction & Résumé",
|
| 45 |
+
description="Une démonstration interactive utilisant des modèles de Hugging Face."
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
demo.launch()
|