coin_identifier / app.py
user6295018's picture
Update app.py
6eda913 verified
import pandas as pd # Make sure pandas is imported
import gradio as gr
from io import StringIO
# Read the dataset file as a string
with open('coindata.jsonl', 'r') as f:
data = f.read()
# Use StringIO to read the JSONL data
coin_dataset = pd.read_json(StringIO(data), lines=True)
# Print dataset to verify its structure
print(coin_dataset.head())
# Coin identification function
def identify_coin(denomination, year, mint):
denomination = denomination.strip().lower() # Convert input to lowercase and strip spaces
mint = mint.strip().lower() # Convert mint to lowercase and strip spaces
try:
year_int = int(year)
except ValueError:
return "Please enter a valid year."
print(f"Looking for denomination: {denomination}, year: {year_int}, mint: {mint}")
matches = []
for _, row in coin_dataset.iterrows():
if row["denomination"].lower() != denomination:
continue
mint_list = [m.strip().lower() for m in row["mint"].split(",")]
if mint not in mint_list:
continue
year_range = row["year"].replace("–", "-").replace(" to ", "-").split("-")
try:
start = int(year_range[0].strip())
end = int(year_range[1].strip()) if len(year_range) > 1 else start
if start <= year_int <= end:
coin_type = row.get("type", None)
type_line = f"Type: {coin_type}" if pd.notna(coin_type) else "Type: Not specified"
match_text = f"Coin: {row['coin name']}\n{type_line}\nDefects: {row['defects']}"
matches.append(match_text)
except Exception as e:
print(f"Error processing year range: {e}")
continue
if matches:
return "\n\n".join(matches)
else:
return "Coin not found."
# Gradio Interface
iface = gr.Interface(
fn=identify_coin,
inputs=[
gr.Dropdown(choices=["Cent", "5 Cent", "10 Cent", "25 Cent"], label="Denomination (e.g., Cent, Nickel, Dime)"),
gr.Textbox(label="Year (e.g., 1916)"),
gr.Dropdown(choices=["Philadelphia", "Denver", "San Francisco", "New Orleans", "Carson City", "West Point"], label="Mint (e.g., Philadelphia)"),
],
outputs="text",
title="Coin Identifier",
description="Enter details to identify a U.S. coin and common defects."
)
# Launch the Gradio app
iface.launch()