ashish-ninehertz commited on
Commit
2ff05a9
·
1 Parent(s): c5d14bc
Files changed (3) hide show
  1. __pycache__/main.cpython-313.pyc +0 -0
  2. main.py +2 -2
  3. web/gradio_app.py +0 -286
__pycache__/main.cpython-313.pyc CHANGED
Binary files a/__pycache__/main.cpython-313.pyc and b/__pycache__/main.cpython-313.pyc differ
 
main.py CHANGED
@@ -398,7 +398,7 @@ class RAGSystem:
398
  # Generate response using retrieved context
399
  context = "\n\n".join(hit.payload["text"] for hit in search_result)
400
  prompt = rag_prompt_template(context, query)
401
- response = self.llm.generate([prompt]).generations[0][0].text
402
 
403
  return {
404
  "status": "success",
@@ -531,5 +531,5 @@ async def websocket_endpoint(websocket: WebSocket):
531
  # Main entry point
532
  if __name__ == "__main__":
533
  Config.create_dirs()
534
- from app.web.gradio_app import launch_interface
535
  launch_interface()
 
398
  # Generate response using retrieved context
399
  context = "\n\n".join(hit.payload["text"] for hit in search_result)
400
  prompt = rag_prompt_template(context, query)
401
+ response = self.llm.generate([prompt]) # Remove .generations[0][0].text
402
 
403
  return {
404
  "status": "success",
 
531
  # Main entry point
532
  if __name__ == "__main__":
533
  Config.create_dirs()
534
+ from app import launch_interface
535
  launch_interface()
web/gradio_app.py DELETED
@@ -1,286 +0,0 @@
1
- import gradio as gr
2
- import uuid
3
- import logging
4
- from typing import List, Tuple
5
- from app.main import RAGSystem
6
- import asyncio
7
-
8
- # Configure logging
9
- logging.basicConfig(level=logging.INFO)
10
- logger = logging.getLogger(__name__)
11
-
12
- # Initialize the RAG system
13
- rag = RAGSystem()
14
-
15
- def create_session() -> str:
16
- """Create a new session ID"""
17
- return str(uuid.uuid4())
18
-
19
- def index_website(url: str, session_id: str) -> Tuple[bool, str]:
20
- """Index a website for a given session"""
21
- try:
22
- result = rag.crawl_and_index(session_id, url)
23
- if result["status"] == "success":
24
- return True, f"Successfully indexed {len(result.get('urls_processed', []))} pages"
25
- return False, result.get("message", "Unknown error during indexing")
26
- except Exception as e:
27
- logger.error(f"Indexing error: {str(e)}")
28
- return False, f"Error during indexing: {str(e)}"
29
-
30
- def chat_response(
31
- session_id: str,
32
- message: str,
33
- model_choice: str,
34
- ollama_url: str,
35
- gemini_api_key: str,
36
- chat_history: List[dict]
37
- ) -> Tuple[List[dict], str]:
38
- """Generate a chat response with proper error handling"""
39
- if not session_id:
40
- chat_history.append({"role": "user", "content": f"🧑‍💻 {message}"})
41
- chat_history.append({"role": "assistant", "content": "🤖 Please index a website first or enter a valid session ID"})
42
- return chat_history, ""
43
-
44
- try:
45
- response = asyncio.run(rag.chat(
46
- session_id=session_id,
47
- question=message,
48
- model=model_choice.lower(),
49
- ollama_url=ollama_url if model_choice == "mistral" else None,
50
- gemini_api_key=gemini_api_key if model_choice == "gemini" else None
51
- ))
52
-
53
- if response["status"] == "success":
54
- answer = response["response"]
55
- sources = "\n\nSources:\n" + "\n".join(
56
- f"- {src['source_url']}" for src in response.get("sources", [])
57
- ) if response.get("sources") else ""
58
- full_response = f"🤖 {answer}{sources}"
59
- else:
60
- full_response = f"🤖 Error: {response.get('message', 'Unknown error')}"
61
-
62
- chat_history.append({"role": "user", "content": f"🧑‍💻 {message}"})
63
- chat_history.append({"role": "assistant", "content": full_response})
64
- return chat_history, ""
65
- except Exception as e:
66
- logger.error(f"Chat error: {str(e)}")
67
- chat_history.append({"role": "user", "content": f"🧑‍💻 {message}"})
68
- chat_history.append({"role": "assistant", "content": f"🤖 System error: {str(e)}"})
69
- return chat_history, ""
70
-
71
- def toggle_model_inputs(model_choice: str) -> List[gr.update]:
72
- """Show/hide model-specific inputs"""
73
- if model_choice == "mistral":
74
- return [gr.update(visible=True), gr.update(visible=False)]
75
- return [gr.update(visible=False), gr.update(visible=True)]
76
-
77
- def load_session(existing_session_id: str) -> Tuple[str, str]:
78
- """Load an existing session"""
79
- if existing_session_id:
80
- # Here you might want to add validation if the session exists
81
- return existing_session_id, f"Loaded existing session: {existing_session_id}"
82
- return "", "Please enter a valid session ID"
83
-
84
- def get_session(self, session_id: str):
85
- # If session exists in memory, return it
86
- if session_id in self.sessions:
87
- return self.sessions[session_id]
88
- # If not, check if Qdrant collection exists and has documents
89
- collection_name = self.get_collection_name(session_id)
90
- try:
91
- results = self.qdrant_client.scroll(collection_name=collection_name, limit=1)
92
- if results and results[0]:
93
- # Rehydrate session in memory
94
- self.sessions[session_id] = {
95
- "documents": [], # Optionally, you can fetch all docs if needed
96
- "history": []
97
- }
98
- return self.sessions[session_id]
99
- except Exception as e:
100
- logger.warning(f"Session {session_id} not found in Qdrant: {e}")
101
- # If not found, return None or raise
102
- raise ValueError("No documents indexed for this session")
103
-
104
- # Custom CSS for better styling
105
- custom_css = """
106
- .gradio-container {
107
- max-width: 1200px !important;
108
- margin: 0 auto !important;
109
- }
110
- .dark .gradio-container {
111
- background: #1e1e2e !important;
112
- }
113
- #chatbot {
114
- min-height: 500px;
115
- border-radius: 12px !important;
116
- }
117
- .message.user {
118
- border-left: 4px solid #4f46e5 !important;
119
- }
120
- .message.assistant {
121
- border-left: 4px solid #10b981 !important;
122
- }
123
- .btn-primary {
124
- background: linear-gradient(to right, #4f46e5, #7c3aed) !important;
125
- border: none !important;
126
- }
127
- .btn-primary:hover {
128
- background: linear-gradient(to right, #4338ca, #6d28d9) !important;
129
- }
130
- .prose {
131
- max-width: 100% !important;
132
- }
133
- """
134
-
135
- with gr.Blocks(title="RAG Chat with Mistral/Gemini", css=custom_css, theme="soft") as demo:
136
- # Header section
137
- with gr.Row():
138
- gr.Markdown("""
139
- # 🌐 RAG Chat Assistant
140
- ### Chat with any website using Mistral or Gemini
141
- """)
142
-
143
- # Session state
144
- session_id = gr.State("")
145
-
146
- with gr.Tabs():
147
- with gr.TabItem("📚 Index Website"):
148
- with gr.Row():
149
- with gr.Column():
150
- gr.Markdown("### Step 1: Configure and Index")
151
- with gr.Group():
152
- url_input = gr.Textbox(
153
- label="Website URL to index",
154
- placeholder="https://example.com",
155
- interactive=True,
156
- lines=1
157
- )
158
-
159
- with gr.Row():
160
- model_choice = gr.Radio(
161
- choices=["mistral", "gemini"],
162
- label="Select Model",
163
- value="mistral",
164
- interactive=True
165
- )
166
-
167
- index_btn = gr.Button(
168
- "🚀 Index Website",
169
- variant="primary",
170
- scale=0
171
- )
172
-
173
- with gr.Accordion("🔐 Model Settings", open=False):
174
- ollama_url = gr.Textbox(
175
- label="Ollama URL (required for Mistral)",
176
- placeholder="http://localhost:11434",
177
- visible=True
178
- )
179
-
180
- gemini_api_key = gr.Textbox(
181
- label="Gemini API Key (required for Gemini)",
182
- placeholder="your-api-key-here",
183
- visible=False,
184
- type="password"
185
- )
186
-
187
- status_output = gr.Textbox(
188
- label="Status",
189
- interactive=False,
190
- elem_classes="prose"
191
- )
192
-
193
- gr.Markdown("""
194
- **Instructions:**
195
- 1. Enter a website URL
196
- 2. Select your preferred model
197
- 3. Configure model settings if needed
198
- 4. Click 'Index Website'
199
- """)
200
-
201
- with gr.TabItem("💬 Chat"):
202
- with gr.Row():
203
- with gr.Column(scale=2):
204
- # New session ID input for resuming sessions
205
- with gr.Accordion("🔍 Resume Previous Session", open=False):
206
- existing_session_input = gr.Textbox(
207
- label="Enter existing Session ID",
208
- placeholder="Paste your session ID here...",
209
- interactive=True
210
- )
211
- load_session_btn = gr.Button(
212
- "🔁 Load Session",
213
- variant="secondary"
214
- )
215
- session_status = gr.Textbox(
216
- label="Session Status",
217
- interactive=False
218
- )
219
-
220
- chatbot = gr.Chatbot(
221
- label="Chat History",
222
- height=500,
223
- avatar_images=(None, None),
224
- show_copy_button=True,
225
- type="messages" # Use the new format
226
- )
227
-
228
- with gr.Row():
229
- message_input = gr.Textbox(
230
- label="Type your message",
231
- placeholder="Ask about the website content...",
232
- interactive=True,
233
- container=False,
234
- scale=7,
235
- autofocus=True
236
- )
237
-
238
- send_btn = gr.Button(
239
- "Send",
240
- variant="primary",
241
- scale=1,
242
- min_width=100
243
- )
244
-
245
- # Event handlers
246
- model_choice.change(
247
- fn=toggle_model_inputs,
248
- inputs=model_choice,
249
- outputs=[ollama_url, gemini_api_key]
250
- )
251
-
252
- index_btn.click(
253
- fn=create_session,
254
- outputs=session_id
255
- ).success(
256
- fn=index_website,
257
- inputs=[url_input, session_id],
258
- outputs=[status_output]
259
- )
260
-
261
- # New handler for loading existing sessions
262
- load_session_btn.click(
263
- fn=load_session,
264
- inputs=[existing_session_input],
265
- outputs=[session_id, session_status]
266
- )
267
-
268
- send_btn.click(
269
- fn=chat_response,
270
- inputs=[session_id, message_input, model_choice, ollama_url, gemini_api_key, chatbot],
271
- outputs=[chatbot, message_input]
272
- )
273
-
274
- # Allow submitting with Enter key
275
- message_input.submit(
276
- fn=chat_response,
277
- inputs=[session_id, message_input, model_choice, ollama_url, gemini_api_key, chatbot],
278
- outputs=[chatbot, message_input]
279
- )
280
-
281
- if __name__ == "__main__":
282
- demo.launch(
283
- server_name="0.0.0.0",
284
- server_port=7860,
285
- favicon_path="https://www.gradio.app/assets/favicon.ico"
286
- )