File size: 4,922 Bytes
b22038d
 
 
 
 
04e6bd6
b22038d
 
 
 
 
 
04e6bd6
 
b22038d
04e6bd6
 
 
 
 
 
 
 
 
effc948
 
 
 
 
 
04e6bd6
 
 
 
effc948
 
 
 
04e6bd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b22038d
04e6bd6
 
 
b22038d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04e6bd6
b22038d
04e6bd6
b22038d
 
 
 
 
 
04e6bd6
 
 
b22038d
 
 
04e6bd6
b22038d
 
 
 
 
04e6bd6
b22038d
 
 
04e6bd6
 
b22038d
04e6bd6
b22038d
04e6bd6
b22038d
 
04e6bd6
 
 
 
 
 
effc948
04e6bd6
 
b22038d
04e6bd6
effc948
04e6bd6
 
b22038d
 
 
 
 
04e6bd6
b22038d
04e6bd6
b22038d
04e6bd6
b22038d
04e6bd6
b22038d
 
 
 
 
 
 
 
 
 
 
 
 
 
effc948
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import streamlit as st
from rag_app import WebRAG
import time
import os

# Page Configuration
st.set_page_config(
    page_title="Web RAG Assistant",
    page_icon="🌐",
    layout="wide"
)

st.markdown(
    """
    <style>
        .stApp {
            max-width: 1200px;
            margin: 0 auto;
            font-family: 'Arial', sans-serif;
        }
        .sidebar .sidebar-content {
            background-color: #1E1E1E;
            color: white;
            padding: 20px;
            position: fixed;
            left: 0;
            top: 0;
            height: 100vh;
            width: 280px;
            z-index: 999;
        }
        .css-1d391kg {
            padding: 0;
        }
        .main-content {
            margin-left: 300px;
            padding: 20px;
        }
        .stTextInput, .stSelectbox, .stButton, .stChatInput {
            border-radius: 10px;
            padding: 10px;
        }
        .chat-container {
            border-radius: 10px;
            padding: 20px;
            background-color: #f0f2f6;
            margin: 10px 0;
        }
        .user-message {
            background-color: #2e7bf3;
            color: white;
            padding: 15px;
            border-radius: 15px;
            margin: 5px 0;
            text-align: right;
        }
        .assistant-message {
            background-color: white;
            padding: 15px;
            border-radius: 15px;
            margin: 5px 0;
            border: 1px solid #e0e0e0;
            text-align: left;
        }
    </style>
    """,
    unsafe_allow_html=True
)

# Initialize session state
if 'rag' not in st.session_state:
    st.session_state.rag = WebRAG()
if 'chat_history' not in st.session_state:
    st.session_state.chat_history = []
if 'url_processed' not in st.session_state:
    st.session_state.url_processed = False
if 'current_url' not in st.session_state:
    st.session_state.current_url = ""

# Function to reset chat history
def reset_chat_history():
    st.session_state.chat_history = []
    st.session_state.current_url = ""

# Sidebar on the extreme left
with st.sidebar:
    st.header("βš™οΈ Settings")
    url = st.text_input("Enter webpage URL:")
    
    scraping_method = st.selectbox(
        "Select Scraping Method",
        ["beautifulsoup", "scrapegraph", "crawl4ai"],
        help="""
        - BeautifulSoup: Basic HTML parsing, faster but less sophisticated
        - ScrapeGraph: AI-powered scraping, better at understanding content but slower
        - Crawl4ai: Advanced async crawler with good JavaScript support
        """
    )
    
    if st.button("πŸš€ Process URL"):
        if url:
            if url != st.session_state.current_url:
                reset_chat_history()
                st.session_state.current_url = url
            
            with st.spinner("Processing URL... Please wait."):
                try:
                    st.session_state.rag.crawl_and_process(url, scraping_method)
                    st.session_state.url_processed = True
                    st.success("βœ… URL processed successfully!")
                    st.rerun()
                except Exception as e:
                    st.error(f"❌ Error processing URL: {str(e)}")
        else:
            st.warning("⚠️ Please enter a URL")
    
    st.divider()
    st.markdown("### πŸ“Œ How to use")
    st.markdown(
        """
        1. Enter a webpage URL
        2. Click 'Process URL' to analyze the content
        3. Ask questions about the webpage
        4. Receive AI-powered answers
        """
    )

# Main Content
st.markdown('<div class="main-content">', unsafe_allow_html=True)
st.title("🌐 Web RAG Assistant")
st.markdown("### Ask questions about any webpage")
st.divider()

# Display chat messages
for message in st.session_state.chat_history:
    if message["role"] == "user":
        st.markdown(f"""<div class='user-message'>{message["content"]}</div>""", unsafe_allow_html=True)
    else:
        st.markdown(f"""<div class='assistant-message'>{message["content"]}</div>""", unsafe_allow_html=True)

# Chat Input
if st.session_state.url_processed:
    question = st.chat_input("πŸ’¬ Ask a question about the webpage...")
    if question:
        st.session_state.chat_history.append({"role": "user", "content": question})
        with st.spinner("Thinking..."):
            try:
                answer = st.session_state.rag.ask_question(
                    question,
                    [(msg["content"], msg["content"]) for msg in st.session_state.chat_history if msg["role"] == "assistant"]
                )
                st.session_state.chat_history.append({"role": "assistant", "content": answer})
                st.rerun()
            except Exception as e:
                st.error(f"Error: {str(e)}")
else:
    st.info("πŸ‘ˆ Please process a URL first using the sidebar")
st.markdown('</div>', unsafe_allow_html=True)