Spaces:
Running
Running
Delete helpers_function
Browse files
helpers_function/helper_email.py
DELETED
|
File without changes
|
helpers_function/helper_meta_data.py
DELETED
|
@@ -1,166 +0,0 @@
|
|
| 1 |
-
import random
|
| 2 |
-
import piexif
|
| 3 |
-
from PIL import Image
|
| 4 |
-
from io import BytesIO
|
| 5 |
-
|
| 6 |
-
def generate_metadata(model):
|
| 7 |
-
# Define different values based on iPhone model
|
| 8 |
-
exposure_times = {
|
| 9 |
-
"iPhone 11": "1/60",
|
| 10 |
-
"iPhone 11 Pro": "1/70",
|
| 11 |
-
"iPhone 12": "1/100",
|
| 12 |
-
"iPhone 12 Pro": "1/110",
|
| 13 |
-
"iPhone 13": "1/120",
|
| 14 |
-
"iPhone 13 Pro": "1/130",
|
| 15 |
-
"iPhone 13 Pro Max": "1/140",
|
| 16 |
-
"iPhone 14": "1/200",
|
| 17 |
-
"iPhone 14 Pro": "1/220",
|
| 18 |
-
"iPhone 14 Pro Max": "1/240",
|
| 19 |
-
"iPhone 15": "1/300",
|
| 20 |
-
"iPhone 15 Plus": "1/320",
|
| 21 |
-
"iPhone 15 Pro": "1/400",
|
| 22 |
-
"iPhone 15 Pro Max": "1/500",
|
| 23 |
-
"iPhone 16": "1/600",
|
| 24 |
-
"iPhone 16 Pro": "1/700",
|
| 25 |
-
"iPhone 16 Pro Max": "1/1000"
|
| 26 |
-
}
|
| 27 |
-
f_numbers = {
|
| 28 |
-
"iPhone 11": "f/1.8",
|
| 29 |
-
"iPhone 11 Pro": "f/1.8",
|
| 30 |
-
"iPhone 12": "f/1.6",
|
| 31 |
-
"iPhone 12 Pro": "f/1.6",
|
| 32 |
-
"iPhone 13": "f/1.5",
|
| 33 |
-
"iPhone 13 Pro": "f/1.5",
|
| 34 |
-
"iPhone 13 Pro Max": "f/1.5",
|
| 35 |
-
"iPhone 14": "f/1.4",
|
| 36 |
-
"iPhone 14 Pro": "f/1.4",
|
| 37 |
-
"iPhone 14 Pro Max": "f/1.4",
|
| 38 |
-
"iPhone 15": "f/1.4",
|
| 39 |
-
"iPhone 15 Plus": "f/1.4",
|
| 40 |
-
"iPhone 15 Pro": "f/1.4",
|
| 41 |
-
"iPhone 15 Pro Max": "f/1.3",
|
| 42 |
-
"iPhone 16": "f/1.3",
|
| 43 |
-
"iPhone 16 Pro": "f/1.3",
|
| 44 |
-
"iPhone 16 Pro Max": "f/1.3"
|
| 45 |
-
}
|
| 46 |
-
focal_lengths = {
|
| 47 |
-
"iPhone 11": "3.99 mm",
|
| 48 |
-
"iPhone 11 Pro": "4.0 mm",
|
| 49 |
-
"iPhone 12": "4.2 mm",
|
| 50 |
-
"iPhone 12 Pro": "4.3 mm",
|
| 51 |
-
"iPhone 13": "5.0 mm",
|
| 52 |
-
"iPhone 13 Pro": "5.1 mm",
|
| 53 |
-
"iPhone 13 Pro Max": "5.2 mm",
|
| 54 |
-
"iPhone 14": "6.0 mm",
|
| 55 |
-
"iPhone 14 Pro": "6.1 mm",
|
| 56 |
-
"iPhone 14 Pro Max": "6.2 mm",
|
| 57 |
-
"iPhone 15": "6.0 mm",
|
| 58 |
-
"iPhone 15 Plus": "6.1 mm",
|
| 59 |
-
"iPhone 15 Pro": "6.1 mm",
|
| 60 |
-
"iPhone 15 Pro Max": "6.2 mm",
|
| 61 |
-
"iPhone 16": "6.5 mm",
|
| 62 |
-
"iPhone 16 Pro": "6.6 mm",
|
| 63 |
-
"iPhone 16 Pro Max": "6.7 mm"
|
| 64 |
-
}
|
| 65 |
-
|
| 66 |
-
metadata = {
|
| 67 |
-
"Make": "Apple",
|
| 68 |
-
"Model": model,
|
| 69 |
-
"Software": "iOS {}".format(random.choice([14, 15, 16, 17, 18, 19])),
|
| 70 |
-
"Orientation": random.choice(["Horizontal (normal)", "Rotate 90 CW", "Rotate 180", "Rotate 90 CCW"]),
|
| 71 |
-
"DateTime": "{}:{}:{} {:02}:{:02}:{:02}".format(random.randint(2022, 2024), random.randint(1, 12), random.randint(1, 28), random.randint(0, 23), random.randint(0, 59), random.randint(0, 59)),
|
| 72 |
-
"ExposureTime": exposure_times[model],
|
| 73 |
-
"FNumber": f_numbers[model],
|
| 74 |
-
"ISOSpeedRatings": random.choice([100, 200, 400, 800, 1600]),
|
| 75 |
-
"FocalLength": focal_lengths[model],
|
| 76 |
-
"Flash": random.choice(["Flash fired", "Flash did not fire, compulsory mode", "Flash fired, auto mode"]),
|
| 77 |
-
"WhiteBalance": random.choice(["Auto", "Manual"]),
|
| 78 |
-
"MeteringMode": random.choice(["Pattern", "CenterWeightedAverage", "Spot"]),
|
| 79 |
-
"SceneCaptureType": random.choice(["Standard", "Landscape", "Portrait", "NightScene"]),
|
| 80 |
-
"GPSLatitude": "{:.4f} N".format(random.uniform(0.0, 90.0)),
|
| 81 |
-
"GPSLongitude": "{:.4f} W".format(random.uniform(0.0, 180.0)),
|
| 82 |
-
"Altitude": "{:.1f} m".format(random.uniform(0, 100)),
|
| 83 |
-
"LensMake": "Apple",
|
| 84 |
-
"LensModel": "{} back triple camera {} f/1.5".format(model, focal_lengths[model]),
|
| 85 |
-
"ColorSpace": random.choice(["sRGB", "Adobe RGB"]),
|
| 86 |
-
"PixelXDimension": random.choice([3024, 4032, 2160]),
|
| 87 |
-
"PixelYDimension": random.choice([3024, 4032, 2160]),
|
| 88 |
-
"ExposureBiasValue": random.choice(["0 EV", "+1 EV", "-1 EV"]),
|
| 89 |
-
"BrightnessValue": "{:.1f}".format(random.uniform(-5, 10)),
|
| 90 |
-
"ExposureMode": random.choice(["Auto Exposure", "Manual Exposure"])
|
| 91 |
-
}
|
| 92 |
-
return metadata
|
| 93 |
-
|
| 94 |
-
def dms_coordinates(value):
|
| 95 |
-
"""Convert decimal degrees to EXIF GPS (DMS format)."""
|
| 96 |
-
degrees = int(value)
|
| 97 |
-
minutes_float = (value - degrees) * 60
|
| 98 |
-
minutes = int(minutes_float)
|
| 99 |
-
seconds = round((minutes_float - minutes) * 60 * 10000)
|
| 100 |
-
return [(degrees, 1), (minutes, 1), (seconds, 10000)]
|
| 101 |
-
|
| 102 |
-
def meta_data_helper_function(image_bytes,model=None):
|
| 103 |
-
"""
|
| 104 |
-
Takes raw image bytes, adds realistic EXIF metadata, and returns new bytes.
|
| 105 |
-
"""
|
| 106 |
-
if model is None:
|
| 107 |
-
model = random.choice([
|
| 108 |
-
"iPhone 11", "iPhone 11 Pro", "iPhone 12", "iPhone 12 Pro",
|
| 109 |
-
"iPhone 13", "iPhone 13 Pro", "iPhone 13 Pro Max", "iPhone 14",
|
| 110 |
-
"iPhone 14 Pro", "iPhone 14 Pro Max", "iPhone 15", "iPhone 15 Plus",
|
| 111 |
-
"iPhone 15 Pro", "iPhone 15 Pro Max", "iPhone 16", "iPhone 16 Pro",
|
| 112 |
-
"iPhone 16 Pro Max"
|
| 113 |
-
])
|
| 114 |
-
|
| 115 |
-
# Load image from bytes
|
| 116 |
-
img = Image.open(BytesIO(image_bytes))
|
| 117 |
-
img = img.convert("RGB") # Ensure compatibility
|
| 118 |
-
|
| 119 |
-
# Generate metadata
|
| 120 |
-
metadata = generate_metadata(model)
|
| 121 |
-
|
| 122 |
-
# Build EXIF data
|
| 123 |
-
exif_dict = {"0th": {}, "Exif": {}, "GPS": {}}
|
| 124 |
-
|
| 125 |
-
exif_dict["0th"][piexif.ImageIFD.Make] = metadata["Make"]
|
| 126 |
-
exif_dict["0th"][piexif.ImageIFD.Model] = metadata["Model"]
|
| 127 |
-
exif_dict["0th"][piexif.ImageIFD.Software] = metadata["Software"]
|
| 128 |
-
exif_dict["0th"][piexif.ImageIFD.DateTime] = metadata["DateTime"]
|
| 129 |
-
|
| 130 |
-
# Exposure time (rational)
|
| 131 |
-
num, den = map(int, metadata["ExposureTime"].split("/"))
|
| 132 |
-
exif_dict["Exif"][piexif.ExifIFD.ExposureTime] = (num, den)
|
| 133 |
-
|
| 134 |
-
# FNumber
|
| 135 |
-
f_number = float(metadata["FNumber"].replace("f/", ""))
|
| 136 |
-
exif_dict["Exif"][piexif.ExifIFD.FNumber] = (int(f_number * 10), 10)
|
| 137 |
-
|
| 138 |
-
# ISO
|
| 139 |
-
exif_dict["Exif"][piexif.ExifIFD.ISOSpeedRatings] = metadata["ISOSpeedRatings"]
|
| 140 |
-
|
| 141 |
-
# Focal Length
|
| 142 |
-
focal_length = float(metadata["FocalLength"].split()[0])
|
| 143 |
-
exif_dict["Exif"][piexif.ExifIFD.FocalLength] = (int(focal_length * 10), 10)
|
| 144 |
-
|
| 145 |
-
# GPS
|
| 146 |
-
lat = float(metadata["GPSLatitude"].split()[0])
|
| 147 |
-
lon = float(metadata["GPSLongitude"].split()[0])
|
| 148 |
-
|
| 149 |
-
exif_dict["GPS"][piexif.GPSIFD.GPSLatitudeRef] = b'N' if lat >= 0 else b'S'
|
| 150 |
-
exif_dict["GPS"][piexif.GPSIFD.GPSLatitude] = dms_coordinates(abs(lat))
|
| 151 |
-
|
| 152 |
-
exif_dict["GPS"][piexif.GPSIFD.GPSLongitudeRef] = b'E' if lon >= 0 else b'W'
|
| 153 |
-
exif_dict["GPS"][piexif.GPSIFD.GPSLongitude] = dms_coordinates(abs(lon))
|
| 154 |
-
|
| 155 |
-
altitude_value = float(metadata["Altitude"].replace(" m", ""))
|
| 156 |
-
exif_dict["GPS"][piexif.GPSIFD.GPSAltitude] = (int(altitude_value), 1)
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
# Dump to bytes
|
| 160 |
-
exif_bytes = piexif.dump(exif_dict)
|
| 161 |
-
|
| 162 |
-
# Save to in-memory buffer
|
| 163 |
-
output_io = BytesIO()
|
| 164 |
-
img.save(output_io, format="JPEG", exif=exif_bytes)
|
| 165 |
-
return output_io.getvalue()
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
helpers_function/helpers.py
DELETED
|
@@ -1,71 +0,0 @@
|
|
| 1 |
-
|
| 2 |
-
import boto3
|
| 3 |
-
from uuid import uuid4
|
| 4 |
-
import os
|
| 5 |
-
import streamlit as st
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
import cv2
|
| 9 |
-
import base64
|
| 10 |
-
import logging
|
| 11 |
-
|
| 12 |
-
logger = logging.getLogger(__name__)
|
| 13 |
-
|
| 14 |
-
def get_video_thumbnail_base64(video_path: str, time_sec: int = 1) -> str:
|
| 15 |
-
try:
|
| 16 |
-
cap = cv2.VideoCapture(video_path)
|
| 17 |
-
cap.set(cv2.CAP_PROP_POS_MSEC, time_sec * 1000)
|
| 18 |
-
success, frame = cap.read()
|
| 19 |
-
cap.release()
|
| 20 |
-
if not success:
|
| 21 |
-
return ""
|
| 22 |
-
_, buffer = cv2.imencode(".jpg", frame)
|
| 23 |
-
return base64.b64encode(buffer).decode("utf-8")
|
| 24 |
-
except Exception:
|
| 25 |
-
logger.exception("Thumbnail extraction failed")
|
| 26 |
-
return ""
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
log = logging.getLogger(__name__)
|
| 30 |
-
|
| 31 |
-
def upload_image_to_r2(image_bytes, folder_name="search_arb", app_type="bulk_generation"):
|
| 32 |
-
try:
|
| 33 |
-
s3 = boto3.client(
|
| 34 |
-
"s3",
|
| 35 |
-
endpoint_url=os.getenv('R2_ENDPOINT'),
|
| 36 |
-
aws_access_key_id=os.getenv('R2_ACCESS_KEY'),
|
| 37 |
-
aws_secret_access_key=os.getenv('R2_SECRET_KEY'),
|
| 38 |
-
region_name="auto"
|
| 39 |
-
)
|
| 40 |
-
filename = f"{uuid4().hex}.png"
|
| 41 |
-
file_key = f"hug_face/{app_type.strip('/')}/{folder_name.strip('/')}/{filename}" if folder_name else f"hug_face/{app_type.strip('/')}/{filename}"
|
| 42 |
-
s3.put_object(Bucket=os.getenv('R2_BUCKET_NAME'), Key=file_key, Body=image_bytes, ContentType="image/png")
|
| 43 |
-
return f"{os.getenv('NEW_BASE').rstrip('/')}/{file_key}"
|
| 44 |
-
except Exception as e:
|
| 45 |
-
log.error(e)
|
| 46 |
-
return None
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
def encode_image_to_base64(image_path: str) -> str:
|
| 50 |
-
with open(image_path, "rb") as f:
|
| 51 |
-
return base64.b64encode(f.read()).decode("utf-8")
|
| 52 |
-
|
| 53 |
-
def is_valid_image(file_name: str) -> bool:
|
| 54 |
-
return file_name.lower().endswith((".png", ".jpg", ".jpeg", ".bmp", ".gif", ".webp"))
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
def get_current_user_id() -> str:
|
| 58 |
-
"""
|
| 59 |
-
Returns a stable user id.
|
| 60 |
-
Priority: Streamlit session -> USER_ID env -> CREATED_BY env -> 'anonymous'
|
| 61 |
-
"""
|
| 62 |
-
try:
|
| 63 |
-
|
| 64 |
-
uid = st.session_state.get("uid")
|
| 65 |
-
if uid:
|
| 66 |
-
return str(uid).strip()
|
| 67 |
-
except Exception:
|
| 68 |
-
pass
|
| 69 |
-
|
| 70 |
-
return str(os.getenv("USER_ID") or os.getenv("CREATED_BY") or "anonymous").strip() or "anonymous"
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|