Commit d35d18ebe5499dc3a5ddd8faeeb67434abc7c81e
1 parent
c0b24bef
fix
Showing
2 changed files
with
45 additions
and
0 deletions
Show diff stats
| @@ -0,0 +1,36 @@ | @@ -0,0 +1,36 @@ | ||
| 1 | +from modelscope import AutoProcessor, Gemma3nForConditionalGeneration | ||
| 2 | +from PIL import Image | ||
| 3 | +import requests | ||
| 4 | +import torch | ||
| 5 | +model_id = "google/gemma-3n-e4b-it" | ||
| 6 | +model = Gemma3nForConditionalGeneration.from_pretrained(model_id, device_map="auto", torch_dtype=torch.bfloat16,).eval() | ||
| 7 | +processor = AutoProcessor.from_pretrained(model_id) | ||
| 8 | +messages = [ | ||
| 9 | + { | ||
| 10 | + "role": "system", | ||
| 11 | + "content": [{"type": "text", "text": "You are a helpful assistant."}] | ||
| 12 | + }, | ||
| 13 | + { | ||
| 14 | + "role": "user", | ||
| 15 | + "content": [ | ||
| 16 | + {"type": "image", "image": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg"}, | ||
| 17 | + {"type": "text", "text": "Describe this image in detail."} | ||
| 18 | + ] | ||
| 19 | + } | ||
| 20 | +] | ||
| 21 | +inputs = processor.apply_chat_template( | ||
| 22 | + messages, | ||
| 23 | + add_generation_prompt=True, | ||
| 24 | + tokenize=True, | ||
| 25 | + return_dict=True, | ||
| 26 | + return_tensors="pt", | ||
| 27 | +).to(model.device) | ||
| 28 | +input_len = inputs["input_ids"].shape[-1] | ||
| 29 | +with torch.inference_mode(): | ||
| 30 | + generation = model.generate(**inputs, max_new_tokens=100, do_sample=False) | ||
| 31 | + generation = generation[0][input_len:] | ||
| 32 | +decoded = processor.decode(generation, skip_special_tokens=True) | ||
| 33 | +print(decoded) | ||
| 34 | +# **Overall Impression:** The image is a close-up shot of a vibrant garden scene, | ||
| 35 | +# focusing on a cluster of pink cosmos flowers and a busy bumblebee. | ||
| 36 | +# It has a slightly soft, natural feel, likely captured in daylight. | ||
| 0 | \ No newline at end of file | 37 | \ No newline at end of file |
offline_tasks/scripts/load_index_to_redis.py
| @@ -51,6 +51,15 @@ def load_index_file(file_path, redis_client, key_prefix, expire_seconds=None): | @@ -51,6 +51,15 @@ def load_index_file(file_path, redis_client, key_prefix, expire_seconds=None): | ||
| 51 | # 格式2 (3字段): item_id \t item_name \t similar_items (推荐格式) | 51 | # 格式2 (3字段): item_id \t item_name \t similar_items (推荐格式) |
| 52 | # 取第一个字段作为key,最后一个字段作为value | 52 | # 取第一个字段作为key,最后一个字段作为value |
| 53 | key_suffix = parts[0] | 53 | key_suffix = parts[0] |
| 54 | + | ||
| 55 | + # 修复:将浮点数ID转换为整数(如 "60678.0" -> "60678") | ||
| 56 | + try: | ||
| 57 | + if '.' in key_suffix: | ||
| 58 | + key_suffix = str(int(float(key_suffix))) | ||
| 59 | + except (ValueError, OverflowError): | ||
| 60 | + # 如果转换失败,保持原样 | ||
| 61 | + pass | ||
| 62 | + | ||
| 54 | value = parts[-1] | 63 | value = parts[-1] |
| 55 | redis_key = f"{key_prefix}:{key_suffix}" | 64 | redis_key = f"{key_prefix}:{key_suffix}" |
| 56 | 65 |