Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import pipeline | |
| text_generator = pipeline("text-generation", model="openai-community/gpt2") | |
| translator = pipeline("translation_en_to_fr", model="Helsinki-NLP/opus-mt-en-fr") | |
| summarizer = pipeline("summarization", model="facebook/bart-large-cnn") | |
| def generate_text(prompt): | |
| try: | |
| result = text_generator(prompt, max_new_tokens=60, num_return_sequences=1) | |
| return result[0]["generated_text"] | |
| except Exception as e: | |
| return f"Error : {str(e)}" | |
| def translate_text(text): | |
| try: | |
| result = translator(text) | |
| return result[0]["translation_text"] | |
| except Exception as e: | |
| return f"Error : {str(e)}" | |
| def summarize_text(text): | |
| try: | |
| result = summarizer(text, max_length=80, min_length=20, do_sample=False) | |
| return result[0]["summary_text"] | |
| except Exception as e: | |
| return f"Error : {str(e)}" | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Hugging Face Demo") | |
| with gr.Tab("Génération de texte"): | |
| text_input = gr.Textbox(label="Prompt", placeholder="Once upon a time...", lines=4, scale=2) | |
| text_output = gr.Textbox(label="Texte généré", lines=8, scale=2) | |
| text_btn = gr.Button("Générer") | |
| text_btn.click(generate_text, inputs=text_input, outputs=text_output) | |
| with gr.Tab("Traduction de texte (anglais -> français)"): | |
| translate_input = gr.Textbox(label="Texte en anglais", placeholder="Machine learning is fascinating!", lines=4, scale=2) | |
| translate_output = gr.Textbox(label="Traduction en français", lines=6, scale=2) | |
| translate_btn = gr.Button("Traduire") | |
| translate_btn.click(translate_text, inputs=translate_input, outputs=translate_output) | |
| with gr.Tab("Résumé de texte"): | |
| summarize_input = gr.Textbox(label="Texte long à résumer", lines=10, scale=2) | |
| summarize_output = gr.Textbox(label="Résumé", lines=6, scale=2) | |
| summarize_btn = gr.Button("Résumer") | |
| summarize_btn.click(summarize_text, inputs=summarize_input, outputs=summarize_output) | |
| demo.launch() | |