NayabShakeel commited on
Commit
43682c1
·
verified ·
1 Parent(s): bb778ee

Update dataset.py

Browse files
Files changed (1) hide show
  1. dataset.py +15 -16
dataset.py CHANGED
@@ -1,29 +1,28 @@
1
- from datasets import load_dataset, Dataset
2
- import pandas as pd
3
 
4
- DATASET_NAME = "your-username/quotes-dataset"
5
 
6
  def load_user_quotes():
7
- """Load user-submitted quotes from Hugging Face Dataset"""
8
- try:
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 with upvotes"""
16
  data = load_user_quotes()
17
- data.append({"quote": quote, "author": author, "upvotes": 0}) # Default upvotes = 0
18
-
19
- dataset = Dataset.from_pandas(pd.DataFrame(data))
20
- dataset.push_to_hub(DATASET_NAME, split="train")
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
- dataset = Dataset.from_pandas(pd.DataFrame(data))
29
- dataset.push_to_hub(DATASET_NAME, split="train")
 
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)