Buckets:
| # Características avanzadas de Interface[[advanced-interface-features]] | |
| Ahora que podemos construir y compartir una interfaz básica, exploremos algunas funcionalidades más avanzadas como el estado y la interpretación. | |
| ### Usar estado para conservar datos[[using-state-to-persist-data]] | |
| Gradio admite *estado de sesión*, donde los datos persisten a lo largo de varios envíos dentro de una carga de página. | |
| ```py | |
| import random | |
| import gradio as gr | |
| def chat(message, history): | |
| history = history or [] | |
| if message.startswith("How many"): | |
| response = random.randint(1, 10) | |
| elif message.startswith("How"): | |
| response = random.choice(["Great", "Good", "Okay", "Bad"]) | |
| elif message.startswith("Where"): | |
| response = random.choice(["Here", "There", "Somewhere"]) | |
| else: | |
| response = "I don't know" | |
| history.append((message, response)) | |
| return history, history | |
| iface = gr.Interface( | |
| chat, | |
| ["text", "state"], | |
| ["chatbot", "state"], | |
| allow_screenshot=False, | |
| allow_flagging="never", | |
| ) | |
| iface.launch() | |
| ``` | |
| ### Usar interpretación para entender las predicciones[[using-interpretation-to-understand-predictions]] | |
| También puedes activar interpretación para ayudar a entender qué partes de la entrada influyen más en la salida: | |
| ```py | |
| import requests | |
| import tensorflow as tf | |
| import gradio as gr | |
| inception_net = tf.keras.applications.MobileNetV2() | |
| response = requests.get("https://git.io/JJkYN") | |
| labels = response.text.split("\n") | |
| def classify_image(inp): | |
| inp = inp.reshape((-1, 224, 224, 3)) | |
| inp = tf.keras.applications.mobilenet_v2.preprocess_input(inp) | |
| prediction = inception_net.predict(inp).flatten() | |
| return {labels[i]: float(prediction[i]) for i in range(1000)} | |
| image = gr.Image(shape=(224, 224)) | |
| label = gr.Label(num_top_classes=3) | |
| title = "Gradio Image Classifiction + Interpretation Example" | |
| gr.Interface( | |
| fn=classify_image, inputs=image, outputs=label, interpretation="default", title=title | |
| ).launch() | |
| ``` | |
| Esto cierra el recorrido por la clase `Interface`. En la siguiente sección veremos cómo usar `Blocks` para construir aplicaciones más flexibles. | |
Xet Storage Details
- Size:
- 2.16 kB
- Xet hash:
- a65d70ee8606100c2751155a5bbcfd83328a6ec2595ddbbcb1a04539c26d0fee
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.