CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
huggingface

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: huggingface/notebooks
Path: blob/main/course/fr/chapter9/section7.ipynb
Views: 2549
Kernel: Python 3

Introduction aux Blocks

Installez les bibliothèques 🤗 Transformers et 🤗 Gradio pour exécuter ce notebook.

!pip install datasets transformers[sentencepiece] !pip install gradio
import gradio as gr def flip_text(x): return x[::-1] demo = gr.Blocks() with demo: gr.Markdown( """ # Inverser le texte ! Commencer à taper ci-dessous pour voir le résultat. """ ) input = gr.Textbox(placeholder="Inverser ce texte") output = gr.Textbox() input.change(fn=flip_text, inputs=input, outputs=output) demo.launch()
import numpy as np import gradio as gr demo = gr.Blocks() def flip_text(x): return x[::-1] def flip_image(x): return np.fliplr(x) with demo: gr.Markdown("Inverser des fichiers texte ou image à l'aide de cette démo.") with gr.Tabs(): with gr.TabItem("Inverser le texte"): with gr.Row(): text_input = gr.Textbox() text_output = gr.Textbox() text_button = gr.Button("Inverser") with gr.TabItem("Inverser l'image"): with gr.Row(): image_input = gr.Image() image_output = gr.Image() image_button = gr.Button("Inverser") text_button.click(flip_text, inputs=text_input, outputs=text_output) image_button.click(flip_image, inputs=image_input, outputs=image_output) demo.launch()
import gradio as gr api = gr.Interface.load("huggingface/asi/gpt-fr-cased-small") def complete_with_gpt(text): # Utilisez les 50 derniers caractères du texte comme contexte. return text[:-50] + api(text[-50:]) with gr.Blocks() as demo: textbox = gr.Textbox(placeholder="Tapez ici et appuyez sur la touche Entrée...", lines=4) btn = gr.Button("Générer") btn.click(complete_with_gpt, textbox, textbox) demo.launch()
from transformers import pipeline import gradio as gr asr = pipeline("automatic-speech-recognition", "wav2vec2-large-xlsr-53-french") classifier = pipeline("text-classification", model="tblard/tf-allocine") def speech_to_text(speech): text = asr(speech)["text"] return text def text_to_sentiment(text): return classifier(text)[0]["label"] demo = gr.Blocks() with demo: audio_file = gr.Audio(type="filepath") text = gr.Textbox() label = gr.Label() b1 = gr.Button("Reconnaître la parole") b2 = gr.Button("Classifier le sentiment") b1.click(speech_to_text, inputs=audio_file, outputs=text) b2.click(text_to_sentiment, inputs=text, outputs=label) demo.launch()
import gradio as gr def change_textbox(choice): if choice == "court": return gr.Textbox.update(lines=2, visible=True) elif choice == "long": return gr.Textbox.update(lines=8, visible=True) else: return gr.Textbox.update(visible=False) with gr.Blocks() as block: radio = gr.Radio( ["court", "long", "none"], label="Quel genre d'essai souhaitez-vous Ă©crire ?" ) text = gr.Textbox(lines=2, interactive=True) radio.change(fn=change_textbox, inputs=radio, outputs=text) block.launch()