Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -163,38 +163,6 @@ def detect_with_model(image: Image.Image, prompt: str, username: str, model_name
|
|
| 163 |
image_filename = f"{image_id}.jpg"
|
| 164 |
image_path = os.path.join(image_dir, image_filename)
|
| 165 |
image.save(image_path)
|
| 166 |
-
|
| 167 |
-
csv_path = os.path.join("test", "leaderboard_entries.csv")
|
| 168 |
-
header = "file_name,prompt,label,model,split,user,timestamp\n"
|
| 169 |
-
csv_line = f"test/fake/{image_filename},\"{prompt}\",fake,{model_name},test,{username},{datetime.now().isoformat()}\n"
|
| 170 |
-
try:
|
| 171 |
-
file_exists = os.path.exists(csv_path)
|
| 172 |
-
with open(csv_path, "a", encoding="utf-8") as f:
|
| 173 |
-
if not file_exists:
|
| 174 |
-
f.write(header)
|
| 175 |
-
f.write(csv_line)
|
| 176 |
-
|
| 177 |
-
from huggingface_hub import HfApi
|
| 178 |
-
|
| 179 |
-
api = HfApi()
|
| 180 |
-
api.upload_file(
|
| 181 |
-
path_or_fileobj=image_path,
|
| 182 |
-
path_in_repo=f"test/fake/{image_filename}",
|
| 183 |
-
repo_id=HUB_REPO_ID,
|
| 184 |
-
repo_type="dataset",
|
| 185 |
-
token=HF_TOKEN,
|
| 186 |
-
commit_message="Add passing image"
|
| 187 |
-
)
|
| 188 |
-
api.upload_file(
|
| 189 |
-
path_or_fileobj=csv_path,
|
| 190 |
-
path_in_repo="test/leaderboard_entries.csv",
|
| 191 |
-
repo_id=HUB_REPO_ID,
|
| 192 |
-
repo_type="dataset",
|
| 193 |
-
token=HF_TOKEN,
|
| 194 |
-
commit_message="Update leaderboard CSV"
|
| 195 |
-
)
|
| 196 |
-
except Exception as e:
|
| 197 |
-
print(f"Failed to save image to HF Hub: {e}")
|
| 198 |
|
| 199 |
else:
|
| 200 |
if model_name.lower() == "real":
|
|
@@ -207,6 +175,39 @@ def detect_with_model(image: Image.Image, prompt: str, username: str, model_name
|
|
| 207 |
sorted_scores = sorted(leaderboard_scores.items(), key=lambda x: x[1], reverse=True)
|
| 208 |
leaderboard_table = [[name, points] for name, points in sorted_scores]
|
| 209 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
return (
|
| 211 |
message,
|
| 212 |
image,
|
|
|
|
| 163 |
image_filename = f"{image_id}.jpg"
|
| 164 |
image_path = os.path.join(image_dir, image_filename)
|
| 165 |
image.save(image_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
else:
|
| 168 |
if model_name.lower() == "real":
|
|
|
|
| 175 |
sorted_scores = sorted(leaderboard_scores.items(), key=lambda x: x[1], reverse=True)
|
| 176 |
leaderboard_table = [[name, points] for name, points in sorted_scores]
|
| 177 |
|
| 178 |
+
type_image = "real" if model_name.lower() == "real" else "fake"
|
| 179 |
+
csv_path = os.path.join("test", "leaderboard_entries.csv")
|
| 180 |
+
header = "file_name,prompt,label,model,split,user,timestamp\n"
|
| 181 |
+
csv_line = f"test/{type_image}/{image_filename},\"{prompt}\",{type_image},{model_name},test,{prediction.lower()},{username},{datetime.now().isoformat()}\n"
|
| 182 |
+
try:
|
| 183 |
+
file_exists = os.path.exists(csv_path)
|
| 184 |
+
with open(csv_path, "a", encoding="utf-8") as f:
|
| 185 |
+
if not file_exists:
|
| 186 |
+
f.write(header)
|
| 187 |
+
f.write(csv_line)
|
| 188 |
+
|
| 189 |
+
from huggingface_hub import HfApi
|
| 190 |
+
|
| 191 |
+
api = HfApi()
|
| 192 |
+
api.upload_file(
|
| 193 |
+
path_or_fileobj=image_path,
|
| 194 |
+
path_in_repo=f"test/{type_image}/{image_filename}",
|
| 195 |
+
repo_id=HUB_REPO_ID,
|
| 196 |
+
repo_type="dataset",
|
| 197 |
+
token=HF_TOKEN,
|
| 198 |
+
commit_message="Add passing image"
|
| 199 |
+
)
|
| 200 |
+
api.upload_file(
|
| 201 |
+
path_or_fileobj=csv_path,
|
| 202 |
+
path_in_repo="test/leaderboard_entries.csv",
|
| 203 |
+
repo_id=HUB_REPO_ID,
|
| 204 |
+
repo_type="dataset",
|
| 205 |
+
token=HF_TOKEN,
|
| 206 |
+
commit_message="Update leaderboard CSV"
|
| 207 |
+
)
|
| 208 |
+
except Exception as e:
|
| 209 |
+
print(f"Failed to save image to HF Hub: {e}")
|
| 210 |
+
|
| 211 |
return (
|
| 212 |
message,
|
| 213 |
image,
|