File size: 2,572 Bytes
c302440
a87a9c9
401cbb9
 
a87a9c9
401cbb9
 
 
 
 
c302440
a87a9c9
 
c302440
401cbb9
 
c302440
401cbb9
 
 
c302440
401cbb9
 
 
a87a9c9
 
c302440
401cbb9
 
 
 
c302440
a87a9c9
c302440
401cbb9
a87a9c9
401cbb9
a87a9c9
c302440
401cbb9
a87a9c9
c302440
401cbb9
 
 
 
c302440
 
401cbb9
 
 
 
 
 
 
 
 
c302440
 
a87a9c9
 
c302440
a87a9c9
401cbb9
 
c302440
a87a9c9
401cbb9
 
c302440
a87a9c9
c302440
 
401cbb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a87a9c9
401cbb9
c302440
a87a9c9
 
 
c302440
 
a87a9c9
401cbb9
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import os
import nbformat
import gradio as gr
from fastapi import FastAPI
from nbconvert.preprocessors import ExecutePreprocessor
from fastapi.responses import JSONResponse

# =========================
# Setup
# =========================

TEMP_FOLDER = "temp"
os.makedirs(TEMP_FOLDER, exist_ok=True)

# FastAPI App
app = FastAPI()

# =========================
# Notebook Execution
# =========================


def execute_notebook(notebook_path):
    try:
        with open(notebook_path, "r", encoding="utf-8") as f:
            nb = nbformat.read(f, as_version=4)

        ep = ExecutePreprocessor(
            timeout=1800,
            kernel_name="python3"
        )

        ep.preprocess(nb, {'metadata': {'path': TEMP_FOLDER}})

        executed_file = os.path.join(
            TEMP_FOLDER,
            f"executed_{os.path.basename(notebook_path)}"
        )

        with open(executed_file, "w", encoding="utf-8") as f:
            nbformat.write(nb, f)

        return {
            "success": True,
            "output": executed_file
        }

    except Exception as e:
        return {
            "success": False,
            "error": str(e)
        }


# =========================
# Gradio Upload Logic
# =========================


def process_notebooks(notebook1, notebook2):
    results = []

    if notebook1:
        result1 = execute_notebook(notebook1.name)
        results.append(f"Notebook 1: {result1}")

    if notebook2:
        result2 = execute_notebook(notebook2.name)
        results.append(f"Notebook 2: {result2}")

    return "\n\n".join(results)


# =========================
# API Endpoints for n8n
# =========================

@app.get("/health")
def health_check():
    return {
        "status": "running",
        "service": "movie recommendation pipeline"
    }


@app.post("/run-pipeline")
def run_pipeline():
    notebook1_path = "notebook1.ipynb"
    notebook2_path = "notebook2.ipynb"

    results = {}

    if os.path.exists(notebook1_path):
        results["notebook1"] = execute_notebook(notebook1_path)

    if os.path.exists(notebook2_path):
        results["notebook2"] = execute_notebook(notebook2_path)

    return JSONResponse(content={
        "status": "completed",
        "results": results
    })


# =========================
# Gradio UI
# =========================

with gr.Blocks() as demo:
    gr.Markdown("# Movie Recommendation Notebook Pipeline")

    notebook1 = gr.File(
        label="Upload Notebook 1 (.ipynb)",
        file_types=[".ipynb"]
    )

    notebook2 = gr.File(
        label="Upload