Spaces:
Sleeping
Sleeping
Update Dockerfile
Browse files- Dockerfile +6 -101
Dockerfile
CHANGED
|
@@ -1,105 +1,10 @@
|
|
| 1 |
-
|
| 2 |
-
import json
|
| 3 |
-
import time
|
| 4 |
-
from flask import Flask, render_template, request, redirect, url_for, session, flash
|
| 5 |
-
from huggingface_hub import HfApi, hf_hub_download
|
| 6 |
-
from werkzeug.security import generate_password_hash, check_password_hash
|
| 7 |
-
from threading import Lock
|
| 8 |
|
| 9 |
-
|
| 10 |
-
app.secret_key = os.environ.get("SECRET_KEY", "dev_key_change_this")
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
DB_REPO = os.environ.get("DB_REPO") # e.g., "username/dataset-name"
|
| 15 |
-
DB_FILE = "db.json"
|
| 16 |
-
api = HfApi(token=HF_TOKEN)
|
| 17 |
-
db_lock = Lock()
|
| 18 |
|
| 19 |
-
|
| 20 |
-
# We download the JSON DB at startup and upload it on every change
|
| 21 |
-
def load_db():
|
| 22 |
-
try:
|
| 23 |
-
path = hf_hub_download(repo_id=DB_REPO, filename=DB_FILE, repo_type="dataset", token=HF_TOKEN)
|
| 24 |
-
with open(path, 'r') as f:
|
| 25 |
-
return json.load(f)
|
| 26 |
-
except:
|
| 27 |
-
# Initialize if not exists
|
| 28 |
-
return {"users": {}, "pins": []}
|
| 29 |
|
| 30 |
-
|
| 31 |
-
with db_lock:
|
| 32 |
-
with open(DB_FILE, 'w') as f:
|
| 33 |
-
json.dump(data, f)
|
| 34 |
-
|
| 35 |
-
# Push to HF Hub (This acts as your persistent storage)
|
| 36 |
-
api.upload_file(
|
| 37 |
-
path_or_fileobj=DB_FILE,
|
| 38 |
-
path_in_repo=DB_FILE,
|
| 39 |
-
repo_id=DB_REPO,
|
| 40 |
-
repo_type="dataset",
|
| 41 |
-
commit_message="Sync DB"
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
# Initialize Local Cache of DB
|
| 45 |
-
DATA_CACHE = load_db()
|
| 46 |
-
|
| 47 |
-
# --- ROUTES ---
|
| 48 |
-
|
| 49 |
-
@app.route('/')
|
| 50 |
-
def index():
|
| 51 |
-
return render_template('index.html', pins=DATA_CACHE['pins'], user=session.get('user'))
|
| 52 |
-
|
| 53 |
-
@app.route('/signup', methods=['POST'])
|
| 54 |
-
def signup():
|
| 55 |
-
username = request.form['username']
|
| 56 |
-
password = request.form['password']
|
| 57 |
-
|
| 58 |
-
if username in DATA_CACHE['users']:
|
| 59 |
-
flash("User already exists")
|
| 60 |
-
return redirect(url_for('index'))
|
| 61 |
-
|
| 62 |
-
DATA_CACHE['users'][username] = generate_password_hash(password)
|
| 63 |
-
save_db(DATA_CACHE)
|
| 64 |
-
session['user'] = username
|
| 65 |
-
return redirect(url_for('index'))
|
| 66 |
-
|
| 67 |
-
@app.route('/login', methods=['POST'])
|
| 68 |
-
def login():
|
| 69 |
-
username = request.form['username']
|
| 70 |
-
password = request.form['password']
|
| 71 |
-
|
| 72 |
-
user_hash = DATA_CACHE['users'].get(username)
|
| 73 |
-
if user_hash and check_password_hash(user_hash, password):
|
| 74 |
-
session['user'] = username
|
| 75 |
-
else:
|
| 76 |
-
flash("Invalid credentials")
|
| 77 |
-
return redirect(url_for('index'))
|
| 78 |
-
|
| 79 |
-
@app.route('/logout')
|
| 80 |
-
def logout():
|
| 81 |
-
session.pop('user', None)
|
| 82 |
-
return redirect(url_for('index'))
|
| 83 |
-
|
| 84 |
-
@app.route('/add_pin', methods=['POST'])
|
| 85 |
-
def add_pin():
|
| 86 |
-
if 'user' not in session:
|
| 87 |
-
return redirect(url_for('index'))
|
| 88 |
-
|
| 89 |
-
# User submits an Image URL (e.g., from Imgur or Unsplash)
|
| 90 |
-
img_url = request.form['img_url']
|
| 91 |
-
caption = request.form['caption']
|
| 92 |
-
|
| 93 |
-
new_pin = {
|
| 94 |
-
"id": int(time.time()),
|
| 95 |
-
"url": img_url,
|
| 96 |
-
"caption": caption,
|
| 97 |
-
"author": session['user']
|
| 98 |
-
}
|
| 99 |
-
|
| 100 |
-
DATA_CACHE['pins'].insert(0, new_pin) # Add to top
|
| 101 |
-
save_db(DATA_CACHE)
|
| 102 |
-
return redirect(url_for('index'))
|
| 103 |
-
|
| 104 |
-
if __name__ == '__main__':
|
| 105 |
-
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
+
FROM python:3.9
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
WORKDIR /code
|
|
|
|
| 4 |
|
| 5 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 6 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
COPY . .
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
CMD ["python", "app.py"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|