Spaces:
Running
on
Zero
Running
on
Zero
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}.") | |