Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Response
|
2 |
+
import papermill as pm
|
3 |
+
import io
|
4 |
+
|
5 |
+
app = FastAPI()
|
6 |
+
|
7 |
+
@app.get("/endpoint")
|
8 |
+
def run_notebook(fruits: str):
|
9 |
+
fruit_list = fruits.split(",")
|
10 |
+
|
11 |
+
# Execute the notebook with Papermill
|
12 |
+
output_path = "/tmp/output.ipynb"
|
13 |
+
pm.execute_notebook(
|
14 |
+
"fruitchecker.ipynb",
|
15 |
+
output_path,
|
16 |
+
parameters={"fruits": fruit_list}
|
17 |
+
)
|
18 |
+
|
19 |
+
# Extract the CSV result
|
20 |
+
with open(output_path, "r") as f:
|
21 |
+
notebook_content = f.read()
|
22 |
+
|
23 |
+
# Find the CSV output in the notebook
|
24 |
+
csv_output = notebook_content.split("csv_output = ")[-1].strip().strip("\"'")
|
25 |
+
|
26 |
+
# Return as downloadable CSV
|
27 |
+
return Response(content=csv_output, media_type="text/csv",
|
28 |
+
headers={"Content-Disposition": "attachment; filename=fruits.csv"})
|