Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,35 @@
|
|
| 1 |
# Installer les bibliothèques nécessaires
|
| 2 |
-
# !pip install transformers
|
| 3 |
|
|
|
|
| 4 |
from transformers import pipeline
|
| 5 |
|
| 6 |
# Charger le pipeline de génération de texte
|
| 7 |
generator = pipeline("text-generation", model="gpt2")
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
prompt =
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
print(f"--- Generated Text {i + 1} ---")
|
| 18 |
-
print(generated_text["generated_text"])
|
| 19 |
-
print()
|
|
|
|
| 1 |
# Installer les bibliothèques nécessaires
|
| 2 |
+
# !pip install transformers gradio
|
| 3 |
|
| 4 |
+
import gradio as gr
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
# Charger le pipeline de génération de texte
|
| 8 |
generator = pipeline("text-generation", model="gpt2")
|
| 9 |
|
| 10 |
+
# Fonction pour générer du texte à partir d'un prompt
|
| 11 |
+
def generate_text(prompt, max_length=50, num_return_sequences=3):
|
| 12 |
+
generated_texts = generator(prompt, max_length=max_length, num_return_sequences=num_return_sequences)
|
| 13 |
+
results = [text["generated_text"] for text in generated_texts]
|
| 14 |
+
return "\n\n".join(results)
|
| 15 |
|
| 16 |
+
# Interface Gradio
|
| 17 |
+
with gr.Blocks() as demo:
|
| 18 |
+
gr.Markdown("# Génération de texte avec Hugging Face GPT-2")
|
| 19 |
+
|
| 20 |
+
with gr.Row():
|
| 21 |
+
prompt_input = gr.Textbox(label="Texte d'entrée (prompt)", lines=2, placeholder="Saisissez votre texte ici")
|
| 22 |
+
max_length_input = gr.Slider(label="Longueur maximale du texte", minimum=10, maximum=100, value=50)
|
| 23 |
+
num_sequences_input = gr.Slider(label="Nombre de textes générés", minimum=1, maximum=5, value=3)
|
| 24 |
+
|
| 25 |
+
generate_button = gr.Button("Générer du texte")
|
| 26 |
+
output_text = gr.Textbox(label="Résultat", lines=10)
|
| 27 |
+
|
| 28 |
+
generate_button.click(
|
| 29 |
+
generate_text,
|
| 30 |
+
inputs=[prompt_input, max_length_input, num_sequences_input],
|
| 31 |
+
outputs=output_text
|
| 32 |
+
)
|
| 33 |
|
| 34 |
+
# Lancer l'application
|
| 35 |
+
demo.launch()
|
|
|
|
|
|
|
|
|