mgbam commited on
Commit
e1707aa
·
verified ·
1 Parent(s): eeb0aa2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -25
app.py CHANGED
@@ -1,6 +1,10 @@
1
  # ------------------------------
2
- # Imports & Dependencies
3
  # ------------------------------
 
 
 
 
4
  from langchain_openai import OpenAIEmbeddings
5
  from langchain_community.vectorstores import Chroma
6
  from langchain_core.messages import HumanMessage, AIMessage, ToolMessage
@@ -13,7 +17,6 @@ from typing import Sequence, Dict, List, Optional, Any
13
  import chromadb
14
  import re
15
  import os
16
- import streamlit as st
17
  import requests
18
  import hashlib
19
  import json
@@ -64,9 +67,9 @@ Format: Markdown with LaTeX mathematical notation where applicable
64
  # Validate API key configuration
65
  if not ResearchConfig.DEEPSEEK_API_KEY:
66
  st.error("""**Research Portal Configuration Required**
67
- 1. Obtain DeepSeek API key: [platform.deepseek.com](https://platform.deepseek.com/)
68
- 2. Configure secret: `DEEPSEEK_API_KEY` in Space settings
69
- 3. Rebuild deployment""")
70
  st.stop()
71
 
72
  # ------------------------------
@@ -87,7 +90,7 @@ class QuantumDocumentManager:
87
  separators=["\n\n", "\n", "|||"]
88
  )
89
  docs = splitter.create_documents(documents)
90
- # Log how many chunks were created
91
  st.write(f"Created {len(docs)} chunks for collection '{collection_name}'")
92
  return Chroma.from_documents(
93
  documents=docs,
@@ -202,7 +205,7 @@ class CognitiveProcessor:
202
  valid = [r for r in results if "error" not in r]
203
  if not valid:
204
  return {"error": "All API requests failed"}
205
- # Choose the result with the longest content
206
  return max(valid, key=lambda x: len(x.get('choices', [{}])[0].get('message', {}).get('content', '')))
207
 
208
  # ------------------------------
@@ -251,7 +254,6 @@ class ResearchWorkflow:
251
  try:
252
  query = state["context"]["raw_query"]
253
  docs = retriever.retrieve(query, "research")
254
- # Log the retrieval result for debugging
255
  st.write(f"[DEBUG] Retrieved {len(docs)} documents from retrieval node.")
256
  return {
257
  "messages": [AIMessage(content=f"Retrieved {len(docs)} documents")],
@@ -265,11 +267,11 @@ class ResearchWorkflow:
265
 
266
  def analyze_content(self, state: AgentState) -> Dict:
267
  try:
268
- # Ensure documents are present before proceeding
269
  if "documents" not in state["context"] or not state["context"]["documents"]:
270
  return self._error_state("No documents retrieved; please check your query or retrieval process.")
271
 
272
- # Concatenate all document content for analysis
273
  docs = "\n\n".join([d.page_content for d in state["context"]["documents"] if hasattr(d, "page_content")])
274
  st.write(f"[DEBUG] Analyzing content from {len(state['context']['documents'])} documents.")
275
  prompt = ResearchConfig.ANALYSIS_TEMPLATE.format(context=docs)
@@ -320,7 +322,6 @@ Improve:
320
 
321
  def _quality_check(self, state: AgentState) -> str:
322
  content = state["messages"][-1].content
323
- # Check for the keyword "VALID" in the output; if missing, trigger refinement
324
  return "valid" if "VALID" in content else "invalid"
325
 
326
  def _error_state(self, message: str) -> Dict:
@@ -337,14 +338,7 @@ Improve:
337
  class ResearchInterface:
338
  def __init__(self):
339
  self.workflow = ResearchWorkflow()
340
- self._initialize_interface()
341
-
342
- def _initialize_interface(self):
343
- st.set_page_config(
344
- page_title="NeuroResearch AI",
345
- layout="wide",
346
- initial_sidebar_state="expanded"
347
- )
348
  self._inject_styles()
349
  self._build_sidebar()
350
  self._build_main_interface()
@@ -410,7 +404,7 @@ class ResearchInterface:
410
  def _build_main_interface(self):
411
  st.title("🧠 NeuroResearch AI")
412
  query = st.text_area("Research Query:", height=200,
413
- placeholder="Enter technical research question...")
414
 
415
  if st.button("Execute Analysis", type="primary"):
416
  self._execute_analysis(query)
@@ -421,10 +415,8 @@ class ResearchInterface:
421
  results = self.workflow.app.stream(
422
  {"messages": [HumanMessage(content=query)], "context": {}, "metadata": {}}
423
  )
424
-
425
  for event in results:
426
  self._render_event(event)
427
-
428
  st.success("✅ Analysis Completed Successfully")
429
  except Exception as e:
430
  st.error(f"""**Analysis Failed**
@@ -438,7 +430,6 @@ Potential issues:
438
  if 'ingest' in event:
439
  with st.container():
440
  st.success("✅ Query Ingested")
441
-
442
  elif 'retrieve' in event:
443
  with st.container():
444
  docs = event['retrieve']['context']['documents']
@@ -447,13 +438,11 @@ Potential issues:
447
  for i, doc in enumerate(docs, 1):
448
  st.markdown(f"**Document {i}**")
449
  st.code(doc.page_content, language='text')
450
-
451
  elif 'analyze' in event:
452
  with st.container():
453
  content = event['analyze']['messages'][0].content
454
  with st.expander("Technical Analysis Report", expanded=True):
455
  st.markdown(content)
456
-
457
  elif 'validate' in event:
458
  with st.container():
459
  content = event['validate']['messages'][0].content
 
1
  # ------------------------------
2
+ # Imports & Initial Configuration
3
  # ------------------------------
4
+ import streamlit as st
5
+ # Set the page configuration immediately—this must be the first Streamlit command.
6
+ st.set_page_config(page_title="NeuroResearch AI", layout="wide", initial_sidebar_state="expanded")
7
+
8
  from langchain_openai import OpenAIEmbeddings
9
  from langchain_community.vectorstores import Chroma
10
  from langchain_core.messages import HumanMessage, AIMessage, ToolMessage
 
17
  import chromadb
18
  import re
19
  import os
 
20
  import requests
21
  import hashlib
22
  import json
 
67
  # Validate API key configuration
68
  if not ResearchConfig.DEEPSEEK_API_KEY:
69
  st.error("""**Research Portal Configuration Required**
70
+ 1. Obtain DeepSeek API key: [platform.deepseek.com](https://platform.deepseek.com/)
71
+ 2. Configure secret: `DEEPSEEK_API_KEY` in Space settings
72
+ 3. Rebuild deployment""")
73
  st.stop()
74
 
75
  # ------------------------------
 
90
  separators=["\n\n", "\n", "|||"]
91
  )
92
  docs = splitter.create_documents(documents)
93
+ # Debug: log the number of chunks created for the collection.
94
  st.write(f"Created {len(docs)} chunks for collection '{collection_name}'")
95
  return Chroma.from_documents(
96
  documents=docs,
 
205
  valid = [r for r in results if "error" not in r]
206
  if not valid:
207
  return {"error": "All API requests failed"}
208
+ # Choose the result with the longest content for robustness.
209
  return max(valid, key=lambda x: len(x.get('choices', [{}])[0].get('message', {}).get('content', '')))
210
 
211
  # ------------------------------
 
254
  try:
255
  query = state["context"]["raw_query"]
256
  docs = retriever.retrieve(query, "research")
 
257
  st.write(f"[DEBUG] Retrieved {len(docs)} documents from retrieval node.")
258
  return {
259
  "messages": [AIMessage(content=f"Retrieved {len(docs)} documents")],
 
267
 
268
  def analyze_content(self, state: AgentState) -> Dict:
269
  try:
270
+ # Ensure documents are present before proceeding.
271
  if "documents" not in state["context"] or not state["context"]["documents"]:
272
  return self._error_state("No documents retrieved; please check your query or retrieval process.")
273
 
274
+ # Concatenate all document content for analysis.
275
  docs = "\n\n".join([d.page_content for d in state["context"]["documents"] if hasattr(d, "page_content")])
276
  st.write(f"[DEBUG] Analyzing content from {len(state['context']['documents'])} documents.")
277
  prompt = ResearchConfig.ANALYSIS_TEMPLATE.format(context=docs)
 
322
 
323
  def _quality_check(self, state: AgentState) -> str:
324
  content = state["messages"][-1].content
 
325
  return "valid" if "VALID" in content else "invalid"
326
 
327
  def _error_state(self, message: str) -> Dict:
 
338
  class ResearchInterface:
339
  def __init__(self):
340
  self.workflow = ResearchWorkflow()
341
+ # Do not call st.set_page_config here because it has already been called at the top.
 
 
 
 
 
 
 
342
  self._inject_styles()
343
  self._build_sidebar()
344
  self._build_main_interface()
 
404
  def _build_main_interface(self):
405
  st.title("🧠 NeuroResearch AI")
406
  query = st.text_area("Research Query:", height=200,
407
+ placeholder="Enter technical research question...")
408
 
409
  if st.button("Execute Analysis", type="primary"):
410
  self._execute_analysis(query)
 
415
  results = self.workflow.app.stream(
416
  {"messages": [HumanMessage(content=query)], "context": {}, "metadata": {}}
417
  )
 
418
  for event in results:
419
  self._render_event(event)
 
420
  st.success("✅ Analysis Completed Successfully")
421
  except Exception as e:
422
  st.error(f"""**Analysis Failed**
 
430
  if 'ingest' in event:
431
  with st.container():
432
  st.success("✅ Query Ingested")
 
433
  elif 'retrieve' in event:
434
  with st.container():
435
  docs = event['retrieve']['context']['documents']
 
438
  for i, doc in enumerate(docs, 1):
439
  st.markdown(f"**Document {i}**")
440
  st.code(doc.page_content, language='text')
 
441
  elif 'analyze' in event:
442
  with st.container():
443
  content = event['analyze']['messages'][0].content
444
  with st.expander("Technical Analysis Report", expanded=True):
445
  st.markdown(content)
 
446
  elif 'validate' in event:
447
  with st.container():
448
  content = event['validate']['messages'][0].content