File size: 2,671 Bytes
18cf2cb
 
82579ab
 
 
73cfaa5
 
 
 
 
18cf2cb
73cfaa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82579ab
 
 
 
 
 
 
 
73cfaa5
 
18cf2cb
de5608c
82579ab
 
 
73cfaa5
 
 
 
 
 
82579ab
de5608c
 
 
 
 
 
 
 
 
 
 
 
 
 
82579ab
73cfaa5
de5608c
82579ab
de5608c
82579ab
 
de5608c
 
 
82579ab
 
de5608c
 
73cfaa5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import gradio as gr
from tools import *
from format_dexcom import process_csv
import tempfile
import os
from huggingface_hub import list_models
from typing import List, Tuple
from pathlib import Path
import plotly.graph_objects as go
from huggingface_hub import HfApi

def get_available_models() -> List[str]:
    """Get list of available gluformer models from HuggingFace."""
    api = HfApi()
    files = api.list_repo_files("Livia-Zaharia/gluformer_models")
    
    # Filter for .pth files
    gluformer_models = [
        file for file in files 
        if file.endswith('.pth') and "weights" in file.lower() and 'gluformer' in file.lower()
    ]
    
    return gluformer_models

AVAILABLE_MODELS = get_available_models()
print(AVAILABLE_MODELS)

def process_and_prepare(file: tempfile._TemporaryFileWrapper, model_name: str) -> Tuple[gr.Slider, gr.Markdown]:
    """Process the raw CSV and prepare it for prediction.
    
    Args:
        file: Uploaded temporary file object
        model_name: Name of the selected model
        
    Returns:
        Tuple containing:
        - Updated slider component
        - Sample count markdown component
    """
    with tempfile.NamedTemporaryFile(delete=False, suffix='.csv') as tmp_file:
        processed_path = tmp_file.name
    
    process_csv(
        input_dir=file.name,
        output_file=processed_path
    )
    
    return prep_predict_glucose_tool(processed_path, model_name)


with gr.Blocks() as demo:
    gr.Markdown("# Glucose Prediction Tool")
    gr.Markdown("Upload a Dexcom CSV file to get predictions")
    
    model_selector = gr.Dropdown(
        choices=AVAILABLE_MODELS,
        value="gluformer_1samples_500epochs_10heads_32batch_geluactivation_livia_large_weights.pth",
        label="Select Model",
        interactive=True
    )
    file_input = gr.File(label="Upload Raw Dexcom CSV File")
    with gr.Row():
        index_slider = gr.Slider(
            minimum=0,
            maximum=100,  # This will be updated dynamically
            value=10,
            step=1,
            label="Select Sample Index",
            visible=False
        )
    sample_count = gr.Markdown(visible=False)
    plot_output = gr.Plot()
    
    # Update slider and show total samples when file is uploaded
    file_input.change(
        fn=process_and_prepare,
        inputs=[file_input, model_selector],
        outputs=[index_slider, sample_count],
        queue=True
    )
    
    # Only update plot after processing is complete
    index_slider.change(
        fn=predict_glucose_tool,
        inputs=[index_slider],
        outputs=plot_output,
        queue=True
    )

demo.launch(share=True)