state working in frontend

This commit is contained in:
Ali Abid 2022-01-13 12:22:38 -08:00
parent 245ec0f75f
commit 526e34a598
6 changed files with 23592 additions and 130 deletions

View File

@ -1,8 +1,8 @@
import gradio as gr
import random
def chat(message):
history = gr.get_state() or []
def chat(message, history):
history = history or []
if message.startswith("How many"):
response = random.randint(1,10)
elif message.startswith("How"):
@ -12,19 +12,18 @@ def chat(message):
else:
response = "I don't know"
history.append((message, response))
gr.set_state(history)
html = "<div class='chatbot'>"
for user_msg, resp_msg in history:
html += f"<div class='user_msg'>{user_msg}</div>"
html += f"<div class='resp_msg'>{resp_msg}</div>"
html += "</div>"
return html
return html, history
iface = gr.Interface(chat, "text", "html", css="""
iface = gr.Interface(chat, ["text", "state"], ["html", "state"], css="""
.chatbox {display:flex;flex-direction:column}
.user_msg, .resp_msg {padding:4px;margin-bottom:4px;border-radius:4px;width:80%}
.user_msg {background-color:cornflowerblue;color:white;align-self:start}
.resp_msg {background-color:lightgray;align-self:self-end}
""", allow_screenshot=False, allow_flagging=False)
""", allow_screenshot=False, allow_flagging="never")
if __name__ == "__main__":
iface.launch()

23675
frontend/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -134,9 +134,13 @@ export class GradioInterface extends React.Component {
if (this.state.example_id === null) {
let input_state = [];
for (let [i, input_component] of this.props.input_components.entries()) {
if (input_component.name === "state") {
input_state[i] = this.state[i];
continue;
}
if (
this.state[i] === null &&
this.props.input_components[i].optional !== true
input_component[i].optional !== true
) {
return;
}
@ -178,7 +182,15 @@ export class GradioInterface extends React.Component {
new_state["avg_duration"] = output["avg_durations"][0];
}
for (let [i, value] of output["data"].entries()) {
new_state[index_start + i] = value;
if (this.props.output_components[i].name === "state") {
for (let [j, input_component] of this.props.input_components.entries()) {
if (input_component.name === "state") {
new_state[j] = value;
}
}
} else {
new_state[index_start + i] = value;
}
}
if (output["flag_index"] !== null) {
new_state["flag_index"] = output["flag_index"];
@ -370,6 +382,9 @@ export class GradioInterface extends React.Component {
>
<div className="component_set">
{this.props.input_components.map((component, index) => {
if (component.name === "state") {
return false;
}
const Component = input_component_set.find(
(c) => c.name === component.name
).memoized_component;
@ -419,6 +434,9 @@ export class GradioInterface extends React.Component {
>
{status}
{this.props.output_components.map((component, index) => {
if (component.name === "state") {
return false;
}
const Component = output_component_set.find(
(c) => c.name === component.name
).memoized_component;

View File

@ -1,4 +1,4 @@
Metadata-Version: 1.0
Metadata-Version: 2.1
Name: gradio
Version: 2.7.0
Summary: Python library for easily interacting with trained machine learning models
@ -6,6 +6,9 @@ Home-page: https://github.com/gradio-app/gradio-UI
Author: Abubakar Abid, Ali Abid, Ali Abdalla, Dawood Khan, Ahsen Khaliq
Author-email: team@gradio.app
License: Apache License 2.0
Description: UNKNOWN
Keywords: machine learning,visualization,reproducibility
Platform: UNKNOWN
License-File: LICENSE
UNKNOWN

View File

@ -1518,10 +1518,7 @@ class State(InputComponent):
self.default = default
super().__init__(label)
def set_default(self, default):
self.default = default
def get_template_context(self):
return {
"default": self.default,

View File

@ -178,7 +178,7 @@ class Interface:
state: i_State = self.input_components[state_param_index]
if state.default is None:
default = utils.get_default_args(fn[0])[state_param_index]
state.set_default(default)
state.default = default
if interpretation is None or isinstance(interpretation, list) or callable(interpretation):
self.interpretation = interpretation