File size: 3,203 Bytes
42cabf2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
@st.cache_resource
def init_sql_agent():
    return SQLAgent("./sakila.db")

@st.cache_resource
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")