Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ from fastapi import FastAPI, HTTPException
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
-
from datasets import Dataset, DatasetDict
|
| 6 |
from huggingface_hub import login, HfApi
|
| 7 |
from typing import List
|
| 8 |
from fastapi.responses import JSONResponse
|
|
@@ -19,21 +19,38 @@ app.add_middleware(
|
|
| 19 |
allow_headers=["*"], # Allow all headers
|
| 20 |
)
|
| 21 |
|
| 22 |
-
|
|
|
|
| 23 |
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
instructions: str
|
| 28 |
|
|
|
|
| 29 |
DATASET_PATH = "/app/data"
|
| 30 |
|
| 31 |
-
# Ensure the 'data' folder exists
|
| 32 |
if not os.path.exists(DATASET_PATH):
|
| 33 |
os.makedirs(DATASET_PATH)
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
@app.put("/add/recipe")
|
| 39 |
async def add_recipe(filename: str, recipe: Recipe):
|
|
@@ -47,14 +64,21 @@ async def add_recipe(filename: str, recipe: Recipe):
|
|
| 47 |
# Prepare the data to be written in JSON format
|
| 48 |
recipe_data = recipe.dict() # Convert Recipe model to dictionary
|
| 49 |
|
| 50 |
-
# Write the data to the new file
|
| 51 |
try:
|
| 52 |
with open(file_path, "w") as f:
|
| 53 |
json.dump(recipe_data, f, indent=4)
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
dataset
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
return {"message": f"Recipe '{filename}' added successfully."}
|
| 59 |
except Exception as e:
|
| 60 |
raise HTTPException(status_code=500, detail=f"Error writing file: {str(e)}")
|
|
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
+
from datasets import Dataset, DatasetDict
|
| 6 |
from huggingface_hub import login, HfApi
|
| 7 |
from typing import List
|
| 8 |
from fastapi.responses import JSONResponse
|
|
|
|
| 19 |
allow_headers=["*"], # Allow all headers
|
| 20 |
)
|
| 21 |
|
| 22 |
+
# Get Hugging Face token from environment variable
|
| 23 |
+
TOKEN = os.getenv("token") # Make sure to set HF_TOKEN environment variable
|
| 24 |
|
| 25 |
+
# Ensure the token is available
|
| 26 |
+
if not TOKEN:
|
| 27 |
+
raise ValueError("HF_TOKEN environment variable is missing.")
|
| 28 |
|
| 29 |
+
# Authentication for Hugging Face
|
| 30 |
+
login(token=TOKEN)
|
|
|
|
| 31 |
|
| 32 |
+
# Define the data folder and recipe model
|
| 33 |
DATASET_PATH = "/app/data"
|
| 34 |
|
|
|
|
| 35 |
if not os.path.exists(DATASET_PATH):
|
| 36 |
os.makedirs(DATASET_PATH)
|
| 37 |
|
| 38 |
+
class Recipe(BaseModel):
|
| 39 |
+
ingredients: List[str]
|
| 40 |
+
instructions: str
|
| 41 |
+
|
| 42 |
+
# Function to load the existing dataset or create a new one
|
| 43 |
+
def load_or_create_dataset():
|
| 44 |
+
dataset_files = [f for f in os.listdir(DATASET_PATH) if f.endswith(".json")]
|
| 45 |
+
all_recipes = []
|
| 46 |
+
|
| 47 |
+
for file_name in dataset_files:
|
| 48 |
+
file_path = os.path.join(DATASET_PATH, file_name)
|
| 49 |
+
with open(file_path, "r") as f:
|
| 50 |
+
recipe_data = json.load(f)
|
| 51 |
+
all_recipes.append(recipe_data)
|
| 52 |
+
|
| 53 |
+
return Dataset.from_dict({"data": all_recipes}) if all_recipes else None
|
| 54 |
|
| 55 |
@app.put("/add/recipe")
|
| 56 |
async def add_recipe(filename: str, recipe: Recipe):
|
|
|
|
| 64 |
# Prepare the data to be written in JSON format
|
| 65 |
recipe_data = recipe.dict() # Convert Recipe model to dictionary
|
| 66 |
|
| 67 |
+
# Write the data to the new file as JSON
|
| 68 |
try:
|
| 69 |
with open(file_path, "w") as f:
|
| 70 |
json.dump(recipe_data, f, indent=4)
|
| 71 |
+
|
| 72 |
+
# Load the updated dataset
|
| 73 |
+
dataset = load_or_create_dataset()
|
| 74 |
+
|
| 75 |
+
# If dataset does not exist, initialize a new one
|
| 76 |
+
if not dataset:
|
| 77 |
+
dataset = Dataset.from_dict({"data": [recipe_data]})
|
| 78 |
+
|
| 79 |
+
# Push the updated dataset to the Hugging Face Hub
|
| 80 |
+
dataset.push_to_hub("sharktide/recipes")
|
| 81 |
+
|
| 82 |
return {"message": f"Recipe '{filename}' added successfully."}
|
| 83 |
except Exception as e:
|
| 84 |
raise HTTPException(status_code=500, detail=f"Error writing file: {str(e)}")
|