Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import io
|
| 3 |
import json
|
| 4 |
-
from fastapi import FastAPI, File, UploadFile, Form
|
| 5 |
from fastapi.responses import HTMLResponse
|
| 6 |
from PIL import Image
|
| 7 |
import imagehash
|
|
@@ -43,6 +43,13 @@ def get_database_hashes():
|
|
| 43 |
print(f"Error loading {filename}: {e}")
|
| 44 |
return hashes
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
@app.get("/", response_class=HTMLResponse)
|
| 47 |
def serve_index():
|
| 48 |
if os.path.exists("index.html"):
|
|
@@ -79,7 +86,7 @@ async def check_image(file: UploadFile = File(...)):
|
|
| 79 |
matched_hash_key = uploaded_hash_str
|
| 80 |
|
| 81 |
# ดึงคะแนนโหวต
|
| 82 |
-
image_vote = votes_data.get(matched_hash_key, {"likes": 0, "dislikes": 0})
|
| 83 |
likes = image_vote.get("likes", 0)
|
| 84 |
dislikes = image_vote.get("dislikes", 0)
|
| 85 |
|
|
@@ -109,11 +116,32 @@ async def check_image(file: UploadFile = File(...)):
|
|
| 109 |
return {"error": f"เกิดข้อผิดพลาดในการประมวลผล: {str(e)}"}
|
| 110 |
|
| 111 |
@app.post("/vote")
|
| 112 |
-
async def vote_image(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
try:
|
| 114 |
votes_data = load_votes()
|
| 115 |
if image_hash not in votes_data:
|
| 116 |
-
votes_data[image_hash] = {"likes": 0, "dislikes": 0}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
if vote_type == "like":
|
| 119 |
votes_data[image_hash]["likes"] += 1
|
|
|
|
| 1 |
import os
|
| 2 |
import io
|
| 3 |
import json
|
| 4 |
+
from fastapi import FastAPI, File, UploadFile, Form, Request
|
| 5 |
from fastapi.responses import HTMLResponse
|
| 6 |
from PIL import Image
|
| 7 |
import imagehash
|
|
|
|
| 43 |
print(f"Error loading {filename}: {e}")
|
| 44 |
return hashes
|
| 45 |
|
| 46 |
+
# ฟังก์ชันดึง IP ของผู้ใช้
|
| 47 |
+
def get_client_ip(request: Request):
|
| 48 |
+
forwarded_for = request.headers.get("x-forwarded-for")
|
| 49 |
+
if forwarded_for:
|
| 50 |
+
return forwarded_for.split(",")[0].strip()
|
| 51 |
+
return request.client.host if request.client else "127.0.0.1"
|
| 52 |
+
|
| 53 |
@app.get("/", response_class=HTMLResponse)
|
| 54 |
def serve_index():
|
| 55 |
if os.path.exists("index.html"):
|
|
|
|
| 86 |
matched_hash_key = uploaded_hash_str
|
| 87 |
|
| 88 |
# ดึงคะแนนโหวต
|
| 89 |
+
image_vote = votes_data.get(matched_hash_key, {"likes": 0, "dislikes": 0, "users": {}})
|
| 90 |
likes = image_vote.get("likes", 0)
|
| 91 |
dislikes = image_vote.get("dislikes", 0)
|
| 92 |
|
|
|
|
| 116 |
return {"error": f"เกิดข้อผิดพลาดในการประมวลผล: {str(e)}"}
|
| 117 |
|
| 118 |
@app.post("/vote")
|
| 119 |
+
async def vote_image(
|
| 120 |
+
request: Request,
|
| 121 |
+
image_hash: str = Form(...),
|
| 122 |
+
vote_type: str = Form(...),
|
| 123 |
+
user_id: str = Form(None)
|
| 124 |
+
):
|
| 125 |
try:
|
| 126 |
votes_data = load_votes()
|
| 127 |
if image_hash not in votes_data:
|
| 128 |
+
votes_data[image_hash] = {"likes": 0, "dislikes": 0, "users": {}}
|
| 129 |
+
elif "users" not in votes_data[image_hash]:
|
| 130 |
+
votes_data[image_hash]["users"] = {}
|
| 131 |
+
|
| 132 |
+
# กำหนด Key ระบุตัวตนผู้ใช้ (รวม IP และ User ID)
|
| 133 |
+
client_ip = get_client_ip(request)
|
| 134 |
+
user_key = f"{client_ip}_{user_id}" if user_id else client_ip
|
| 135 |
+
|
| 136 |
+
# ตรวจสอบว่าผู้ใช้งานคนนี้เคยโหวตรูปนี้ไปแล้วหรือไม่
|
| 137 |
+
if user_key in votes_data[image_hash]["users"]:
|
| 138 |
+
return {
|
| 139 |
+
"success": False,
|
| 140 |
+
"error": "คุณเคยโหวตรูปภาพนี้ไปแล้ว สามารถโหวตได้เพียง 1 ครั้งเท่านั้น"
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
# บันทึกการโหวตของผู้ใช้งานคนนี้
|
| 144 |
+
votes_data[image_hash]["users"][user_key] = vote_type
|
| 145 |
|
| 146 |
if vote_type == "like":
|
| 147 |
votes_data[image_hash]["likes"] += 1
|