Spaces:
Configuration error
Configuration error
File size: 6,252 Bytes
b15d777 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | import os
import io
import json
from fastapi import FastAPI, File, UploadFile, Form
from fastapi.responses import HTMLResponse
from PIL import Image
import imagehash
app = FastAPI()
DATABASE_DIR = "scam_database"
VOTES_FILE = "votes.json"
# ฟังก์ชันโหลดข้อมูลโหวตจากไฟล์ JSON
def load_votes():
if os.path.exists(VOTES_FILE):
try:
with open(VOTES_FILE, "r", encoding="utf-8") as f:
return json.load(f)
except Exception:
return {}
return {}
# ฟังก์ชันบันทึกข้อมูลโหวตลงไฟล์ JSON
def save_votes(votes_data):
with open(VOTES_FILE, "w", encoding="utf-8") as f:
json.dump(votes_data, f, ensure_ascii=False, indent=2)
# ฟังก์ชันดึง Hash ของรูปทั้งหมดในฐานข้อมูล
def get_database_hashes():
hashes = {}
if not os.path.exists(DATABASE_DIR):
os.makedirs(DATABASE_DIR)
for filename in os.listdir(DATABASE_DIR):
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.webp')):
filepath = os.path.join(DATABASE_DIR, filename)
try:
img = Image.open(filepath)
h = str(imagehash.phash(img))
hashes[filename] = h
except Exception as e:
print(f"Error loading {filename}: {e}")
return hashes
@app.get("/", response_class=HTMLResponse)
def serve_index():
if os.path.exists("index.html"):
with open("index.html", "r", encoding="utf-8") as f:
return f.read()
return "<h1>index.html not found</h1>"
@app.post("/check")
async def check_image(file: UploadFile = File(...)):
try:
contents = await file.read()
uploaded_img = Image.open(io.BytesIO(contents))
uploaded_hash_obj = imagehash.phash(uploaded_img)
uploaded_hash_str = str(uploaded_hash_obj)
db_hashes = get_database_hashes()
votes_data = load_votes()
MATCH_THRESHOLD = 12
matched_hash_key = None
is_in_db = False
# เปรียบเทียบกับภาพในฐานข้อมูล
for filename, db_hash_str in db_hashes.items():
db_hash_obj = imagehash.hex_to_hash(db_hash_str)
distance = uploaded_hash_obj - db_hash_obj
if distance <= MATCH_THRESHOLD:
matched_hash_key = db_hash_str
is_in_db = True
break
# ถ้าไม่เจอในฐานข้อมูล ใช้ Hash ของภาพที่อัปโหลดเป็น Key
if not matched_hash_key:
matched_hash_key = uploaded_hash_str
# ดึงคะแนนโหวต
image_vote = votes_data.get(matched_hash_key, {"likes": 0, "dislikes": 0})
likes = image_vote.get("likes", 0)
dislikes = image_vote.get("dislikes", 0)
# เงื่อนไขตรวจสอบสถานะ
if likes >= 100:
is_scam = False
result_text = "ปลอดภัย"
detail_text = f"รูปภาพนี้ได้รับการยืนยันว่าปลอดภัยจากผู้ใช้งาน (กดไลก์ {likes} คน)"
elif is_in_db:
is_scam = True
result_text = "มิจแน่นอน100%"
detail_text = "พบรูปภาพตรงกับฐานข้อมูลรูปภาพโปรโมทหลอกลวง"
else:
is_scam = False
result_text = "ไม่สามารถตรวจสอบได้"
detail_text = "ยังไม่พบรูปภาพนี้ในฐานข้อมูลมิจฉาชีพ"
return {
"is_scam": is_scam,
"result": result_text,
"detail": detail_text,
"image_hash": matched_hash_key,
"likes": likes,
"dislikes": dislikes
}
except Exception as e:
return {"error": f"เกิดข้อผิดพลาดในการประมวลผล: {str(e)}"}
@app.post("/vote")
async def vote_image(image_hash: str = Form(...), vote_type: str = Form(...)):
try:
votes_data = load_votes()
if image_hash not in votes_data:
votes_data[image_hash] = {"likes": 0, "dislikes": 0}
if vote_type == "like":
votes_data[image_hash]["likes"] += 1
elif vote_type == "dislike":
votes_data[image_hash]["dislikes"] += 1
save_votes(votes_data)
likes = votes_data[image_hash]["likes"]
dislikes = votes_data[image_hash]["dislikes"]
# คำนวณสถานะใหม่หลังกดโหวต
db_hashes = get_database_hashes()
is_in_db = image_hash in db_hashes.values()
if likes >= 100:
is_scam = False
result_text = "ปลอดภัย"
detail_text = f"รูปภาพนี้ได้รับการยืนยันว่าปลอดภัยจากผู้ใช้งาน (กดไลก์ {likes} คน)"
elif is_in_db:
is_scam = True
result_text = "มิจแน่นอน100%"
detail_text = "พบรูปภาพตรงกับฐานข้อมูลรูปภาพโปรโมทหลอกลวง"
else:
is_scam = False
result_text = "ไม่สามารถตรวจสอบได้"
detail_text = "ยังไม่พบรูปภาพนี้ในฐานข้อมูลมิจฉาชีพ"
return {
"success": True,
"likes": likes,
"dislikes": dislikes,
"is_scam": is_scam,
"result": result_text,
"detail": detail_text
}
except Exception as e:
return {"error": f"เกิดข้อผิดพลาดในการโหวต: {str(e)}"}
|