Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,18 +4,19 @@ import gradio as gr
|
|
| 4 |
def process_data(name, age):
|
| 5 |
return f"Hello {name}! You are {age} years old."
|
| 6 |
|
| 7 |
-
#
|
| 8 |
custom_script = """
|
| 9 |
<script>
|
| 10 |
function submitForm() {
|
| 11 |
const name = document.getElementById('name-field').value;
|
| 12 |
const age = document.getElementById('age-field').value;
|
|
|
|
| 13 |
fetch('/api/predict', {
|
| 14 |
method: 'POST',
|
| 15 |
headers: { 'Content-Type': 'application/json' },
|
| 16 |
body: JSON.stringify({
|
| 17 |
data: [name, age],
|
| 18 |
-
fn_index: 0
|
| 19 |
})
|
| 20 |
})
|
| 21 |
.then(response => response.json())
|
|
@@ -30,10 +31,10 @@ function submitForm() {
|
|
| 30 |
</script>
|
| 31 |
"""
|
| 32 |
|
| 33 |
-
# HTML
|
| 34 |
custom_html = """
|
| 35 |
<div>
|
| 36 |
-
<h2>
|
| 37 |
<label for="name-field">Name:</label>
|
| 38 |
<input type="text" id="name-field" placeholder="Enter your name" />
|
| 39 |
<br /><br />
|
|
@@ -45,7 +46,7 @@ custom_html = """
|
|
| 45 |
</div>
|
| 46 |
"""
|
| 47 |
|
| 48 |
-
# Gradio
|
| 49 |
with gr.Blocks() as app:
|
| 50 |
with gr.Row():
|
| 51 |
gr.HTML("<h1>Interactive Form Example</h1>")
|
|
@@ -54,12 +55,13 @@ with gr.Blocks() as app:
|
|
| 54 |
with gr.Row():
|
| 55 |
gr.HTML(custom_script)
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
-
# Launch the
|
| 65 |
-
|
|
|
|
|
|
| 4 |
def process_data(name, age):
|
| 5 |
return f"Hello {name}! You are {age} years old."
|
| 6 |
|
| 7 |
+
# Custom JavaScript for the frontend
|
| 8 |
custom_script = """
|
| 9 |
<script>
|
| 10 |
function submitForm() {
|
| 11 |
const name = document.getElementById('name-field').value;
|
| 12 |
const age = document.getElementById('age-field').value;
|
| 13 |
+
|
| 14 |
fetch('/api/predict', {
|
| 15 |
method: 'POST',
|
| 16 |
headers: { 'Content-Type': 'application/json' },
|
| 17 |
body: JSON.stringify({
|
| 18 |
data: [name, age],
|
| 19 |
+
fn_index: 0
|
| 20 |
})
|
| 21 |
})
|
| 22 |
.then(response => response.json())
|
|
|
|
| 31 |
</script>
|
| 32 |
"""
|
| 33 |
|
| 34 |
+
# HTML Form for Frontend
|
| 35 |
custom_html = """
|
| 36 |
<div>
|
| 37 |
+
<h2>Interactive Form</h2>
|
| 38 |
<label for="name-field">Name:</label>
|
| 39 |
<input type="text" id="name-field" placeholder="Enter your name" />
|
| 40 |
<br /><br />
|
|
|
|
| 46 |
</div>
|
| 47 |
"""
|
| 48 |
|
| 49 |
+
# Gradio Interface
|
| 50 |
with gr.Blocks() as app:
|
| 51 |
with gr.Row():
|
| 52 |
gr.HTML("<h1>Interactive Form Example</h1>")
|
|
|
|
| 55 |
with gr.Row():
|
| 56 |
gr.HTML(custom_script)
|
| 57 |
|
| 58 |
+
# Separate backend interface to map the function
|
| 59 |
+
interface = gr.Interface(
|
| 60 |
+
fn=process_data,
|
| 61 |
+
inputs=["text", "number"],
|
| 62 |
+
outputs="text"
|
| 63 |
+
)
|
| 64 |
|
| 65 |
+
# Launch the backend and frontend together
|
| 66 |
+
app.queue()
|
| 67 |
+
app.launch()
|