"""
Streamlit UI for API Query Parser
This app provides an interactive interface for parsing natural language queries
and matching them to relevant APIs using NLP and fuzzy matching.
"""
import streamlit as st
import sys
from pathlib import Path
import time
# Add src directory to path for imports
sys.path.insert(0, str(Path(__file__).parent))
# Import our parser
from parser import parse_query
# ============================================================================
# PAGE CONFIGURATION
# ============================================================================
st.set_page_config(
page_title="API Query Parser",
page_icon="🔍",
layout="wide",
initial_sidebar_state="collapsed"
)
# ============================================================================
# CUSTOM CSS STYLING
# ============================================================================
st.markdown("""
""", unsafe_allow_html=True)
# ============================================================================
# SESSION STATE INITIALIZATION
# ============================================================================
# Initialize session state variables
if 'query_history' not in st.session_state:
st.session_state.query_history = []
if 'last_query' not in st.session_state:
st.session_state.last_query = ""
if 'results' not in st.session_state:
st.session_state.results = None
if 'selected_example' not in st.session_state:
st.session_state.selected_example = ""
# ============================================================================
# HELPER FUNCTIONS
# ============================================================================
def display_keywords(keywords):
"""Display extracted keywords as styled badges."""
if keywords:
st.markdown("### 🔑 Extracted Keywords")
# Create HTML for keyword badges
badges_html = ""
for keyword in keywords:
badges_html += f'{keyword}'
st.markdown(badges_html, unsafe_allow_html=True)
else:
st.info("No keywords extracted from the query.")
def get_confidence_category(score):
"""Categorize confidence score into high/medium/low."""
if score >= 85:
return "high", "🟢"
elif score >= 70:
return "medium", "🟡"
else:
return "low", "🔴"
def display_api_matches(ranked_apis):
"""Display matched APIs as styled cards with confidence scores."""
if ranked_apis:
st.markdown("### 🎯 Matched APIs")
for api in ranked_apis:
api_name = api.get("api_name", "Unknown")
description = api.get("description", "No description available")
score = api.get("score", 0)
# Get confidence category and emoji
category, emoji = get_confidence_category(score)
# Create card HTML
card_class = f"api-card api-card-{category}"
st.markdown(f"""
{emoji} {api_name}
{description}
Confidence Score:
""", unsafe_allow_html=True)
# Display progress bar for confidence score
st.progress(score / 100)
st.caption(f"{score}% match")
st.markdown("
", unsafe_allow_html=True)
else:
st.warning("⚠️ No matching APIs found. Try rephrasing your query or use different keywords.")
def process_query(query):
"""Process the query and return results."""
try:
with st.spinner("🔄 Parsing your query..."):
start_time = time.time()
results = parse_query(query)
end_time = time.time()
processing_time = round((end_time - start_time) * 1000, 2)
return results, processing_time
except Exception as e:
st.error(f"❌ An error occurred while processing your query: {str(e)}")
st.info("💡 Please check that all dependencies are installed and the API catalog is available.")
return None, 0
# ============================================================================
# MAIN UI LAYOUT
# ============================================================================
# Header Section
st.markdown('🔍 API Query Parser
', unsafe_allow_html=True)
st.markdown(
'',
unsafe_allow_html=True
)
st.markdown("---")
# Introduction
st.markdown("""
Welcome to the **API Query Parser**! This tool helps you find the right API by simply describing what you need in natural language.
Just type your query below, and we'll analyze it to suggest the most relevant APIs from our catalog.
""")
# ============================================================================
# EXAMPLE QUERIES SECTION
# ============================================================================
st.markdown("### 💡 Try These Example Queries")
example_queries = [
"Get customer details for John Smith",
"What's the product stock for laptops?",
"Check inventory levels for wireless headphones",
"I need customer information",
"Show me product availability"
]
# Create columns for example queries
col1, col2 = st.columns(2)
with col1:
if st.button(example_queries[0], key="ex1", use_container_width=True):
st.session_state.selected_example = example_queries[0]
if st.button(example_queries[1], key="ex2", use_container_width=True):
st.session_state.selected_example = example_queries[1]
if st.button(example_queries[2], key="ex3", use_container_width=True):
st.session_state.selected_example = example_queries[2]
with col2:
if st.button(example_queries[3], key="ex4", use_container_width=True):
st.session_state.selected_example = example_queries[3]
if st.button(example_queries[4], key="ex5", use_container_width=True):
st.session_state.selected_example = example_queries[4]
st.markdown("---")
# ============================================================================
# QUERY INPUT SECTION
# ============================================================================
st.markdown("### ✍️ Enter Your Query")
# Use selected example if available
default_query = st.session_state.selected_example if st.session_state.selected_example else ""
# Create two columns for input and buttons
input_col, button_col = st.columns([4, 1])
with input_col:
user_query = st.text_input(
"Type your natural language query here:",
value=default_query,
placeholder="e.g., Get customer details or Check product inventory",
label_visibility="collapsed"
)
with button_col:
parse_button = st.button("🔍 Parse Query", type="primary", use_container_width=True)
clear_button = st.button("🗑️ Clear", use_container_width=True)
# Clear button functionality
if clear_button:
st.session_state.selected_example = ""
st.session_state.last_query = ""
st.session_state.results = None
st.rerun()
# ============================================================================
# QUERY PROCESSING AND RESULTS DISPLAY
# ============================================================================
# Process query when button is clicked or example is selected
if parse_button or st.session_state.selected_example:
# Reset selected example after processing
if st.session_state.selected_example:
user_query = st.session_state.selected_example
st.session_state.selected_example = ""
# Validate query
if not user_query or user_query.strip() == "":
st.warning("⚠️ Please enter a query before parsing.")
else:
# Process the query
results, processing_time = process_query(user_query)
if results:
# Store results in session state
st.session_state.results = results
st.session_state.last_query = user_query
# Add to query history
if user_query not in st.session_state.query_history:
st.session_state.query_history.insert(0, user_query)
# Keep only last 10 queries
st.session_state.query_history = st.session_state.query_history[:10]
# Display results if available
if st.session_state.results:
st.markdown("---")
st.markdown("## 📊 Results")
# Display original query
st.markdown(f"""
📝 Your Query:
"{st.session_state.last_query}"
""", unsafe_allow_html=True)
# Create two columns for results
col_left, col_right = st.columns([1, 1])
with col_left:
# Display keywords
keywords = st.session_state.results.get("extracted_keywords", [])
display_keywords(keywords)
with col_right:
# Display processing time
st.markdown("### ⚡ Performance")
st.metric("Processing Time", "< 1 second")
st.metric("Keywords Found", len(keywords))
st.metric("APIs Matched", len(st.session_state.results.get("ranked_api_matches", [])))
st.markdown("
", unsafe_allow_html=True)
# Display API matches
ranked_apis = st.session_state.results.get("ranked_api_matches", [])
display_api_matches(ranked_apis)
# ============================================================================
# FOOTER
# ============================================================================
st.markdown("---")
st.markdown("""
API Query Parser v1.0 | Powered by spaCy, RapidFuzz, and LangGraph | Built using Streamlit
""", unsafe_allow_html=True)