Spaces:
Sleeping
Sleeping
Update dataset.py
Browse files- dataset.py +15 -16
dataset.py
CHANGED
@@ -1,29 +1,28 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
|
4 |
-
|
5 |
|
6 |
def load_user_quotes():
|
7 |
-
"""Load user-submitted quotes from
|
8 |
-
|
9 |
-
dataset = load_dataset(DATASET_NAME)
|
10 |
-
return dataset["train"].to_pandas().to_dict(orient="records")
|
11 |
-
except:
|
12 |
return []
|
|
|
|
|
13 |
|
14 |
def save_user_quote(quote, author):
|
15 |
-
"""Save a new user quote
|
16 |
data = load_user_quotes()
|
17 |
-
data.append({"quote": quote, "author": author, "upvotes": 0})
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
|
22 |
def upvote_quote(index):
|
23 |
-
"""Increment upvote count for a quote"""
|
24 |
data = load_user_quotes()
|
25 |
if 0 <= index < len(data):
|
26 |
data[index]["upvotes"] += 1
|
27 |
|
28 |
-
|
29 |
-
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
|
4 |
+
FILE_NAME = "quotes.json"
|
5 |
|
6 |
def load_user_quotes():
|
7 |
+
"""Load user-submitted quotes from a local file."""
|
8 |
+
if not os.path.exists(FILE_NAME):
|
|
|
|
|
|
|
9 |
return []
|
10 |
+
with open(FILE_NAME, "r") as f:
|
11 |
+
return json.load(f)
|
12 |
|
13 |
def save_user_quote(quote, author):
|
14 |
+
"""Save a new user quote in the local file."""
|
15 |
data = load_user_quotes()
|
16 |
+
data.append({"quote": quote, "author": author, "upvotes": 0})
|
17 |
+
|
18 |
+
with open(FILE_NAME, "w") as f:
|
19 |
+
json.dump(data, f)
|
20 |
|
21 |
def upvote_quote(index):
|
22 |
+
"""Increment upvote count for a quote."""
|
23 |
data = load_user_quotes()
|
24 |
if 0 <= index < len(data):
|
25 |
data[index]["upvotes"] += 1
|
26 |
|
27 |
+
with open(FILE_NAME, "w") as f:
|
28 |
+
json.dump(data, f)
|