Spaces:
Sleeping
Sleeping
File size: 1,955 Bytes
ee2050e 1220b7a ee2050e 1220b7a ee2050e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | import base64
import json
import httpx
import sys
import os
from dotenv import load_dotenv
load_dotenv()
img_path = r'C:\Users\Benny Pepper\OneDrive - Ma Chung University\College Docs\PKM\assets\test_tcm\golden_throat_lozenges_golden_th.jpg'
def run():
with open(img_path, 'rb') as f:
img_bytes = f.read()
b64 = base64.b64encode(img_bytes).decode('utf-8')
prompt = (
'You are a TCM ingredient extractor. Look at the image and extract the ingredient names list. '
'Return ONLY a JSON array of strings — one ingredient per string. Example: ["Ginseng", "Honey"]. '
'If none, return []. Do NOT include markdown code fences, do NOT include any explanation.'
)
models = [
'google/gemini-2.0-flash-lite-001',
'google/gemini-3.1-flash-lite-preview'
]
for model in models:
payload = {
'model': model,
'messages': [
{
'role': 'user',
'content': [
{'type': 'text', 'text': prompt},
{'type': 'image_url', 'image_url': {'url': f'data:image/jpeg;base64,{b64}'}},
],
}
],
'temperature': 0.1,
}
headers = {
'Authorization': f'Bearer {os.getenv("OPENROUTER_OCR_API_KEY", "")}',
'Content-Type': 'application/json',
'HTTP-Referer': 'https://fitmate.com'
}
print(f"\nTesting {model}...")
try:
r = httpx.post('https://openrouter.ai/api/v1/chat/completions', headers=headers, json=payload, timeout=45.0)
data = r.json()
if 'choices' in data:
print("Output:", data['choices'][0]['message']['content'])
else:
print("Error payload:", data)
except Exception as e:
print("Exception:", e)
if __name__ == "__main__":
run()
|