Spaces:
Runtime error
Runtime error
syurein
commited on
Commit
·
48a4145
1
Parent(s):
53952f9
moondreamも引っかかたので削除
Browse files- LLM_package.py +1 -36
LLM_package.py
CHANGED
|
@@ -1,45 +1,10 @@
|
|
| 1 |
from google import genai
|
| 2 |
-
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
from PIL import Image
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
load_dotenv()
|
| 8 |
-
class MoondreamInference:
|
| 9 |
-
def __init__(self, api_key=None):
|
| 10 |
-
if api_key is None:
|
| 11 |
-
api_key = os.getenv('MOONDREAM_API_KEY')
|
| 12 |
-
self.model = md.vl(api_key=api_key)
|
| 13 |
-
|
| 14 |
-
def get_response(self, image_path, prompt):
|
| 15 |
-
"""
|
| 16 |
-
COCOEvaluator は get_response を呼ぶので、
|
| 17 |
-
ここで Moondream の detect を内部で呼び、結果を JSON文字列で返す
|
| 18 |
-
"""
|
| 19 |
-
image = Image.open(image_path)
|
| 20 |
-
cat = list(prompt) # prompt を直接カテゴリ名に使う
|
| 21 |
-
result = self.model.detect(image, list(cat)[0])
|
| 22 |
-
# Moondream はすでに dict なので JSON にして返す
|
| 23 |
-
return json.dumps(result["objects"])
|
| 24 |
-
|
| 25 |
-
def parse_response(self, resp_text):
|
| 26 |
-
"""
|
| 27 |
-
get_response で返した JSON文字列をパースし、
|
| 28 |
-
Gemini と同じ形式の list[dict] に揃える
|
| 29 |
-
"""
|
| 30 |
-
|
| 31 |
-
detections = json.loads(resp_text)
|
| 32 |
-
parsed = []
|
| 33 |
-
for obj in detections:
|
| 34 |
-
parsed.append({
|
| 35 |
-
"label": obj.get("label", "object"), # ない場合もあるかも
|
| 36 |
-
"box_2d": [
|
| 37 |
-
obj["y_min"], obj["x_min"],
|
| 38 |
-
obj["y_max"], obj["x_max"]
|
| 39 |
-
]
|
| 40 |
-
})
|
| 41 |
-
|
| 42 |
-
return parsed
|
| 43 |
|
| 44 |
|
| 45 |
class GeminiInference:
|
|
|
|
| 1 |
from google import genai
|
| 2 |
+
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
from PIL import Image
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
load_dotenv()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
class GeminiInference:
|