# Upload File API for Saving to Persistent Datasets on HF Spaces

**URL:** <https://discuss.huggingface.co/t/upload-file-api-for-saving-to-persistent-datasets-on-hf-spaces/23689>\
**Category:** 🤗Datasets\
**Created:** [September 27, 2022, 6:14pm UTC](https://discuss.huggingface.co/t/upload-file-api-for-saving-to-persistent-datasets-on-hf-spaces/23689 "2022-09-27T18:14:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![awacke1](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/awacke1/32/40934_2.png) [@awacke1](https://discuss.huggingface.co/u/awacke1)\
**Post date:** [September 27, 2022, 6:14pm UTC](https://discuss.huggingface.co/t/upload-file-api-for-saving-to-persistent-datasets-on-hf-spaces/23689/1 "2022-09-27T18:14:16Z")

</div>

I am trying to demonstrate the loading and writing of a dataset where I can persist a history of messages and within spaces load my token key which would provide write access to the data to save the information back to the dataset.

I would like to use the example in a class I teach on Fridays as a persistent dataset example. URL is here: [PersistentDatasetMemoryGradio - a Hugging Face Space by awacke1](https://huggingface.co/spaces/awacke1/Memory-Shared)

My code errors here:

def store\_message(name: str, message: str):  
if name and message:  
with open(DATA\_FILE, “a”) as csvfile:  
writer = csv.DictWriter(csvfile, fieldnames=[“name”, “message”, “time”])  
writer.writerow(  
{“name”: name, “message”: message, “time”: str(datetime.now())}  
)  
commit\_url = upload\_file(  
DATA\_FILE,  
path\_in\_repo=DATA\_FILENAME,  
repo\_id=DATASET\_REPO\_ID,  
token=HF\_TOKEN,  
)

```
    print(commit_url)

return generate_html()

```

I set up the parameters used here:  
DATASET\_REPO\_URL = “[awacke1/data.csv · Datasets at Hugging Face](https://huggingface.co/datasets/awacke1/data.csv)”  
DATASET\_REPO\_ID = “awacke1/data.csv”  
DATA\_FILENAME = “data.csv”  
DATA\_FILE = os.path.join(“data”, DATA\_FILENAME)  
HF\_TOKEN = os.environ.get(“HF\_TOKEN”)

The stack trace is shown below. I am pretty sure one of my variables is not correct which calls upload file api.

## Cloning [awacke1/data.csv · Datasets at Hugging Face](https://huggingface.co/datasets/awacke1/data.csv) into local empty directory. IMPORTANT: You are using gradio version 2.4.2, however version 2.5.1 is available, please upgrade.

Running on local URL: [http://0.0.0.0:7860/](http://0.0.0.0:7860/)

To create a public link, set `share=True` in `launch()`.  
Traceback (most recent call last):  
File “/home/user/.local/lib/python3.8/site-packages/gradio/networking.py”, line 193, in predict  
prediction, durations = app.interface.process(raw\_input)  
File “/home/user/.local/lib/python3.8/site-packages/gradio/interface.py”, line 363, in process  
predictions, durations = self.run\_prediction(  
File “/home/user/.local/lib/python3.8/site-packages/gradio/interface.py”, line 332, in run\_prediction  
prediction = predict\_fn(\*processed\_input)  
File “app.py”, line 78, in store\_message  
commit\_url = upload\_file(  
File “/home/user/.local/lib/python3.8/site-packages/huggingface\_hub/hf\_api.py”, line 1346, in upload\_file  
raise err  
File “/home/user/.local/lib/python3.8/site-packages/huggingface\_hub/hf\_api.py”, line 1337, in upload\_file  
r.raise\_for\_status()  
File “/home/user/.local/lib/python3.8/site-packages/requests/models.py”, line 1021, in raise\_for\_status  
raise HTTPError(http\_error\_msg, response=self)  
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: [https://huggingface.co/api/awacke1/data.csv/upload/main/data.csv](https://huggingface.co/api/awacke1/data.csv/upload/main/data.csv)

Can you point out what I need to change in parameters below to get my commit URL correct?  
commit\_url = upload\_file(  
DATA\_FILE,  
path\_in\_repo=DATA\_FILENAME,  
repo\_id=DATASET\_REPO\_ID,  
token=HF\_TOKEN,  
)

---

<div class="post-metadata">

**Author:** ![lhoestq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lhoestq/32/64813_2.png) [@lhoestq](https://discuss.huggingface.co/u/lhoestq)\
**Post date:** [September 29, 2022, 9:19am UTC](https://discuss.huggingface.co/t/upload-file-api-for-saving-to-persistent-datasets-on-hf-spaces/23689/2 "2022-09-29T09:19:36Z")

</div>

I think you’re simpy missing `repo_type="dataset"` in `upload_file` 😉
