Upload caption_cropped_images.py with huggingface_hub
Browse files- caption_cropped_images.py +24 -21
caption_cropped_images.py
CHANGED
|
@@ -9,9 +9,9 @@ import time
|
|
| 9 |
import base64
|
| 10 |
import json
|
| 11 |
import concurrent.futures
|
|
|
|
| 12 |
from pathlib import Path
|
| 13 |
from typing import Dict, Tuple
|
| 14 |
-
from openai import OpenAI
|
| 15 |
|
| 16 |
# Alibaba Qwen Configuration - Read from Environment Variables (HF Spaces Secrets)
|
| 17 |
QWEN_API_KEY = os.environ.get("QWEN_API_KEY")
|
|
@@ -22,22 +22,10 @@ QWEN_MODEL = os.environ.get("QWEN_MODEL", "qwen-vl-max")
|
|
| 22 |
if not QWEN_API_KEY:
|
| 23 |
print("⚠️ Warning: QWEN_API_KEY not set in environment variables")
|
| 24 |
print(" Configure in HF Spaces: Settings → Secrets")
|
| 25 |
-
qwen_client =
|
| 26 |
else:
|
| 27 |
print(f"✓ Qwen configured: {QWEN_MODEL} at {QWEN_BASE_URL}")
|
| 28 |
-
|
| 29 |
-
# Initialize client
|
| 30 |
-
try:
|
| 31 |
-
if QWEN_API_KEY:
|
| 32 |
-
qwen_client = OpenAI(
|
| 33 |
-
api_key=QWEN_API_KEY,
|
| 34 |
-
base_url=QWEN_BASE_URL,
|
| 35 |
-
)
|
| 36 |
-
else:
|
| 37 |
-
qwen_client = None
|
| 38 |
-
except Exception as e:
|
| 39 |
-
print(f"⚠️ Error initializing Qwen client: {e}")
|
| 40 |
-
qwen_client = None
|
| 41 |
|
| 42 |
def encode_image(image_path: str) -> str:
|
| 43 |
"""Encode image to base64."""
|
|
@@ -55,15 +43,21 @@ def caption_single_image(image_path: str, crop_id: str) -> Tuple[str, str]:
|
|
| 55 |
Returns:
|
| 56 |
Tuple of (crop_id, caption_text)
|
| 57 |
"""
|
| 58 |
-
if not
|
| 59 |
return crop_id, "unavailable"
|
| 60 |
|
| 61 |
try:
|
| 62 |
base64_image = encode_image(image_path)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
{
|
| 68 |
"role": "user",
|
| 69 |
"content": [
|
|
@@ -78,10 +72,19 @@ def caption_single_image(image_path: str, crop_id: str) -> Tuple[str, str]:
|
|
| 78 |
]
|
| 79 |
}
|
| 80 |
],
|
| 81 |
-
max_tokens
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
)
|
|
|
|
| 83 |
|
| 84 |
-
|
|
|
|
| 85 |
return crop_id, caption
|
| 86 |
|
| 87 |
except Exception as e:
|
|
|
|
| 9 |
import base64
|
| 10 |
import json
|
| 11 |
import concurrent.futures
|
| 12 |
+
import requests
|
| 13 |
from pathlib import Path
|
| 14 |
from typing import Dict, Tuple
|
|
|
|
| 15 |
|
| 16 |
# Alibaba Qwen Configuration - Read from Environment Variables (HF Spaces Secrets)
|
| 17 |
QWEN_API_KEY = os.environ.get("QWEN_API_KEY")
|
|
|
|
| 22 |
if not QWEN_API_KEY:
|
| 23 |
print("⚠️ Warning: QWEN_API_KEY not set in environment variables")
|
| 24 |
print(" Configure in HF Spaces: Settings → Secrets")
|
| 25 |
+
qwen_client = "disabled"
|
| 26 |
else:
|
| 27 |
print(f"✓ Qwen configured: {QWEN_MODEL} at {QWEN_BASE_URL}")
|
| 28 |
+
qwen_client = "enabled"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def encode_image(image_path: str) -> str:
|
| 31 |
"""Encode image to base64."""
|
|
|
|
| 43 |
Returns:
|
| 44 |
Tuple of (crop_id, caption_text)
|
| 45 |
"""
|
| 46 |
+
if qwen_client != "enabled" or not QWEN_API_KEY:
|
| 47 |
return crop_id, "unavailable"
|
| 48 |
|
| 49 |
try:
|
| 50 |
base64_image = encode_image(image_path)
|
| 51 |
|
| 52 |
+
# Use requests to call the Dashscope API
|
| 53 |
+
headers = {
|
| 54 |
+
"Authorization": f"Bearer {QWEN_API_KEY}",
|
| 55 |
+
"Content-Type": "application/json",
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
payload = {
|
| 59 |
+
"model": QWEN_MODEL,
|
| 60 |
+
"messages": [
|
| 61 |
{
|
| 62 |
"role": "user",
|
| 63 |
"content": [
|
|
|
|
| 72 |
]
|
| 73 |
}
|
| 74 |
],
|
| 75 |
+
"max_tokens": 50
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
response = requests.post(
|
| 79 |
+
f"{QWEN_BASE_URL}/chat/completions",
|
| 80 |
+
json=payload,
|
| 81 |
+
headers=headers,
|
| 82 |
+
timeout=30
|
| 83 |
)
|
| 84 |
+
response.raise_for_status()
|
| 85 |
|
| 86 |
+
result = response.json()
|
| 87 |
+
caption = result["choices"][0]["message"]["content"].strip()
|
| 88 |
return crop_id, caption
|
| 89 |
|
| 90 |
except Exception as e:
|