ST-x-Tony commited on
Commit
7a682ac
·
verified ·
1 Parent(s): 9afda58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -19
app.py CHANGED
@@ -20,13 +20,42 @@ import spaces
20
  # ============================================================
21
 
22
  APP_NAME = "X-RUDRA"
23
- VERSION = "3.2.0" # bumped version
24
 
25
  M1_REPO = os.getenv("M1_REPO", "Shrijanagain/M1")
26
  M2_REPO = os.getenv("M2_REPO", "Shrijanagain/M2")
27
  PORT = int(os.getenv("PORT", "7860"))
28
 
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  # ============================================================
31
  # LAZY ENGINE
32
  # ============================================================
@@ -62,7 +91,7 @@ def safe_dict(value):
62
 
63
 
64
  # ============================================================
65
- # FORMATTERS (unchanged)
66
  # ============================================================
67
 
68
  def format_sources(sources):
@@ -137,7 +166,6 @@ def extract_answer(data):
137
  val = data.get(key)
138
  if isinstance(val, str) and val.strip():
139
  return val.strip()
140
- # If no answer, we'll build one from results in the caller
141
  return None
142
 
143
 
@@ -180,7 +208,7 @@ def build_activity(data, elapsed_ms):
180
 
181
 
182
  # ============================================================
183
- # RESEARCH FUNCTION – FIXED TO HANDLE ENGINE OUTPUT
184
  # ============================================================
185
 
186
  async def do_research(question, max_results, max_rounds, use_models, freshness):
@@ -188,6 +216,27 @@ async def do_research(question, max_results, max_rounds, use_models, freshness):
188
  return [], "⚪ Enter a question to start.", "", ""
189
 
190
  question = str(question).strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
  started = time.perf_counter()
192
 
193
  try:
@@ -202,18 +251,17 @@ async def do_research(question, max_results, max_rounds, use_models, freshness):
202
  data = safe_dict(report)
203
  elapsed_ms = int((time.perf_counter() - started) * 1000)
204
 
205
- # Debug: print raw data to logs (helps with debugging)
206
  print("\n" + "="*60)
207
  print("RAW ENGINE DATA:")
208
- print(json.dumps(data, indent=2, default=str)[:3000]) # first 3000 chars
209
  print("="*60 + "\n")
210
 
211
  # ------------------------------------------------------------
212
- # 1. EXTRACT SOURCES – convert 'results' to 'sources' if needed
213
  # ------------------------------------------------------------
214
  sources = data.get("sources", [])
215
  if not sources:
216
- # Engine uses 'results' – convert each to source format
217
  results = data.get("results", [])
218
  for res in results:
219
  if isinstance(res, dict):
@@ -225,15 +273,13 @@ async def do_research(question, max_results, max_rounds, use_models, freshness):
225
  "source_score": res.get("rank", "N/A"),
226
  "description": res.get("snippet", ""),
227
  })
228
- # Store back for activity and formatters
229
  data["sources"] = sources
230
 
231
  # ------------------------------------------------------------
232
- # 2. EXTRACT ANSWER – if missing, synthesize from sources
233
  # ------------------------------------------------------------
234
  answer = extract_answer(data)
235
  if answer is None:
236
- # Build a simple answer from the top sources
237
  if sources:
238
  top = sources[:5]
239
  parts = [f"Based on the top results:"]
@@ -246,15 +292,9 @@ async def do_research(question, max_results, max_rounds, use_models, freshness):
246
  answer = "No information found. Try rephrasing your question."
247
 
248
  # ------------------------------------------------------------
249
- # 3. CLAIMS – if missing, we can keep empty or derive from snippets
250
  # ------------------------------------------------------------
251
  claims = data.get("claims", [])
252
- # (Optional) You could generate simple claims from each source's snippet,
253
- # but we'll leave it empty for now.
254
-
255
- # ------------------------------------------------------------
256
- # 4. BUILD OUTPUTS
257
- # ------------------------------------------------------------
258
  sources_md = format_sources(sources)
259
  evidence_md = format_evidence(claims)
260
  verification_md = format_verification(data.get("contradictions", []))
@@ -304,7 +344,7 @@ def health_check():
304
 
305
 
306
  # ============================================================
307
- # CSS AND UI (unchanged)
308
  # ============================================================
309
 
310
  CSS = """
 
20
  # ============================================================
21
 
22
  APP_NAME = "X-RUDRA"
23
+ VERSION = "3.2.1" # bumped version
24
 
25
  M1_REPO = os.getenv("M1_REPO", "Shrijanagain/M1")
26
  M2_REPO = os.getenv("M2_REPO", "Shrijanagain/M2")
27
  PORT = int(os.getenv("PORT", "7860"))
28
 
29
 
30
+ # ============================================================
31
+ # CASUAL QUERY DETECTOR
32
+ # ============================================================
33
+
34
+ def is_casual_query(text: str) -> bool:
35
+ """Return True if the query is casual/timepass, False if it's a serious research question."""
36
+ text = text.lower().strip()
37
+
38
+ # Very short queries (1‑2 words) are almost always casual
39
+ if len(text.split()) <= 2:
40
+ return True
41
+
42
+ # List of casual patterns (expand as needed)
43
+ casual_patterns = [
44
+ "hey", "hi", "hello", "yo", "what's up", "how are you",
45
+ "good morning", "good evening", "good night",
46
+ "lol", "haha", "just testing", "timepass", "nothing",
47
+ "tell me a joke", "sing a song", "what's your name",
48
+ "who are you", "what can you do", "help", "thanks"
49
+ ]
50
+
51
+ # Check if the query starts with or contains any casual pattern
52
+ for pattern in casual_patterns:
53
+ if text.startswith(pattern) or pattern in text:
54
+ return True
55
+
56
+ return False
57
+
58
+
59
  # ============================================================
60
  # LAZY ENGINE
61
  # ============================================================
 
91
 
92
 
93
  # ============================================================
94
+ # FORMATTERS
95
  # ============================================================
96
 
97
  def format_sources(sources):
 
166
  val = data.get(key)
167
  if isinstance(val, str) and val.strip():
168
  return val.strip()
 
169
  return None
170
 
171
 
 
208
 
209
 
210
  # ============================================================
211
+ # RESEARCH FUNCTION – WITH CASUAL FILTER
212
  # ============================================================
213
 
214
  async def do_research(question, max_results, max_rounds, use_models, freshness):
 
216
  return [], "⚪ Enter a question to start.", "", ""
217
 
218
  question = str(question).strip()
219
+
220
+ # ------------------------------------------------------------
221
+ # 1. CASUAL FILTER – skip expensive search for timepass
222
+ # ------------------------------------------------------------
223
+ if is_casual_query(question):
224
+ casual_response = (
225
+ f"👋 Hey there! I'm X‑RUDRA, your research assistant. "
226
+ f"I'm designed to help with serious questions, deep dives, and fact‑finding. "
227
+ f"If you have a specific topic you'd like me to research, just ask!\n\n"
228
+ f"*(Your message `{question}` seemed casual, so I skipped the heavy research.)*"
229
+ )
230
+ history = [
231
+ {"role": "user", "content": question},
232
+ {"role": "assistant", "content": casual_response}
233
+ ]
234
+ empty_activity = "⚡ Skipped research (casual query)."
235
+ return history, empty_activity, "", "", ""
236
+
237
+ # ------------------------------------------------------------
238
+ # 2. SERIOUS QUERY – run the engine
239
+ # ------------------------------------------------------------
240
  started = time.perf_counter()
241
 
242
  try:
 
251
  data = safe_dict(report)
252
  elapsed_ms = int((time.perf_counter() - started) * 1000)
253
 
254
+ # Debug: print raw data to logs
255
  print("\n" + "="*60)
256
  print("RAW ENGINE DATA:")
257
+ print(json.dumps(data, indent=2, default=str)[:3000])
258
  print("="*60 + "\n")
259
 
260
  # ------------------------------------------------------------
261
+ # 3. EXTRACT SOURCES – convert 'results' to 'sources' if needed
262
  # ------------------------------------------------------------
263
  sources = data.get("sources", [])
264
  if not sources:
 
265
  results = data.get("results", [])
266
  for res in results:
267
  if isinstance(res, dict):
 
273
  "source_score": res.get("rank", "N/A"),
274
  "description": res.get("snippet", ""),
275
  })
 
276
  data["sources"] = sources
277
 
278
  # ------------------------------------------------------------
279
+ # 4. EXTRACT ANSWER – if missing, synthesize from sources
280
  # ------------------------------------------------------------
281
  answer = extract_answer(data)
282
  if answer is None:
 
283
  if sources:
284
  top = sources[:5]
285
  parts = [f"Based on the top results:"]
 
292
  answer = "No information found. Try rephrasing your question."
293
 
294
  # ------------------------------------------------------------
295
+ # 5. BUILD OUTPUTS
296
  # ------------------------------------------------------------
297
  claims = data.get("claims", [])
 
 
 
 
 
 
298
  sources_md = format_sources(sources)
299
  evidence_md = format_evidence(claims)
300
  verification_md = format_verification(data.get("contradictions", []))
 
344
 
345
 
346
  # ============================================================
347
+ # CSS AND UI
348
  # ============================================================
349
 
350
  CSS = """