Spaces:
Configuration error
Configuration error
| import os, json, io | |
| lines = [] | |
| try: | |
| path = hf_hub_download(repo_id=OUT_DATASET, repo_type="dataset", filename=OUT_FILE, token=HF_TOKEN) | |
| with open(path, "r", encoding="utf-8") as f: | |
| lines = f.read().splitlines() | |
| except Exception: | |
| lines = [] | |
| lines.append(json.dumps(record, ensure_ascii=False)) | |
| content = "\n".join(lines).encode("utf-8") | |
| upload_file( | |
| path_or_fileobj=io.BytesIO(content), | |
| path_in_repo=OUT_FILE, | |
| repo_id=OUT_DATASET, | |
| repo_type="dataset", | |
| token=HF_TOKEN | |
| ) | |
| return f"Guardado en {OUT_DATASET}/{OUT_FILE}" | |
| # --- Callback principal --- | |
| def on_generate(name, age, sex, distance, target_date, | |
| days_per_week, long_run_day, current_5k_pace, long_run_km, experience, | |
| keywords, topk): | |
| # 1) búsqueda en el dataset | |
| table = search_papers(keywords, topk) | |
| # 2) generar plan | |
| plan = build_plan(distance, target_date, int(days_per_week), long_run_day, | |
| float(current_5k_pace), float(long_run_km), experience) | |
| # 3) guardar | |
| record = { | |
| "ts": datetime.utcnow().isoformat()+"Z", | |
| "user": {"name": name, "age": age, "sex": sex}, | |
| "goal": {"distance": distance, "target_date": target_date}, | |
| "prefs": {"days_per_week": days_per_week, "long_run_day": long_run_day, "experience": experience}, | |
| "benchmarks": {"current_5k_pace_min_per_km": current_5k_pace, "long_run_km": long_run_km}, | |
| "keywords": keywords, | |
| "plan": plan | |
| } | |
| saved = ensure_repo_and_push(record) | |
| return table, json.dumps(plan, ensure_ascii=False, indent=2), saved | |
| # --- UI --- | |
| with gr.Blocks(title="OR_Training — Running Plan Builder") as demo: | |
| gr.Markdown(""" | |
| # 🏃 OR_Training — Running Plan Builder | |
| Este Space busca en literatura (dataset NCBI extraído) y genera un **plan básico** personalizado. | |
| - Dataset: `florentgbelidji/ncbi_extracted_running` (recomendado **duplicarlo** a `rogarces85/ncbi_extracted_running`). | |
| - Cada envío puede guardarse como una línea en `dataset` propio (`OR_Training_submissions`). | |
| """) | |
| with gr.Row(): | |
| with gr.Column(): | |
| gr.Markdown("### 🔎 Búsqueda en el dataset") | |
| keywords = gr.Textbox(label="Palabras clave (ej: injury, marathon, VO2max)") | |
| topk = gr.Slider(1, 20, value=5, step=1, label="Resultados (top-k)") | |
| table_out = gr.Dataframe(label="Artículos coincidentes (id, title, date, authors, language, snippet)", wrap=True, height=300) | |
| with gr.Column(): | |
| gr.Markdown("### 📝 Formulario del atleta") | |
| name = gr.Textbox(label="Nombre", value="") | |
| age = gr.Number(label="Edad", value=30) | |
| sex = gr.Radio(["F","M","Otro"], label="Sexo", value="F") | |
| distance = gr.Dropdown(["5K","10K","21K","42K"], value="21K", label="Objetivo") | |
| target_date = gr.Textbox(label="Fecha objetivo (YYYY-MM-DD)", value="2026-03-01") | |
| days_per_week = gr.Slider(3,6,value=4,step=1,label="Días/semana") | |
| long_run_day = gr.Dropdown(["Sábado","Domingo"], value="Domingo", label="Día de fondo") | |
| current_5k_pace = gr.Number(label="Ritmo actual 5K (min/km)", value=5.30) | |
| long_run_km = gr.Number(label="Fondo actual (km)", value=12) | |
| experience = gr.Dropdown(["Principiante","Intermedio","Avanzado"], value="Intermedio", label="Experiencia") | |
| btn = gr.Button("Generar plan y guardar envío") | |
| plan_json = gr.Code(label="Plan (JSON)", language="json", lines=20) | |
| saved_label = gr.Markdown() | |
| btn.click(on_generate, | |
| inputs=[name, age, sex, distance, target_date, days_per_week, long_run_day, | |
| current_5k_pace, long_run_km, experience, keywords, topk], | |
| outputs=[table_out, plan_json, saved_label]) | |
| if __name__ == "__main__": | |
| demo.launch() | |