seshubon commited on
Commit
046775f
1 Parent(s): 02938e0

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ def predict(text, url_params):
4
+ print(text)
5
+ return ["Hello " + text + "!!", url_params]
6
+
7
+
8
+ get_window_url_params = """
9
+ function(text_input, url_params) {
10
+ console.log(text_input, url_params);
11
+ const params = new URLSearchParams(window.location.search);
12
+ url_params = Object.fromEntries(params);
13
+ console.log(typeof(url_params));
14
+ document.getElementById('textbox').value = "Polo";//url_params['text_input'];
15
+ document.getElementById('output').value = url_params['text_input'];
16
+ return [text_input, url_params];
17
+ }
18
+ """
19
+ set_window_url_params = """
20
+ function(text_input, url_params) {
21
+ const params = new URLSearchParams(window.location.search);
22
+ params.set("text_input", text_input)
23
+ url_params = Object.fromEntries(params);
24
+ const queryString = '?' + params.toString();
25
+ // this next line is only needed inside Spaces, so the child frame updates parent
26
+ window.parent.postMessage({ queryString: queryString }, "*")
27
+ return [text_input, url_params];
28
+ }
29
+ """
30
+ with gr.Blocks() as block:
31
+ url_params = gr.JSON({}, visible=True, label="URL Params")
32
+ text_input = gr.Text(label="Input", elem_id="textbox")
33
+ text_output = gr.Text(label="Output", elem_id="output")
34
+
35
+ btn = gr.Button("Get Params")
36
+ btn.click(fn=predict, inputs=[text_input, url_params],
37
+ outputs=[text_output, url_params], js=get_window_url_params)
38
+
39
+ btn2 = gr.Button("Set Params")
40
+ btn2.click(fn=predict, inputs=[text_input, url_params],
41
+ outputs=[text_output, url_params], js=set_window_url_params)
42
+ block.load(
43
+ fn=predict,
44
+ inputs=[text_input, url_params],
45
+ outputs=[text_output, url_params],
46
+ js=get_window_url_params
47
+ )
48
+ block.launch(debug=True)