Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,9 +3,10 @@ import os
|
|
3 |
from fastapi import FastAPI, HTTPException
|
4 |
from pydantic import BaseModel
|
5 |
from typing import List
|
6 |
-
from huggingface_hub import login, HfApi
|
7 |
from fastapi.middleware.cors import CORSMiddleware
|
8 |
from datetime import datetime
|
|
|
9 |
|
10 |
# Initialize FastAPI app
|
11 |
app = FastAPI()
|
@@ -53,19 +54,21 @@ async def add_recipe(filename: str, recipe: Recipe):
|
|
53 |
|
54 |
# Save the recipe to the JSON file
|
55 |
try:
|
|
|
56 |
with open(file_path, "a") as f:
|
57 |
json.dump(recipe_data, f)
|
58 |
f.write("\n") # Add a newline after each entry
|
59 |
-
|
60 |
-
#
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
)
|
|
|
|
|
66 |
|
67 |
return {"message": f"Recipe '{filename}' added and pushed to Hugging Face dataset."}
|
68 |
|
69 |
except Exception as e:
|
70 |
raise HTTPException(status_code=500, detail=f"Error writing file: {str(e)}")
|
71 |
-
|
|
|
3 |
from fastapi import FastAPI, HTTPException
|
4 |
from pydantic import BaseModel
|
5 |
from typing import List
|
6 |
+
from huggingface_hub import login, HfApi, Repository
|
7 |
from fastapi.middleware.cors import CORSMiddleware
|
8 |
from datetime import datetime
|
9 |
+
from pathlib import Path
|
10 |
|
11 |
# Initialize FastAPI app
|
12 |
app = FastAPI()
|
|
|
54 |
|
55 |
# Save the recipe to the JSON file
|
56 |
try:
|
57 |
+
# Append the recipe data to the JSON file
|
58 |
with open(file_path, "a") as f:
|
59 |
json.dump(recipe_data, f)
|
60 |
f.write("\n") # Add a newline after each entry
|
61 |
+
|
62 |
+
# Now push this dataset file to Hugging Face dataset repo
|
63 |
+
# Initialize the Repository object to manage the dataset upload
|
64 |
+
repo = Repository(local_dir=JSON_DATASET_DIR, clone_from=dataset_repo, use_auth_token=TOKEN)
|
65 |
+
|
66 |
+
# Add the new file to the repository and push the changes
|
67 |
+
repo.git_add()
|
68 |
+
repo.git_commit(f"Add new recipe: {filename}")
|
69 |
+
repo.git_push()
|
70 |
|
71 |
return {"message": f"Recipe '{filename}' added and pushed to Hugging Face dataset."}
|
72 |
|
73 |
except Exception as e:
|
74 |
raise HTTPException(status_code=500, detail=f"Error writing file: {str(e)}")
|
|