Spaces:
Running on Zero
Running on Zero
Upload community_contributions/ederhion_week8/agents/items.py with huggingface_hub
Browse files
community_contributions/ederhion_week8/agents/items.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel
|
| 2 |
+
from datasets import load_dataset
|
| 3 |
+
from typing import Optional, List
|
| 4 |
+
|
| 5 |
+
PREFIX = "Price is $"
|
| 6 |
+
QUESTION = "What is the fair market value of this used car to the nearest dollar?"
|
| 7 |
+
|
| 8 |
+
class Item(BaseModel):
|
| 9 |
+
"""
|
| 10 |
+
An Item is a data-point representing a used car with a price.
|
| 11 |
+
"""
|
| 12 |
+
title: str
|
| 13 |
+
category: str
|
| 14 |
+
price: float
|
| 15 |
+
full: Optional[str] = None
|
| 16 |
+
weight: Optional[float] = None
|
| 17 |
+
summary: Optional[str] = None
|
| 18 |
+
prompt: Optional[str] = None
|
| 19 |
+
id: Optional[int] = None
|
| 20 |
+
|
| 21 |
+
def make_prompt(self, text: str):
|
| 22 |
+
self.prompt = f"{QUESTION}\n\n{text}\n\n{PREFIX}{round(self.price)}.00"
|
| 23 |
+
|
| 24 |
+
def test_prompt(self) -> str:
|
| 25 |
+
return self.prompt.split(PREFIX)[0] + PREFIX
|
| 26 |
+
|
| 27 |
+
def __repr__(self) -> str:
|
| 28 |
+
return f"<{self.title} = ${self.price}>"
|
| 29 |
+
|
| 30 |
+
@classmethod
|
| 31 |
+
def from_hub(cls, dataset_name: str) -> tuple[List['Item'], List['Item'], List['Item']]:
|
| 32 |
+
"""
|
| 33 |
+
Load a used car dataset from Hugging Face (HF) and map it to our Item schema.
|
| 34 |
+
We use 'Carson-Shively/used-car-price' as it is a standard automotive dataset.
|
| 35 |
+
"""
|
| 36 |
+
ds = load_dataset(dataset_name, split="train")
|
| 37 |
+
ds = ds.shuffle(seed=42)
|
| 38 |
+
|
| 39 |
+
items = []
|
| 40 |
+
for row in ds:
|
| 41 |
+
make = row.get("Make", "Unknown Make")
|
| 42 |
+
model = row.get("Model", "Unknown Model")
|
| 43 |
+
year = row.get("Year", "Unknown Year")
|
| 44 |
+
mileage = row.get("Mileage", "Unknown Mileage")
|
| 45 |
+
price = float(row.get("Price", 0.0))
|
| 46 |
+
|
| 47 |
+
if price <= 0:
|
| 48 |
+
continue
|
| 49 |
+
|
| 50 |
+
summary = f"{year} {make} {model} with {mileage} miles. Good condition used car."
|
| 51 |
+
|
| 52 |
+
item = cls(
|
| 53 |
+
title=f"{year} {make} {model}",
|
| 54 |
+
category="Automotive",
|
| 55 |
+
price=price,
|
| 56 |
+
summary=summary
|
| 57 |
+
)
|
| 58 |
+
items.append(item)
|
| 59 |
+
|
| 60 |
+
# Capping at 10,000 for high-speed local processing
|
| 61 |
+
if len(items) >= 10000:
|
| 62 |
+
break
|
| 63 |
+
|
| 64 |
+
train_size = int(len(items) * 0.8)
|
| 65 |
+
val_size = int(len(items) * 0.1)
|
| 66 |
+
|
| 67 |
+
train = items[:train_size]
|
| 68 |
+
val = items[train_size:train_size + val_size]
|
| 69 |
+
test = items[train_size + val_size:]
|
| 70 |
+
|
| 71 |
+
return train, val, test
|