File size: 1,645 Bytes
821310d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import openai
import firebase_admin
from firebase_admin import firestore
from firebase_setup import init_firebase
from utils.coin_tools import get_coin_data, save_coin_data
from utils.image_tools import compare_images

# Init Firebase
init_firebase()
db = firestore.client()

# Init OpenAI
openai.api_key = os.getenv("OPENAI_API_KEY")

# === USER INPUT (Simulated for now) ===
user_image_path = "images/user_coin.jpg"
suspected_coin_name = "1955 Lincoln Cent DDO-001"

# === Step 1: Check if coin already exists in Firebase ===
coin_doc = db.collection("coins").document(suspected_coin_name.replace(" ", "_")).get()

if coin_doc.exists:
    print(f"βœ… Found {suspected_coin_name} in the database. Comparing now...")
    ref_images = coin_doc.to_dict().get("reference_images", [])
    result = compare_images(user_image_path, ref_images)
    print(f"Match Confidence: {result['confidence']*100:.1f}%")
else:
    print(f"πŸ” No match found in database. Performing web search...")

    # === Step 2: Use web_search_preview tool to find data ===
    response = openai.responses.create(
        model="gpt-4.1",
        tools=[{"type": "web_search_preview"}],
        input=f"Find die marker info, image links, and estimated value for {suspected_coin_name}"
    )

    output_text = response.output[1]['content'][0]['text']
    print("πŸ“„ AI Search Result:\n", output_text)

    # === Step 3: Save AI result to Firebase ===
    coin_data = get_coin_data(suspected_coin_name, output_text, user_image_path)
    save_coin_data(suspected_coin_name, coin_data)

    print(f"πŸ’Ύ Saved new coin info to Firebase under {suspected_coin_name}.")