Download services.py from hf1024/grok-api: direct link, hf CLI and curl.
- Browser
- Download file 1.18 kB
-
https://huggingface.co/spaces/hf1024/grok-api/resolve/main/services.py
- Command line
-
hf download hf://spaces/hf1024/grok-api/services.py
-
curl -L -o services.py https://huggingface.co/spaces/hf1024/grok-api/resolve/main/services.py
1.18 kB
| # !/usr/bin/python3 | |
| # -*- coding: utf-8 -*- | |
| """ | |
| @Author : Ailove | |
| ------------------------------------ | |
| @File : services.py | |
| @CreateTime : 2025/5/22 20:37 | |
| ------------------------------------ | |
| """ | |
| from utils import readimg, is_bottom_third_blank, detect_features, get_embedding, get_cos_similar | |
| def analyze_image(body): | |
| inputs = readimg(body, keys=['url']) | |
| img = list(inputs.values())[0] | |
| if img is None: | |
| raise ValueError("Image not found or invalid.") | |
| result = { | |
| "embedding": get_embedding(img).round(6).tolist(), | |
| "blank": bool(is_bottom_third_blank(img)), | |
| "features": detect_features(img) | |
| } | |
| return result | |
| def compare_images(body): | |
| print(body) | |
| keys = ["img_1", "url_1", "img_2", "url_2"] | |
| inputs = readimg(body, keys=[k for k in keys if k in body]) | |
| img1, img2 = inputs.get("img_1") or inputs.get("url_1"), inputs.get("img_2") or inputs.get("url_2") | |
| if not img1 or not img2: | |
| raise ValueError("Both images must be provided.") | |
| emb1 = get_embedding(img1) | |
| emb2 = get_embedding(img2) | |
| sim = get_cos_similar(emb1, emb2) | |
| return {"similarity": float(round(sim, 6))} | |