Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
from agents.sql_agent import SQLAgent | |
from agents.rag_agent import RAGAgent | |
import os | |
from dotenv import load_dotenv | |
load_dotenv() | |
st.set_page_config(page_title="Q&A Chatbot", layout="wide") | |
st.title(" Q&A Chatbot") | |
# Initializing agents | |
def init_sql_agent(): | |
return SQLAgent("./sakila.db") | |
def init_rag_agent(): | |
return RAGAgent() | |
mode = st.sidebar.radio( | |
"Select Mode:", | |
["Movie Database (SQL)", "News Search (RAG)"] | |
) | |
st.markdown("---") | |
if mode == "Movie Database (SQL)": | |
st.subheader(" Movie Database (SQL)") | |
sql_question = st.text_input("Ask about movies:", placeholder="Please enter your nlp sql question here ", key="sql_input") | |
if sql_question: | |
sql_agent = init_sql_agent() | |
with st.spinner("Querying database..."): | |
answer = sql_agent.query(sql_question) | |
# Display answer | |
st.markdown("### Answer") | |
st.write(answer) | |
else: | |
st.subheader(" News Search (RAG)") | |
# RAG input | |
rag_question = st.text_input("Ask about news:", placeholder="What's happening around the world?", key="rag_input") | |
if rag_question: | |
rag_agent = init_rag_agent() | |
with st.spinner("Searching news..."): | |
result = rag_agent.search(rag_question) | |
st.markdown("### Answer") | |
st.info(result['answer']) | |
# Sources section with full chunks | |
st.markdown("### Source Articles") | |
for j, chunk in enumerate(result['chunks']): | |
with st.container(): | |
col1, col2 = st.columns([3, 1]) | |
with col1: | |
st.markdown(f"**Article {j+1}**") | |
with col2: | |
st.markdown(f"**{chunk['category']}**") | |
# Full text in a text area for better readability | |
st.text_area( | |
label="", | |
value=chunk['text'], | |
height=150, | |
disabled=True, | |
key=f"chunk_{j}" | |
) | |
# Score if available | |
if chunk.get('score', 0) > 0: | |
st.caption(f"Relevance Score: {chunk['score']:.1%}") | |
st.markdown("---") | |
# Sidebar | |
st.sidebar.markdown("---") | |
st.sidebar.markdown("### Example Questions") | |
if mode == "Movie Database (SQL)": | |
st.sidebar.markdown(""" | |
- How many films are there? | |
- Show me the top 5 longest films | |
- Which actors have the most films? | |
- List all film categories | |
- How many customers do we have? | |
""") | |
else: | |
st.sidebar.markdown(""" | |
- What's happening with oil prices? | |
- Tell me about technology news | |
- Any sports updates? | |
- Business news today | |
- Science discoveries | |
""") | |
st.sidebar.markdown("---") | |
st.sidebar.caption("Created by Bharath Gajula") | |
st.sidebar.caption("Powered by Gemini & LangChain") |