Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
import os
|
| 4 |
import time
|
| 5 |
-
from typing import List, Union, Dict, Any
|
| 6 |
|
| 7 |
# =================================================================
|
| 8 |
# CONFIGURATION AND LANGUAGE DEFINITIONS
|
|
@@ -11,7 +11,8 @@ from typing import List, Union, Dict, Any
|
|
| 11 |
# Setting a dummy CSV path for demonstration. In a real scenario, this would save the data.
|
| 12 |
CSV_FILENAME = f"survey_data_{int(time.time())}.csv"
|
| 13 |
# The number of data points expected by the function signature
|
| 14 |
-
EXPECTED_COUNT = 126
|
|
|
|
| 15 |
|
| 16 |
# --- Shared Choices (French Only) ---
|
| 17 |
FREQ_FR = ["Jamais", "Rarement", "Parfois", "Souvent"]
|
|
@@ -168,7 +169,7 @@ prat_cult_freq_components = [
|
|
| 168 |
]
|
| 169 |
prat_nature = gr.Radio(label=LANG["PRAT_NATURE_LABEL"], choices=["Oui", "Non", "Parfois"])
|
| 170 |
|
| 171 |
-
#
|
| 172 |
platform_components = [
|
| 173 |
gr.Number(label=f"Heures/jour pour {platform}", placeholder="0.5, 1, 2...", minimum=0, step=0.5) for platform in PLATFORM_CHOICES
|
| 174 |
]
|
|
@@ -199,25 +200,30 @@ open_alternatives = gr.Textbox(label=LANG["OPEN_ALTERNATIVES_LABEL"], placeholde
|
|
| 199 |
open_motivations = gr.Textbox(label=LANG["OPEN_MOTIVATIONS_LABEL"], lines=3)
|
| 200 |
|
| 201 |
# =================================================================
|
| 202 |
-
# DUMMY PROCESSING FUNCTION
|
| 203 |
# =================================================================
|
| 204 |
|
| 205 |
-
|
|
|
|
| 206 |
"""
|
| 207 |
-
A dummy function to stand in for the actual process_survey
|
| 208 |
-
It checks if the correct number of inputs (
|
| 209 |
"""
|
| 210 |
-
|
| 211 |
-
|
|
|
|
|
|
|
| 212 |
|
| 213 |
-
#
|
| 214 |
-
data_values = list(
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
# In a real scenario, the data would be collected, transformed, and saved here.
|
| 217 |
-
#
|
| 218 |
-
# data_row.to_csv(CSV_FILENAME, mode='a', header=not os.path.exists(CSV_FILENAME), index=False)
|
| 219 |
|
| 220 |
-
return f"✨ Succès ! Le questionnaire a été soumis avec {len(data_values)} réponses. (Données non sauvegardées dans cette démo)."
|
| 221 |
|
| 222 |
# =================================================================
|
| 223 |
# GRADIO UI & LOGIC
|
|
@@ -270,9 +276,8 @@ def update_visibility_space6(knows_practices):
|
|
| 270 |
}
|
| 271 |
|
| 272 |
|
| 273 |
-
# Define
|
| 274 |
-
|
| 275 |
-
lang_state,
|
| 276 |
# TAB 1 - Contact & Lieux (22 components used)
|
| 277 |
enqueteur_id, approach_answer, refusal_reason, refusal_reason_other, contact_later, firstname,
|
| 278 |
space1, space2a_1, space2a_2, space2a_3, space2b,
|
|
@@ -296,22 +301,27 @@ ALL_INPUT_COMPONENTS = [
|
|
| 296 |
open_non_institutionnel, open_alternatives, open_motivations,
|
| 297 |
]
|
| 298 |
|
| 299 |
-
#
|
| 300 |
-
MISSING_COMPONENTS = [gr.Textbox(label=f"Placeholder_{i}", visible=False) for i in range(
|
| 301 |
-
|
|
|
|
|
|
|
|
|
|
| 302 |
|
| 303 |
|
| 304 |
with gr.Blocks(title=LANG["TITLE"], css=".gradio-container { max-width: 1200px; }") as demo:
|
| 305 |
gr.Markdown(f"## {LANG['TITLE']}")
|
| 306 |
gr.Markdown(LANG["INTRO_TEXT"])
|
| 307 |
|
| 308 |
-
output_message = gr.Markdown("---", elem_id="output_message")
|
| 309 |
|
| 310 |
with gr.Row():
|
| 311 |
# Global Inputs
|
| 312 |
enqueteur_id.render()
|
| 313 |
|
| 314 |
with gr.Tabs():
|
|
|
|
|
|
|
| 315 |
# =================================================================
|
| 316 |
# TAB 1: Contact & Lieux Fréquentés
|
| 317 |
# =================================================================
|
|
@@ -456,18 +466,18 @@ with gr.Blocks(title=LANG["TITLE"], css=".gradio-container { max-width: 1200px;
|
|
| 456 |
submit_btn = gr.Button(LANG["SUBMIT_BUTTON"], variant="primary")
|
| 457 |
reset_btn = gr.Button(LANG["RESET_BUTTON"])
|
| 458 |
|
| 459 |
-
# FIX APPLIED HERE:
|
| 460 |
submit_btn.click(
|
| 461 |
fn=process_survey,
|
| 462 |
-
inputs=
|
| 463 |
outputs=output_message
|
| 464 |
)
|
| 465 |
|
| 466 |
-
#
|
| 467 |
reset_btn.click(
|
| 468 |
-
fn=lambda: [None] *
|
| 469 |
inputs=[],
|
| 470 |
-
outputs=
|
| 471 |
# Reset the output message too using JavaScript
|
| 472 |
js="() => { document.getElementById('output_message').innerHTML = '---'; }"
|
| 473 |
)
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import os
|
| 4 |
import time
|
| 5 |
+
from typing import List, Union, Dict, Any, Tuple
|
| 6 |
|
| 7 |
# =================================================================
|
| 8 |
# CONFIGURATION AND LANGUAGE DEFINITIONS
|
|
|
|
| 11 |
# Setting a dummy CSV path for demonstration. In a real scenario, this would save the data.
|
| 12 |
CSV_FILENAME = f"survey_data_{int(time.time())}.csv"
|
| 13 |
# The number of data points expected by the function signature
|
| 14 |
+
EXPECTED_COUNT = 126 # Total inputs expected by Gradio click event
|
| 15 |
+
EXPECTED_DATA_COUNT = EXPECTED_COUNT - 1 # 125 data inputs, plus 1 state input
|
| 16 |
|
| 17 |
# --- Shared Choices (French Only) ---
|
| 18 |
FREQ_FR = ["Jamais", "Rarement", "Parfois", "Souvent"]
|
|
|
|
| 169 |
]
|
| 170 |
prat_nature = gr.Radio(label=LANG["PRAT_NATURE_LABEL"], choices=["Oui", "Non", "Parfois"])
|
| 171 |
|
| 172 |
+
# Used gr.Number instead of gr.Textbox(type="number")
|
| 173 |
platform_components = [
|
| 174 |
gr.Number(label=f"Heures/jour pour {platform}", placeholder="0.5, 1, 2...", minimum=0, step=0.5) for platform in PLATFORM_CHOICES
|
| 175 |
]
|
|
|
|
| 200 |
open_motivations = gr.Textbox(label=LANG["OPEN_MOTIVATIONS_LABEL"], lines=3)
|
| 201 |
|
| 202 |
# =================================================================
|
| 203 |
+
# DUMMY PROCESSING FUNCTION - ADJUSTED SIGNATURE
|
| 204 |
# =================================================================
|
| 205 |
|
| 206 |
+
# The function now expects 125 positional arguments (*data_inputs) followed by the state (lang_state_value)
|
| 207 |
+
def process_survey(*data_inputs: Any, lang_state_value: str) -> str:
|
| 208 |
"""
|
| 209 |
+
A dummy function to stand in for the actual process_survey.
|
| 210 |
+
It checks if the correct number of inputs (125 data + 1 state) was received.
|
| 211 |
"""
|
| 212 |
+
|
| 213 |
+
# Check if the number of data inputs matches the expected count (125)
|
| 214 |
+
if len(data_inputs) != EXPECTED_DATA_COUNT:
|
| 215 |
+
return f"Erreur: Le nombre d'entrées reçues est incorrect ({len(data_inputs) + 1} au lieu de {EXPECTED_COUNT}). Veuillez vérifier la structure."
|
| 216 |
|
| 217 |
+
# data_values is the list of 125 data inputs
|
| 218 |
+
data_values = list(data_inputs)
|
| 219 |
+
|
| 220 |
+
# The language is now explicitly captured by lang_state_value
|
| 221 |
+
language = lang_state_value
|
| 222 |
|
| 223 |
# In a real scenario, the data would be collected, transformed, and saved here.
|
| 224 |
+
# Note: data_values is the list of survey responses (125 items)
|
|
|
|
| 225 |
|
| 226 |
+
return f"✨ Succès ! Le questionnaire a été soumis avec {len(data_values)} réponses en langue '{language}'. (Données non sauvegardées dans cette démo)."
|
| 227 |
|
| 228 |
# =================================================================
|
| 229 |
# GRADIO UI & LOGIC
|
|
|
|
| 276 |
}
|
| 277 |
|
| 278 |
|
| 279 |
+
# Define the 125 DATA components (excluding the state component)
|
| 280 |
+
DATA_INPUT_COMPONENTS = [
|
|
|
|
| 281 |
# TAB 1 - Contact & Lieux (22 components used)
|
| 282 |
enqueteur_id, approach_answer, refusal_reason, refusal_reason_other, contact_later, firstname,
|
| 283 |
space1, space2a_1, space2a_2, space2a_3, space2b,
|
|
|
|
| 301 |
open_non_institutionnel, open_alternatives, open_motivations,
|
| 302 |
]
|
| 303 |
|
| 304 |
+
# Pad with placeholders to reach the total expected data count (125)
|
| 305 |
+
MISSING_COMPONENTS = [gr.Textbox(label=f"Placeholder_{i}", visible=False) for i in range(EXPECTED_DATA_COUNT - len(DATA_INPUT_COMPONENTS))]
|
| 306 |
+
DATA_INPUT_COMPONENTS.extend(MISSING_COMPONENTS)
|
| 307 |
+
|
| 308 |
+
# The list of inputs passed to the submit button: 125 Data Components followed by 1 State Component
|
| 309 |
+
SUBMIT_INPUT_COMPONENTS = DATA_INPUT_COMPONENTS + [lang_state]
|
| 310 |
|
| 311 |
|
| 312 |
with gr.Blocks(title=LANG["TITLE"], css=".gradio-container { max-width: 1200px; }") as demo:
|
| 313 |
gr.Markdown(f"## {LANG['TITLE']}")
|
| 314 |
gr.Markdown(LANG["INTRO_TEXT"])
|
| 315 |
|
| 316 |
+
output_message = gr.Markdown("---", elem_id="output_message")
|
| 317 |
|
| 318 |
with gr.Row():
|
| 319 |
# Global Inputs
|
| 320 |
enqueteur_id.render()
|
| 321 |
|
| 322 |
with gr.Tabs():
|
| 323 |
+
# ... UI elements rendered as before ...
|
| 324 |
+
|
| 325 |
# =================================================================
|
| 326 |
# TAB 1: Contact & Lieux Fréquentés
|
| 327 |
# =================================================================
|
|
|
|
| 466 |
submit_btn = gr.Button(LANG["SUBMIT_BUTTON"], variant="primary")
|
| 467 |
reset_btn = gr.Button(LANG["RESET_BUTTON"])
|
| 468 |
|
| 469 |
+
# FIX APPLIED HERE: The inputs are now 125 data components, followed by the single lang_state component.
|
| 470 |
submit_btn.click(
|
| 471 |
fn=process_survey,
|
| 472 |
+
inputs=SUBMIT_INPUT_COMPONENTS,
|
| 473 |
outputs=output_message
|
| 474 |
)
|
| 475 |
|
| 476 |
+
# The reset outputs still need to be the 125 data components
|
| 477 |
reset_btn.click(
|
| 478 |
+
fn=lambda: [None] * EXPECTED_DATA_COUNT,
|
| 479 |
inputs=[],
|
| 480 |
+
outputs=DATA_INPUT_COMPONENTS,
|
| 481 |
# Reset the output message too using JavaScript
|
| 482 |
js="() => { document.getElementById('output_message').innerHTML = '---'; }"
|
| 483 |
)
|