CedricSA commited on
Commit
404a931
·
verified ·
1 Parent(s): d860f5f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -6
app.py CHANGED
@@ -1,11 +1,49 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- generator = pipeline("text-generation", model="openai-community/gpt2")
 
 
 
 
 
5
 
6
- def generate_text(prompt):
7
- result = generator(prompt, max_new_tokens=50)
8
- return result[0]["generated_text"]
9
 
10
- demo = gr.Interface(fn=generate_text, inputs="text", outputs="text", title="Demo GPT-2")
11
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()