MohamedFahim commited on
Commit
3b5a9d4
Β·
1 Parent(s): c1ac2e5

modified ui

Browse files
Files changed (2) hide show
  1. __pycache__/main_api.cpython-312.pyc +0 -0
  2. interface.py +133 -181
__pycache__/main_api.cpython-312.pyc ADDED
Binary file (17.8 kB). View file
 
interface.py CHANGED
@@ -52,8 +52,8 @@ def check_api_health():
52
  except:
53
  return False
54
 
55
- def process_web_rag(url, prompt, data_source, progress=gr.Progress()):
56
- """Main processing function with progress tracking"""
57
  if not url or not prompt:
58
  return "❌ Error: Please provide both URL and prompt", "", ""
59
 
@@ -62,28 +62,34 @@ def process_web_rag(url, prompt, data_source, progress=gr.Progress()):
62
  return "❌ Error: FastAPI service is not available. Please wait a moment and try again.", "", ""
63
 
64
  try:
65
- progress(0.1, desc="Starting process...")
 
 
 
66
 
67
- if data_source == "Multiple links (first 5)":
68
- progress(0.2, desc="πŸ” Extracting links from webpage...")
69
- links = extract_links(url)
70
- sample_links = links[:5]
71
-
72
- progress(0.4, desc="πŸ“„ Extracting text from multiple pages...")
73
- file_path = extract_text(sample_links)
74
-
75
- status_msg = f"βœ… Processed {len(sample_links)} pages from {len(links)} total links found"
76
- else:
77
- progress(0.3, desc="πŸ“„ Extracting text from homepage...")
78
- file_path = extract_text([url])
79
- status_msg = "βœ… Processed homepage content"
80
 
81
- progress(0.7, desc="πŸ€– Performing RAG analysis...")
82
- result = perform_rag(file_path, prompt)
83
 
84
- progress(1.0, desc="βœ… Complete!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
- # Format the response
87
  response_text = f"**Query:** {result['user_query']}\n\n**Response:** {result['assistant_response']}"
88
  sources_text = result['sources']
89
 
@@ -92,175 +98,121 @@ def process_web_rag(url, prompt, data_source, progress=gr.Progress()):
92
  except Exception as e:
93
  return f"❌ Error: {str(e)}", "", ""
94
 
95
- # Custom CSS for modern styling
96
- custom_css = """
97
- .gradio-container {
98
- max-width: 900px !important;
99
- margin: auto !important;
100
- }
101
-
102
- .header-text {
103
- text-align: center;
104
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
105
- -webkit-background-clip: text;
106
- -webkit-text-fill-color: transparent;
107
- font-size: 2.5em;
108
- font-weight: bold;
109
- margin-bottom: 0.5em;
110
- }
111
-
112
- .description-text {
113
- text-align: center;
114
- color: #666;
115
- font-size: 1.1em;
116
- margin-bottom: 2em;
117
- }
118
-
119
- .input-group {
120
- background: #f8f9fa;
121
- padding: 1.5em;
122
- border-radius: 12px;
123
- margin: 1em 0;
124
- border: 1px solid #e9ecef;
125
- }
126
-
127
- .output-group {
128
- background: #ffffff;
129
- border-radius: 12px;
130
- border: 1px solid #dee2e6;
131
- margin: 1em 0;
132
- }
133
-
134
- .status-box {
135
- padding: 1em;
136
- border-radius: 8px;
137
- margin: 0.5em 0;
138
- }
139
-
140
- .status-success {
141
- background-color: #d4edda;
142
- border-color: #c3e6cb;
143
- color: #155724;
144
- }
145
-
146
- .status-error {
147
- background-color: #f8d7da;
148
- border-color: #f5c6cb;
149
- color: #721c24;
150
- }
151
- """
152
 
153
- # Create the Gradio interface
154
- with gr.Blocks(css=custom_css, title="Web RAG System", theme=gr.themes.Soft()) as app:
155
- # Header
156
- gr.HTML("""
157
- <div class="header-text">🌐 Web RAG System</div>
158
- <div class="description-text">
159
- Extract content from web pages and ask questions using AI-powered retrieval
160
- </div>
161
- """)
162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  with gr.Row():
164
- with gr.Column(scale=1):
165
- # Input section
166
- gr.HTML('<div style="font-size: 1.2em; font-weight: bold; margin-bottom: 1em;">πŸ“ Input Configuration</div>')
 
 
 
 
 
 
 
 
 
167
 
168
- url_input = gr.Textbox(
169
- label="πŸ”— Website URL",
170
- placeholder="https://example.com",
171
- info="Enter the URL you want to analyze"
172
- )
173
-
174
- prompt_input = gr.Textbox(
175
- label="❓ Your Question",
176
- placeholder="What is this website about?",
177
- lines=3,
178
- info="Ask any question about the content"
179
- )
180
 
181
- data_source = gr.Radio(
182
- choices=["Multiple links (first 5)", "Homepage only"],
183
- value="Multiple links (first 5)",
184
- label="πŸ“Š Data Source",
185
- info="Choose how much content to analyze"
186
  )
187
 
188
- process_btn = gr.Button(
189
- "πŸš€ Analyze Website",
190
- variant="primary",
191
- size="lg"
192
- )
193
-
194
- # Output section
195
- gr.HTML('<div style="font-size: 1.2em; font-weight: bold; margin: 2em 0 1em 0;">πŸ“‹ Results</div>')
196
-
197
- status_output = gr.Textbox(
198
- label="πŸ“Š Processing Status",
199
- interactive=False,
200
- show_label=True
201
- )
202
-
203
- with gr.Row():
204
- with gr.Column(scale=2):
205
- response_output = gr.Textbox(
206
- label="πŸ€– AI Response",
207
- lines=8,
208
- interactive=False,
209
- show_label=True
210
- )
211
-
212
- with gr.Column(scale=1):
213
- sources_output = gr.Textbox(
214
- label="πŸ“š Sources",
215
- lines=8,
216
- interactive=False,
217
- show_label=True
218
- )
219
-
220
- # Example section
221
- gr.HTML("""
222
- <div style="margin-top: 2em; padding: 1.5em; background: #f8f9fa; border-radius: 12px; border-left: 4px solid #667eea;">
223
- <h3 style="margin-top: 0; color: #333;">πŸ’‘ Example Usage</h3>
224
- <p><strong>URL:</strong> https://openai.com</p>
225
- <p><strong>Question:</strong> What are the main products and services offered?</p>
226
- <p><strong>Data Source:</strong> Multiple links (first 5)</p>
227
- </div>
228
- """)
229
-
230
- # Add a note about the system status
231
- gr.HTML("""
232
- <div style="margin-top: 1em; padding: 1em; background: #e3f2fd; border-radius: 8px; border-left: 4px solid #2196f3;">
233
- <p style="margin: 0; color: #0d47a1;">
234
- ℹ️ <strong>Note:</strong> If you encounter connection errors, please wait a moment for the system to initialize and try again.
235
- </p>
236
- </div>
237
- """)
238
-
239
- # Connect the function
240
- process_btn.click(
241
- fn=process_web_rag,
242
- inputs=[url_input, prompt_input, data_source],
243
- outputs=[status_output, response_output, sources_output],
244
- show_progress=True
245
- )
246
-
247
- # Add keyboard shortcut
248
- url_input.submit(
249
- fn=process_web_rag,
250
- inputs=[url_input, prompt_input, data_source],
251
- outputs=[status_output, response_output, sources_output],
252
- show_progress=True
253
- )
254
-
255
- prompt_input.submit(
256
- fn=process_web_rag,
257
- inputs=[url_input, prompt_input, data_source],
258
- outputs=[status_output, response_output, sources_output],
259
- show_progress=True
260
- )
261
-
262
  if __name__ == "__main__":
263
- app.launch(
264
  server_name="0.0.0.0",
265
  server_port=7860,
266
  share=False,
 
52
  except:
53
  return False
54
 
55
+ def process_multiple_links(url, prompt):
56
+ """Process multiple links from a webpage"""
57
  if not url or not prompt:
58
  return "❌ Error: Please provide both URL and prompt", "", ""
59
 
 
62
  return "❌ Error: FastAPI service is not available. Please wait a moment and try again.", "", ""
63
 
64
  try:
65
+ links = extract_links(url)
66
+ sample_links = links[:5]
67
+ file_path = extract_text(sample_links)
68
+ result = perform_rag(file_path, prompt)
69
 
70
+ status_msg = f"βœ… Processed {len(sample_links)} pages from {len(links)} total links found"
71
+ response_text = f"**Query:** {result['user_query']}\n\n**Response:** {result['assistant_response']}"
72
+ sources_text = result['sources']
 
 
 
 
 
 
 
 
 
 
73
 
74
+ return status_msg, response_text, sources_text
 
75
 
76
+ except Exception as e:
77
+ return f"❌ Error: {str(e)}", "", ""
78
+
79
+ def process_homepage_only(url, prompt):
80
+ """Process homepage content only"""
81
+ if not url or not prompt:
82
+ return "❌ Error: Please provide both URL and prompt", "", ""
83
+
84
+ # Check API health first
85
+ if not check_api_health():
86
+ return "❌ Error: FastAPI service is not available. Please wait a moment and try again.", "", ""
87
+
88
+ try:
89
+ file_path = extract_text([url])
90
+ result = perform_rag(file_path, prompt)
91
 
92
+ status_msg = "βœ… Processed homepage content"
93
  response_text = f"**Query:** {result['user_query']}\n\n**Response:** {result['assistant_response']}"
94
  sources_text = result['sources']
95
 
 
98
  except Exception as e:
99
  return f"❌ Error: {str(e)}", "", ""
100
 
101
+ def extract_links_only(url):
102
+ """Extract and display links from a webpage"""
103
+ if not url:
104
+ return "❌ Error: Please provide a URL"
105
+
106
+ # Check API health first
107
+ if not check_api_health():
108
+ return "❌ Error: FastAPI service is not available. Please wait a moment and try again."
109
+
110
+ try:
111
+ links = extract_links(url)
112
+ links_text = "\n".join([f"β€’ {link}" for link in links])
113
+ return f"βœ… Found {len(links)} links:\n\n{links_text}"
114
+
115
+ except Exception as e:
116
+ return f"❌ Error: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
+ def extract_text_only(url):
119
+ """Extract and display text from a webpage"""
120
+ if not url:
121
+ return "❌ Error: Please provide a URL"
122
+
123
+ # Check API health first
124
+ if not check_api_health():
125
+ return "❌ Error: FastAPI service is not available. Please wait a moment and try again."
 
126
 
127
+ try:
128
+ file_path = extract_text([url])
129
+ return f"βœ… Text extracted and saved to: {file_path}"
130
+
131
+ except Exception as e:
132
+ return f"❌ Error: {str(e)}"
133
+
134
+ # Create individual interfaces for each tab
135
+ multiple_links_demo = gr.Interface(
136
+ process_multiple_links,
137
+ inputs=[
138
+ gr.Textbox(label="πŸ”— Website URL", placeholder="https://example.com"),
139
+ gr.Textbox(label="❓ Your Question", placeholder="What is this website about?", lines=3)
140
+ ],
141
+ outputs=[
142
+ gr.Textbox(label="πŸ“Š Status"),
143
+ gr.Textbox(label="πŸ€– AI Response", lines=8),
144
+ gr.Textbox(label="πŸ“š Sources", lines=6)
145
+ ],
146
+ title=" \n🌐 Multiple Links Analysis",
147
+ allow_flagging="never"
148
+ )
149
+
150
+ homepage_only_demo = gr.Interface(
151
+ process_homepage_only,
152
+ inputs=[
153
+ gr.Textbox(label="πŸ”— Website URL", placeholder="https://example.com"),
154
+ gr.Textbox(label="❓ Your Question", placeholder="What is this website about?", lines=3)
155
+ ],
156
+ outputs=[
157
+ gr.Textbox(label="πŸ“Š Status"),
158
+ gr.Textbox(label="πŸ€– AI Response", lines=8),
159
+ gr.Textbox(label="πŸ“š Sources", lines=6)
160
+ ],
161
+ title=" \n🏠 Homepage Only Analysis",
162
+ allow_flagging="never"
163
+ )
164
+
165
+ # Tab names
166
+ tab_names = ["Multiple Links Analysis", "Homepage Only Analysis"]
167
+
168
+ # Main interface with tabs
169
+ with gr.Blocks(theme=gr.themes.Soft()) as interface:
170
  with gr.Row():
171
+ with gr.Column(scale=1, min_width=1200):
172
+ gr.Markdown("## \n\n<style>h1, h2, h3, h4, h5, h6 { font-size: 40px !important; text-align:center !important; display: block !important;}</style>", label="")
173
+ gr.HTML("""
174
+ <div style="text-align: center; margin: 2em 0;">
175
+ <h1 style="font-size: 3em; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent; margin-bottom: 0.5em;">
176
+ 🌐 Web RAG System
177
+ </h1>
178
+ <p style="font-size: 1.2em; color: #666; margin-bottom: 1em;">
179
+ Extract content from web pages and ask questions using AI-powered retrieval
180
+ </p>
181
+ </div>
182
+ """)
183
 
184
+ # Add custom CSS for increasing font size of text areas and toggles
185
+ gr.HTML("""
186
+ <style>
187
+ textarea, input[type="text"], .gr-button {
188
+ font-size: 1.2em !important;
189
+ }
190
+ </style>
191
+ """)
 
 
 
 
192
 
193
+ tabbed_interface = gr.TabbedInterface(
194
+ [multiple_links_demo, homepage_only_demo],
195
+ tab_names
 
 
196
  )
197
 
198
+ # Add example and notes at the bottom
199
+ gr.HTML("""
200
+ <div style="margin-top: 2em; padding: 1.5em; background: #f8f9fa; border-radius: 12px; border-left: 4px solid #667eea;">
201
+ <h3 style="margin-top: 0; color: #333;">πŸ’‘ Example Usage</h3>
202
+ <p><strong>URL:</strong> https://openai.com</p>
203
+ <p><strong>Question:</strong> What are the main products and services offered?</p>
204
+ </div>
205
+
206
+ <div style="margin-top: 1em; padding: 1em; background: #e3f2fd; border-radius: 8px; border-left: 4px solid #2196f3;">
207
+ <p style="margin: 0; color: #0d47a1;">
208
+ ℹ️ <strong>Note:</strong> If you encounter connection errors, please wait a moment for the system to initialize and try again.
209
+ </p>
210
+ </div>
211
+ """)
212
+
213
+ # Launch the interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
  if __name__ == "__main__":
215
+ interface.launch(
216
  server_name="0.0.0.0",
217
  server_port=7860,
218
  share=False,