Beginner Project: Fuel Trip Planner with Hugging Face Transformers

Hey Hugging Face community! I’m new here, diving into Python and AI through Hugging Face’s awesome tools, and I’m excited to share a project idea I’m working on. As a road trip enthusiast, I want to build a simple fuel trip planner that uses a Hugging Face transformer model to parse natural language inputs (e.g., “I’m driving 500 km at 12 km/liter”) and output fuel cost estimates. I got inspired by a clean fuel calculator I found at kalkulatorpaliwa.com.pl, which handles inputs so smoothly, and I’d love to replicate that vibe with NLP.

Here’s a basic script using transformers to extract trip details:

from transformers import pipeline
import re

def parse_trip_details(text):
    nlp = pipeline("ner", model="dslim/bert-base-NER")
    entities = nlp(text)
    
    distance = None
    efficiency = None
    for entity in entities:
        if "km" in entity["word"].lower():
            distance = float(re.search(r"\d+", entity["word"]).group())
        if "km/liter" in entity["word"].lower():
            efficiency = float(re.search(r"\d+", entity["word"]).group())
    
    if distance and efficiency:
        fuel_needed = distance / efficiency
        return {"distance": distance, "efficiency": efficiency, "fuel_needed": fuel_needed}
    return {"error": "Couldn’t parse trip details"}

# Example
text = "I’m driving 500 km at 12 km/liter"
result = parse_trip_details(text)
print(result)  # Outputs: {'distance': 500.0, 'efficiency': 12.0, 'fuel_needed': 41.666666666666664}

I’m aiming to add a fuel price input and calculate costs, maybe even suggest optimal routes. As a beginner, I’m struggling with a few things:

  • Is dslim/bert-base-NER a good choice for extracting numbers and units, or is there a better model for this?
  • How can I improve the regex to handle varied inputs (e.g., “300 kilometers, 10 km per liter”)?
  • Any tips for integrating this with a simple CLI or web interface?

I’d love to make it as user-friendly as kalkulatorpaliwa.com.pl. Any advice on model selection, text parsing, or cool features to add? Thanks for helping a newbie out!

1 Like

Any tips for integrating this with a simple CLI or web interface?

I think Python’s standard argparse or Google’s Fire are good choices. The code for these well-known libraries is familiar to many people and easy to read.
If you’re looking for a GUI, Gradio or Streamlit are convenient options. Gradio is particularly versatile and recommended because it can also be used via API. However, it does have various minor issues, so you can easily resolve them by searching online or asking questions in the Gradio-specific channel on the HF Discord forum.

regex

Hmm… You might want to use a small LLM for this.

# not perfect
import re
pattern = r"(\d+\.?\d*|\.\d+)\s*(km|kilometers?|mi|miles|km/liter|km per liter)"
matches = re.findall(pattern, text.lower())

Is dslim/bert-base-NER a good choice for extracting numbers and units, or is there a better model for this?

I think this is suit for that purpose, too.