|
|
|
|
|
|
|
|
|
|
|
|
|
|
import sys, os |
|
|
import streamlit as st |
|
|
|
|
|
|
|
|
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/..") |
|
|
|
|
|
from query_engine.parser import parse_query |
|
|
from query_engine.logic_engine import run_query |
|
|
from answer_generator.citation_manager import get_source |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.set_page_config(page_title="πΎ Project Samarth β Intelligent Q&A", layout="centered") |
|
|
|
|
|
|
|
|
try: |
|
|
st.markdown("<style>" + open("ui/style.css").read() + "</style>", unsafe_allow_html=True) |
|
|
except Exception as e: |
|
|
st.warning("β οΈ Could not load CSS file. Using default Streamlit styling.") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.title("πΎ Project Samarth β Intelligent Q&A System") |
|
|
st.caption("Ask intelligent, data-driven questions about agriculture and climate using live datasets from data.gov.in.") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
query = st.text_area( |
|
|
"π§ Ask your question:", |
|
|
height=100, |
|
|
placeholder="Example: Compare rainfall and rice production in Andaman and Nicobar Islands and Andhra Pradesh for the last 5 years" |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if st.button("π Analyze"): |
|
|
if not query.strip(): |
|
|
st.warning("Please enter a valid question.") |
|
|
else: |
|
|
with st.spinner("Analyzing your question..."): |
|
|
try: |
|
|
|
|
|
parsed_query = parse_query(query) |
|
|
|
|
|
|
|
|
result = run_query(parsed_query) |
|
|
|
|
|
|
|
|
source = get_source(parsed_query.get("query_type")) |
|
|
|
|
|
|
|
|
st.markdown("---") |
|
|
st.markdown("### π Result Summary") |
|
|
|
|
|
if "error" in result: |
|
|
st.error(result["error"]) |
|
|
|
|
|
elif "message" in result: |
|
|
st.info(result["message"]) |
|
|
|
|
|
else: |
|
|
states = parsed_query.get("states", []) |
|
|
crop = parsed_query.get("crop", "") |
|
|
st.markdown(f"### π Analysis for {', '.join(states)} β Crop: {crop.title()}") |
|
|
|
|
|
|
|
|
if "rainfall_summary" in result: |
|
|
st.markdown("#### π§οΈ Average Rainfall (mm):") |
|
|
for state, value in result["rainfall_summary"].items(): |
|
|
st.markdown(f"- **{state.title()}**: `{value}` mm") |
|
|
|
|
|
|
|
|
if "production_summary" in result: |
|
|
st.markdown("#### πΎ Total Production (tonnes):") |
|
|
for state, value in result["production_summary"].items(): |
|
|
st.markdown(f"- **{state.title()}**: `{int(value)}` tonnes") |
|
|
|
|
|
st.markdown("---") |
|
|
st.markdown(f"π **Data Source:** {source}") |
|
|
st.caption("Developed for Project Samarth β Integrating Agriculture & Climate Data π¦οΈπΎ") |
|
|
|
|
|
except Exception as e: |
|
|
st.error(f"β Something went wrong: {e}") |
|
|
|