DrishtiSharma commited on
Commit
04e6bd6
Β·
verified Β·
1 Parent(s): 0be614c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -69
app.py CHANGED
@@ -3,42 +3,60 @@ from rag_app import WebRAG
3
  import time
4
  import os
5
 
6
- # Set page configuration
7
  st.set_page_config(
8
  page_title="Web RAG Assistant",
9
  page_icon="🌐",
10
  layout="wide"
11
  )
12
 
13
- # Custom CSS
14
- st.markdown("""
 
15
  <style>
16
- .stApp {
17
- max-width: 1200px;
18
- margin: 0 auto;
19
- }
20
- .chat-container {
21
- border-radius: 10px;
22
- padding: 20px;
23
- background-color: #f0f2f6;
24
- margin: 10px 0;
25
- }
26
- .user-message {
27
- background-color: #2e7bf3;
28
- color: white;
29
- padding: 15px;
30
- border-radius: 15px;
31
- margin: 5px 0;
32
- }
33
- .assistant-message {
34
- background-color: #white;
35
- padding: 15px;
36
- border-radius: 15px;
37
- margin: 5px 0;
38
- border: 1px solid #e0e0e0;
39
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  </style>
41
- """, unsafe_allow_html=True)
 
 
42
 
43
  # Initialize session state
44
  if 'rag' not in st.session_state:
@@ -55,86 +73,72 @@ def reset_chat_history():
55
  st.session_state.chat_history = []
56
  st.session_state.current_url = ""
57
 
58
- # Header
59
- st.title("🌐 Web RAG Assistant")
60
- st.markdown("### Ask questions about any webpage")
61
-
62
- # Sidebar
63
  with st.sidebar:
64
- st.header("Settings")
65
  url = st.text_input("Enter webpage URL:")
66
 
67
- # Add scraping method selection
68
  scraping_method = st.selectbox(
69
  "Select Scraping Method",
70
  ["beautifulsoup", "scrapegraph", "crawl4ai"],
71
  help="""
72
- BeautifulSoup: Basic HTML parsing, faster but less sophisticated
73
- ScrapeGraph: AI-powered scraping, better at understanding content but slower
74
- Crawl4ai: Advanced async crawler with good JavaScript support
75
  """
76
  )
77
 
78
- if st.button("Process URL", type="primary"):
79
  if url:
80
- # Check if URL has changed
81
  if url != st.session_state.current_url:
82
  reset_chat_history()
83
  st.session_state.current_url = url
84
 
85
- with st.spinner("Processing URL... This may take a moment."):
86
  try:
87
  st.session_state.rag.crawl_and_process(url, scraping_method)
88
  st.session_state.url_processed = True
89
- st.success("URL processed successfully!")
90
- st.rerun() # Rerun the app to refresh the chat interface
91
  except Exception as e:
92
- st.error(f"Error processing URL: {str(e)}")
93
  else:
94
- st.warning("Please enter a URL")
95
 
96
  st.divider()
97
- st.markdown("### How to use")
98
- st.markdown("""
99
- 1. Enter a webpage URL in the input field
100
- 2. Click 'Process URL' to analyze the content
101
- 3. Ask questions about the webpage content
102
- 4. Get AI-powered answers based on the content
103
- """)
 
 
104
 
105
- # Main chat interface
 
 
106
  st.divider()
107
 
108
  # Display chat messages
109
  for message in st.session_state.chat_history:
110
  if message["role"] == "user":
111
- st.markdown(f"""
112
- <div class="user-message">
113
- {message["content"]}
114
- </div>
115
- """, unsafe_allow_html=True)
116
  else:
117
- st.markdown(f"""
118
- <div class="assistant-message">
119
- {message["content"]}
120
- </div>
121
- """, unsafe_allow_html=True)
122
 
123
- # Chat input
124
  if st.session_state.url_processed:
125
- question = st.chat_input("Ask a question about the webpage...")
126
  if question:
127
- # Add user message to chat history
128
  st.session_state.chat_history.append({"role": "user", "content": question})
129
-
130
- # Get answer from RAG
131
  with st.spinner("Thinking..."):
132
  try:
133
  answer = st.session_state.rag.ask_question(
134
  question,
135
  [(msg["content"], msg["content"]) for msg in st.session_state.chat_history if msg["role"] == "assistant"]
136
  )
137
- # Add assistant message to chat history
138
  st.session_state.chat_history.append({"role": "assistant", "content": answer})
139
  st.rerun()
140
  except Exception as e:
 
3
  import time
4
  import os
5
 
6
+ # Page Configuration
7
  st.set_page_config(
8
  page_title="Web RAG Assistant",
9
  page_icon="🌐",
10
  layout="wide"
11
  )
12
 
13
+ # Custom CSS for better aesthetics
14
+ st.markdown(
15
+ """
16
  <style>
17
+ .stApp {
18
+ max-width: 1200px;
19
+ margin: 0 auto;
20
+ font-family: 'Arial', sans-serif;
21
+ }
22
+ .sidebar .sidebar-content {
23
+ background-color: #1E1E1E;
24
+ color: white;
25
+ padding: 20px;
26
+ }
27
+ .css-1d391kg {
28
+ padding: 0;
29
+ }
30
+ .stTextInput, .stSelectbox, .stButton, .stChatInput {
31
+ border-radius: 10px;
32
+ padding: 10px;
33
+ }
34
+ .chat-container {
35
+ border-radius: 10px;
36
+ padding: 20px;
37
+ background-color: #f0f2f6;
38
+ margin: 10px 0;
39
+ }
40
+ .user-message {
41
+ background-color: #2e7bf3;
42
+ color: white;
43
+ padding: 15px;
44
+ border-radius: 15px;
45
+ margin: 5px 0;
46
+ text-align: right;
47
+ }
48
+ .assistant-message {
49
+ background-color: white;
50
+ padding: 15px;
51
+ border-radius: 15px;
52
+ margin: 5px 0;
53
+ border: 1px solid #e0e0e0;
54
+ text-align: left;
55
+ }
56
  </style>
57
+ """,
58
+ unsafe_allow_html=True
59
+ )
60
 
61
  # Initialize session state
62
  if 'rag' not in st.session_state:
 
73
  st.session_state.chat_history = []
74
  st.session_state.current_url = ""
75
 
76
+ # Sidebar on the extreme left
 
 
 
 
77
  with st.sidebar:
78
+ st.header("βš™οΈ Settings")
79
  url = st.text_input("Enter webpage URL:")
80
 
 
81
  scraping_method = st.selectbox(
82
  "Select Scraping Method",
83
  ["beautifulsoup", "scrapegraph", "crawl4ai"],
84
  help="""
85
+ - BeautifulSoup: Basic HTML parsing, faster but less sophisticated
86
+ - ScrapeGraph: AI-powered scraping, better at understanding content but slower
87
+ - Crawl4ai: Advanced async crawler with good JavaScript support
88
  """
89
  )
90
 
91
+ if st.button("πŸš€ Process URL"):
92
  if url:
 
93
  if url != st.session_state.current_url:
94
  reset_chat_history()
95
  st.session_state.current_url = url
96
 
97
+ with st.spinner("Processing URL... Please wait."):
98
  try:
99
  st.session_state.rag.crawl_and_process(url, scraping_method)
100
  st.session_state.url_processed = True
101
+ st.success("βœ… URL processed successfully!")
102
+ st.rerun()
103
  except Exception as e:
104
+ st.error(f"❌ Error processing URL: {str(e)}")
105
  else:
106
+ st.warning("⚠️ Please enter a URL")
107
 
108
  st.divider()
109
+ st.markdown("### πŸ“Œ How to use")
110
+ st.markdown(
111
+ """
112
+ 1. Enter a webpage URL
113
+ 2. Click 'Process URL' to analyze the content
114
+ 3. Ask questions about the webpage
115
+ 4. Receive answers
116
+ """
117
+ )
118
 
119
+ # Main Content
120
+ st.title("🌐 Web RAG Assistant")
121
+ st.markdown("### Ask questions about any webpage")
122
  st.divider()
123
 
124
  # Display chat messages
125
  for message in st.session_state.chat_history:
126
  if message["role"] == "user":
127
+ st.markdown(f"""<div class='user-message'>{message["content"]}</div>""", unsafe_allow_html=True)
 
 
 
 
128
  else:
129
+ st.markdown(f"""<div class='assistant-message'>{message["content"]}</div>""", unsafe_allow_html=True)
 
 
 
 
130
 
131
+ # Chat Input
132
  if st.session_state.url_processed:
133
+ question = st.chat_input("πŸ’¬ Ask a question about the webpage...")
134
  if question:
 
135
  st.session_state.chat_history.append({"role": "user", "content": question})
 
 
136
  with st.spinner("Thinking..."):
137
  try:
138
  answer = st.session_state.rag.ask_question(
139
  question,
140
  [(msg["content"], msg["content"]) for msg in st.session_state.chat_history if msg["role"] == "assistant"]
141
  )
 
142
  st.session_state.chat_history.append({"role": "assistant", "content": answer})
143
  st.rerun()
144
  except Exception as e: