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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# interface.py
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import logging
|
| 5 |
+
from typing import Dict
|
| 6 |
+
|
| 7 |
+
from langchain_core.messages import HumanMessage
|
| 8 |
+
from workflow import ResearchWorkflow
|
| 9 |
+
from config import ResearchConfig
|
| 10 |
+
from langchain_core.messages import AIMessage
|
| 11 |
+
|
| 12 |
+
logger = logging.getLogger(__name__)
|
| 13 |
+
|
| 14 |
+
class ResearchInterface:
|
| 15 |
+
"""
|
| 16 |
+
Provides the Streamlit-based interface for executing the research workflow.
|
| 17 |
+
"""
|
| 18 |
+
def __init__(self) -> None:
|
| 19 |
+
self.workflow = ResearchWorkflow()
|
| 20 |
+
self._initialize_interface()
|
| 21 |
+
|
| 22 |
+
def _initialize_interface(self) -> None:
|
| 23 |
+
st.set_page_config(
|
| 24 |
+
page_title="NeuroResearch AI",
|
| 25 |
+
layout="wide",
|
| 26 |
+
initial_sidebar_state="expanded"
|
| 27 |
+
)
|
| 28 |
+
self._inject_styles()
|
| 29 |
+
self._build_sidebar()
|
| 30 |
+
self._build_main_interface()
|
| 31 |
+
|
| 32 |
+
def _inject_styles(self) -> None:
|
| 33 |
+
st.markdown(
|
| 34 |
+
"""
|
| 35 |
+
<style>
|
| 36 |
+
:root {
|
| 37 |
+
--primary: #2ecc71;
|
| 38 |
+
--secondary: #3498db;
|
| 39 |
+
--background: #0a0a0a;
|
| 40 |
+
--text: #ecf0f1;
|
| 41 |
+
}
|
| 42 |
+
.stApp {
|
| 43 |
+
background: var(--background);
|
| 44 |
+
color: var(--text);
|
| 45 |
+
font-family: 'Roboto', sans-serif;
|
| 46 |
+
}
|
| 47 |
+
.stTextArea textarea {
|
| 48 |
+
background: #1a1a1a !important;
|
| 49 |
+
color: var(--text) !important;
|
| 50 |
+
border: 2px solid var(--secondary);
|
| 51 |
+
border-radius: 8px;
|
| 52 |
+
padding: 1rem;
|
| 53 |
+
}
|
| 54 |
+
.stButton>button {
|
| 55 |
+
background: linear-gradient(135deg, var(--primary), var(--secondary));
|
| 56 |
+
border: none;
|
| 57 |
+
border-radius: 8px;
|
| 58 |
+
padding: 1rem 2rem;
|
| 59 |
+
transition: all 0.3s;
|
| 60 |
+
}
|
| 61 |
+
.stButton>button:hover {
|
| 62 |
+
transform: translateY(-2px);
|
| 63 |
+
box-shadow: 0 4px 12px rgba(46, 204, 113, 0.3);
|
| 64 |
+
}
|
| 65 |
+
.stExpander {
|
| 66 |
+
background: #1a1a1a;
|
| 67 |
+
border: 1px solid #2a2a2a;
|
| 68 |
+
border-radius: 8px;
|
| 69 |
+
margin: 1rem 0;
|
| 70 |
+
}
|
| 71 |
+
</style>
|
| 72 |
+
""",
|
| 73 |
+
unsafe_allow_html=True
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
def _build_sidebar(self) -> None:
|
| 77 |
+
with st.sidebar:
|
| 78 |
+
st.title("π Research Database")
|
| 79 |
+
st.subheader("Technical Papers")
|
| 80 |
+
for title, short in ResearchConfig.DOCUMENT_MAP.items():
|
| 81 |
+
with st.expander(short):
|
| 82 |
+
st.markdown(f"```\n{title}\n```")
|
| 83 |
+
st.subheader("Analysis Metrics")
|
| 84 |
+
st.metric("Vector Collections", 2)
|
| 85 |
+
st.metric("Embedding Dimensions", ResearchConfig.EMBEDDING_DIMENSIONS)
|
| 86 |
+
with st.sidebar.expander("Collaboration Hub"):
|
| 87 |
+
st.subheader("Live Research Team")
|
| 88 |
+
st.write("π©π» Researcher A")
|
| 89 |
+
st.write("π¨π¬ Researcher B")
|
| 90 |
+
st.write("π€ AI Assistant")
|
| 91 |
+
st.subheader("Knowledge Graph")
|
| 92 |
+
if st.button("πΈ View Current Graph"):
|
| 93 |
+
self._display_knowledge_graph()
|
| 94 |
+
|
| 95 |
+
def _build_main_interface(self) -> None:
|
| 96 |
+
st.title("π§ NeuroResearch AI")
|
| 97 |
+
query = st.text_area("Research Query:", height=200, placeholder="Enter technical research question...")
|
| 98 |
+
domain = st.selectbox(
|
| 99 |
+
"Select Research Domain:",
|
| 100 |
+
options=[
|
| 101 |
+
"Biomedical Research",
|
| 102 |
+
"Legal Research",
|
| 103 |
+
"Environmental and Energy Studies",
|
| 104 |
+
"Competitive Programming and Theoretical Computer Science",
|
| 105 |
+
"Social Sciences"
|
| 106 |
+
],
|
| 107 |
+
index=0
|
| 108 |
+
)
|
| 109 |
+
if st.button("Execute Analysis", type="primary"):
|
| 110 |
+
self._execute_analysis(query, domain)
|
| 111 |
+
|
| 112 |
+
def _execute_analysis(self, query: str, domain: str) -> None:
|
| 113 |
+
try:
|
| 114 |
+
with st.spinner("Initializing Quantum Analysis..."):
|
| 115 |
+
results = self.workflow.app.stream(
|
| 116 |
+
{
|
| 117 |
+
"messages": [HumanMessage(content=query)],
|
| 118 |
+
"context": {"domain": domain},
|
| 119 |
+
"metadata": {}
|
| 120 |
+
},
|
| 121 |
+
{"recursion_limit": 100}
|
| 122 |
+
)
|
| 123 |
+
for event in results:
|
| 124 |
+
self._render_event(event)
|
| 125 |
+
st.success("β
Analysis Completed Successfully")
|
| 126 |
+
except Exception as e:
|
| 127 |
+
st.error(
|
| 128 |
+
f"""**Analysis Failed**
|
| 129 |
+
{str(e)}
|
| 130 |
+
Potential issues:
|
| 131 |
+
- Complex query structure
|
| 132 |
+
- Document correlation failure
|
| 133 |
+
- Temporal processing constraints"""
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
def _render_event(self, event: Dict) -> None:
|
| 137 |
+
if 'ingest' in event:
|
| 138 |
+
with st.container():
|
| 139 |
+
st.success("β
Query Ingested")
|
| 140 |
+
elif 'retrieve' in event:
|
| 141 |
+
with st.container():
|
| 142 |
+
docs = event['retrieve']['context'].get('documents', [])
|
| 143 |
+
st.info(f"π Retrieved {len(docs)} documents")
|
| 144 |
+
with st.expander("View Retrieved Documents", expanded=False):
|
| 145 |
+
for idx, doc in enumerate(docs, start=1):
|
| 146 |
+
st.markdown(f"**Document {idx}**")
|
| 147 |
+
st.code(doc.page_content, language='text')
|
| 148 |
+
elif 'analyze' in event:
|
| 149 |
+
with st.container():
|
| 150 |
+
content = event['analyze']['messages'][0].content
|
| 151 |
+
with st.expander("Technical Analysis Report", expanded=True):
|
| 152 |
+
st.markdown(content)
|
| 153 |
+
elif 'validate' in event:
|
| 154 |
+
with st.container():
|
| 155 |
+
content = event['validate']['messages'][0].content
|
| 156 |
+
if "VALID" in content:
|
| 157 |
+
st.success("β
Validation Passed")
|
| 158 |
+
with st.expander("View Validated Analysis", expanded=True):
|
| 159 |
+
st.markdown(content.split("Validation:")[0])
|
| 160 |
+
else:
|
| 161 |
+
st.warning("β οΈ Validation Issues Detected")
|
| 162 |
+
with st.expander("View Validation Details", expanded=True):
|
| 163 |
+
st.markdown(content)
|
| 164 |
+
elif 'enhance' in event:
|
| 165 |
+
with st.container():
|
| 166 |
+
content = event['enhance']['messages'][0].content
|
| 167 |
+
with st.expander("Enhanced Multi-Modal Analysis Report", expanded=True):
|
| 168 |
+
st.markdown(content)
|
| 169 |
+
|
| 170 |
+
def _display_knowledge_graph(self) -> None:
|
| 171 |
+
st.write("Knowledge Graph visualization is not implemented yet.")
|
| 172 |
+
|
| 173 |
+
class ResearchInterfaceExtended(ResearchInterface):
|
| 174 |
+
"""
|
| 175 |
+
Extended interface that includes domain adaptability, collaboration features, and graph visualization.
|
| 176 |
+
"""
|
| 177 |
+
def _build_main_interface(self) -> None:
|
| 178 |
+
super()._build_main_interface()
|