Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -542,10 +542,11 @@ def ask_with_llm():
|
|
| 542 |
- Loads databases and mappings
|
| 543 |
- Parses natural-language queries
|
| 544 |
- Executes queries and renders results
|
|
|
|
| 545 |
"""
|
| 546 |
# ---------- CONFIG ----------
|
| 547 |
-
DATABASE_PATH = r"
|
| 548 |
-
STATE_RTO_JSON = r"state_rto_data.json"
|
| 549 |
|
| 550 |
TABLE_MAPPINGS = {
|
| 551 |
'fuel': {'state': 'fuel_vs_state', 'state_ev': 'fuel_vs_stateev', 'rto': 'fuel_vs_rto', 'rto_ev': 'fuel_vs_rtoev'},
|
|
@@ -606,14 +607,19 @@ def ask_with_llm():
|
|
| 606 |
return match[0]
|
| 607 |
return None
|
| 608 |
|
| 609 |
-
def parse_year_filter(q: str) -> tuple[str|None,int|None]:
|
| 610 |
ql = q.lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 611 |
if m := re.search(r"after\s*(20\d{2})", ql): return ('>', int(m.group(1)))
|
| 612 |
if m := re.search(r"before\s*(20\d{2})", ql): return ('<', int(m.group(1)))
|
| 613 |
if m := re.search(r"in\s*(20\d{2})", ql): return ('==', int(m.group(1)))
|
| 614 |
-
if m := re.search(r"\b(20\d{2})\b", ql):
|
| 615 |
return (None, None)
|
| 616 |
|
|
|
|
| 617 |
def parse_top_n(q: str) -> int|None:
|
| 618 |
ql = q.lower()
|
| 619 |
if m := re.search(r"top\s*(\d+)\s*states?\b", ql):
|
|
@@ -660,7 +666,10 @@ def ask_with_llm():
|
|
| 660 |
|
| 661 |
df = df_map[tbl_name]
|
| 662 |
if op and year:
|
| 663 |
-
|
|
|
|
|
|
|
|
|
|
| 664 |
if loc_type == 'rto':
|
| 665 |
df = df[df['column_value']==loc]
|
| 666 |
elif loc_type == 'state':
|
|
@@ -698,15 +707,30 @@ def ask_with_llm():
|
|
| 698 |
title = f"Overall total registrations{' (EV)' if ev else ''}"
|
| 699 |
result = ts.rename(columns={'count':'Total'})
|
| 700 |
fig = px.line(ts, x='year', y='count', labels={'count':'Total'})
|
|
|
|
|
|
|
| 701 |
st.dataframe(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 702 |
st.plotly_chart(fig)
|
| 703 |
-
|
| 704 |
-
st.markdown("- Top 5 states by fuel in 2022 ")
|
| 705 |
-
st.markdown("- Norm-wise registrations in Maharashtra in 2024 ")
|
| 706 |
-
st.markdown("- Electric (EV) norm-wise registrations in Karnataka after 2021 ")
|
| 707 |
-
st.markdown("- Fuel-wise registrations in Delhi in 2020")
|
| 708 |
-
st.markdown("- vehicle classes by registrations in Tamil Nadu in 2022 ")
|
| 709 |
-
|
| 710 |
|
| 711 |
|
| 712 |
# Run the entire app
|
|
@@ -738,4 +762,10 @@ elif section == "State Analysis":
|
|
| 738 |
elif section == "Vehicle Class & Category":
|
| 739 |
vehicle_class_category()
|
| 740 |
elif section == "Ask with Text (LLM)":
|
| 741 |
-
ask_with_llm()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 542 |
- Loads databases and mappings
|
| 543 |
- Parses natural-language queries
|
| 544 |
- Executes queries and renders results
|
| 545 |
+
- Allows download of resulting data
|
| 546 |
"""
|
| 547 |
# ---------- CONFIG ----------
|
| 548 |
+
DATABASE_PATH = r"D:\Data_for_query_tool\vehicle_analysis_DHRUMI.db"
|
| 549 |
+
STATE_RTO_JSON = r"D:\Data_for_query_tool\data_for_excel\state_rto_data.json"
|
| 550 |
|
| 551 |
TABLE_MAPPINGS = {
|
| 552 |
'fuel': {'state': 'fuel_vs_state', 'state_ev': 'fuel_vs_stateev', 'rto': 'fuel_vs_rto', 'rto_ev': 'fuel_vs_rtoev'},
|
|
|
|
| 607 |
return match[0]
|
| 608 |
return None
|
| 609 |
|
| 610 |
+
def parse_year_filter(q: str) -> tuple[str|None,int|tuple[int,int]|None]:
|
| 611 |
ql = q.lower()
|
| 612 |
+
# Check for year range like "from 2018 to 2022"
|
| 613 |
+
if m := re.search(r"(from|between)\s*(20\d{2})\s*(to|-)\s*(20\d{2})", ql):
|
| 614 |
+
y1, y2 = int(m.group(2)), int(m.group(4))
|
| 615 |
+
return ('range', (min(y1, y2), max(y1, y2)))
|
| 616 |
if m := re.search(r"after\s*(20\d{2})", ql): return ('>', int(m.group(1)))
|
| 617 |
if m := re.search(r"before\s*(20\d{2})", ql): return ('<', int(m.group(1)))
|
| 618 |
if m := re.search(r"in\s*(20\d{2})", ql): return ('==', int(m.group(1)))
|
| 619 |
+
if m := re.search(r"\b(20\d{2})\b", ql): return ('==', int(m.group(1)))
|
| 620 |
return (None, None)
|
| 621 |
|
| 622 |
+
|
| 623 |
def parse_top_n(q: str) -> int|None:
|
| 624 |
ql = q.lower()
|
| 625 |
if m := re.search(r"top\s*(\d+)\s*states?\b", ql):
|
|
|
|
| 666 |
|
| 667 |
df = df_map[tbl_name]
|
| 668 |
if op and year:
|
| 669 |
+
if op == 'range' and isinstance(year, tuple):
|
| 670 |
+
df = df[df['year'].between(year[0], year[1])]
|
| 671 |
+
else:
|
| 672 |
+
df = df.query(f"year {op} @year")
|
| 673 |
if loc_type == 'rto':
|
| 674 |
df = df[df['column_value']==loc]
|
| 675 |
elif loc_type == 'state':
|
|
|
|
| 707 |
title = f"Overall total registrations{' (EV)' if ev else ''}"
|
| 708 |
result = ts.rename(columns={'count':'Total'})
|
| 709 |
fig = px.line(ts, x='year', y='count', labels={'count':'Total'})
|
| 710 |
+
|
| 711 |
+
st.subheader(title)
|
| 712 |
st.dataframe(result)
|
| 713 |
+
|
| 714 |
+
# Append year info to result if applicable
|
| 715 |
+
if op and year:
|
| 716 |
+
if op == '==':
|
| 717 |
+
year_info = f"In {year}"
|
| 718 |
+
elif op == '>':
|
| 719 |
+
year_info = f"After {year}"
|
| 720 |
+
elif op == '<':
|
| 721 |
+
year_info = f"Before {year}"
|
| 722 |
+
elif op == 'range' and isinstance(year, tuple):
|
| 723 |
+
year_info = f"From {year[0]} to {year[1]}"
|
| 724 |
+
else:
|
| 725 |
+
year_info = f"Year Filter: {op} {year}"
|
| 726 |
+
result['Query_Year_Info'] = year_info
|
| 727 |
+
|
| 728 |
+
# Download option
|
| 729 |
+
csv = result.to_csv(index=False).encode('utf-8')
|
| 730 |
+
st.download_button("⬇ Download Result as CSV", data=csv, file_name="query_result.csv", mime='text/csv')
|
| 731 |
+
|
| 732 |
st.plotly_chart(fig)
|
| 733 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 734 |
|
| 735 |
|
| 736 |
# Run the entire app
|
|
|
|
| 762 |
elif section == "Vehicle Class & Category":
|
| 763 |
vehicle_class_category()
|
| 764 |
elif section == "Ask with Text (LLM)":
|
| 765 |
+
ask_with_llm()
|
| 766 |
+
st.markdown("#### 💬 Try asking:")
|
| 767 |
+
st.markdown("- Top 5 states by fuel in 2022 ")
|
| 768 |
+
st.markdown("- Norm-wise registrations in Maharashtra in 2024 ")
|
| 769 |
+
st.markdown("- Electric (EV) norm-wise registrations in Karnataka after 2021 ")
|
| 770 |
+
st.markdown("- Fuel-wise registrations in Delhi in 2020")
|
| 771 |
+
st.markdown("- Vehicle classes by registrations in Tamil Nadu in 2022 ")
|