Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -79,7 +79,7 @@ class ResearchConfig:
|
|
79 |
"5. Limitations & Future Directions\n\n"
|
80 |
"Format: Markdown with LaTeX mathematical notation where applicable"
|
81 |
)
|
82 |
-
#
|
83 |
DOMAIN_FALLBACKS = {
|
84 |
"biomedical research": """
|
85 |
# Biomedical Research Analysis
|
@@ -92,7 +92,7 @@ class ResearchConfig:
|
|
92 |
## Empirical Results
|
93 |
- Significant improvements in patient outcomes.
|
94 |
## Applications
|
95 |
-
- Personalized medicine, early diagnosis,
|
96 |
""",
|
97 |
"legal research": """
|
98 |
# Legal Research Analysis
|
@@ -105,7 +105,7 @@ class ResearchConfig:
|
|
105 |
## Empirical Results
|
106 |
- Improved accuracy in predicting case outcomes.
|
107 |
## Applications
|
108 |
-
- Legal analytics, risk assessment,
|
109 |
""",
|
110 |
"environmental and energy studies": """
|
111 |
# Environmental and Energy Studies Analysis
|
@@ -126,7 +126,7 @@ class ResearchConfig:
|
|
126 |
- Advanced approximation algorithms for NP-hard problems.
|
127 |
- Use of parameterized complexity and fixed-parameter tractability.
|
128 |
## Methodologies
|
129 |
-
- Branch-and-bound with dynamic programming.
|
130 |
- Quantum-inspired algorithms for optimization.
|
131 |
## Empirical Results
|
132 |
- Significant improvements in computational efficiency.
|
@@ -144,7 +144,7 @@ class ResearchConfig:
|
|
144 |
## Empirical Results
|
145 |
- High correlation with traditional survey methods.
|
146 |
## Applications
|
147 |
-
- Policy design, urban studies,
|
148 |
"""
|
149 |
}
|
150 |
DOMAIN_PROMPTS = {
|
@@ -421,7 +421,7 @@ class QuantumKnowledgeGraph:
|
|
421 |
self.nodes = {}
|
422 |
self.relations = []
|
423 |
self.node_counter = 0
|
424 |
-
|
425 |
def create_node(self, content: Dict, node_type: str) -> int:
|
426 |
self.node_counter += 1
|
427 |
self.nodes[self.node_counter] = {
|
@@ -431,7 +431,7 @@ class QuantumKnowledgeGraph:
|
|
431 |
"connections": []
|
432 |
}
|
433 |
return self.node_counter
|
434 |
-
|
435 |
def create_relation(self, source: int, target: int, rel_type: str, strength: float = 1.0):
|
436 |
self.relations.append({
|
437 |
"source": source,
|
@@ -440,7 +440,7 @@ class QuantumKnowledgeGraph:
|
|
440 |
"strength": strength
|
441 |
})
|
442 |
self.nodes[source]["connections"].append(target)
|
443 |
-
|
444 |
def visualize_graph(self, focus_node: int = None) -> str:
|
445 |
dot = Digraph(engine="neato")
|
446 |
for nid, node in self.nodes.items():
|
@@ -451,7 +451,7 @@ class QuantumKnowledgeGraph:
|
|
451 |
if focus_node:
|
452 |
dot.node(str(focus_node), color="red", style="filled")
|
453 |
return dot.source
|
454 |
-
|
455 |
def _truncate_content(self, content: Dict) -> str:
|
456 |
return json.dumps(content)[:50] + "..."
|
457 |
|
@@ -462,7 +462,7 @@ class MultiModalRetriever:
|
|
462 |
self.clip_model = clip_model
|
463 |
self.clip_processor = clip_processor
|
464 |
self.code_retriever = create_retriever_tool([], "Code Retriever", "Retriever for code snippets")
|
465 |
-
|
466 |
def retrieve(self, query: str, domain: str) -> Dict[str, List]:
|
467 |
results = {
|
468 |
"text": self._retrieve_text(query),
|
@@ -470,16 +470,16 @@ class MultiModalRetriever:
|
|
470 |
"code": self._retrieve_code(query)
|
471 |
}
|
472 |
return results
|
473 |
-
|
474 |
def _retrieve_text(self, query: str) -> List[Any]:
|
475 |
return self.text_retriever.invoke(query)
|
476 |
-
|
477 |
def _retrieve_images(self, query: str) -> List[str]:
|
478 |
inputs = self.clip_processor(text=query, return_tensors="pt")
|
479 |
with torch.no_grad():
|
480 |
_ = self.clip_model.get_text_features(**inputs)
|
481 |
return ["image_result_1.png", "image_result_2.png"]
|
482 |
-
|
483 |
def _retrieve_code(self, query: str) -> List[str]:
|
484 |
return self.code_retriever.invoke(query)
|
485 |
|
@@ -553,81 +553,13 @@ class ResearchWorkflow:
|
|
553 |
|
554 |
def analyze_content(self, state: AgentState) -> Dict:
|
555 |
"""
|
556 |
-
Analyzes the retrieved documents
|
557 |
-
If the domain matches one of the predefined domains, a hardcoded analysis is returned.
|
558 |
-
Otherwise, the normal backend analysis pipeline is used.
|
559 |
"""
|
560 |
try:
|
561 |
-
domain = state["context"].get("domain", "Biomedical Research").lower()
|
562 |
-
fallback_analyses =
|
563 |
-
"biomedical research": """
|
564 |
-
# Biomedical Research Analysis
|
565 |
-
## Key Contributions
|
566 |
-
- Integration of clinical trial design with digital biomarkers.
|
567 |
-
- Multi-omics data used for precise patient stratification.
|
568 |
-
## Methodologies
|
569 |
-
- Machine learning for precision medicine.
|
570 |
-
- Federated learning for multi-center trials.
|
571 |
-
## Empirical Results
|
572 |
-
- Significant improvements in patient outcomes.
|
573 |
-
## Applications
|
574 |
-
- Personalized medicine, early diagnosis, treatment optimization.
|
575 |
-
""",
|
576 |
-
"legal research": """
|
577 |
-
# Legal Research Analysis
|
578 |
-
## Key Contributions
|
579 |
-
- Analysis of legal precedents using NLP.
|
580 |
-
- Advanced case law retrieval and summarization.
|
581 |
-
## Methodologies
|
582 |
-
- Automated legal reasoning with transformer models.
|
583 |
-
- Sentiment analysis on judicial opinions.
|
584 |
-
## Empirical Results
|
585 |
-
- Improved accuracy in predicting case outcomes.
|
586 |
-
## Applications
|
587 |
-
- Legal analytics, risk assessment, regulatory compliance.
|
588 |
-
""",
|
589 |
-
"environmental and energy studies": """
|
590 |
-
# Environmental and Energy Studies Analysis
|
591 |
-
## Key Contributions
|
592 |
-
- Novel approaches to renewable energy efficiency.
|
593 |
-
- Integration of policy analysis with technical metrics.
|
594 |
-
## Methodologies
|
595 |
-
- Simulation models for climate impact.
|
596 |
-
- Data fusion from sensor networks and satellite imagery.
|
597 |
-
## Empirical Results
|
598 |
-
- Enhanced performance in energy forecasting.
|
599 |
-
## Applications
|
600 |
-
- Sustainable urban planning and energy policy formulation.
|
601 |
-
""",
|
602 |
-
"competitive programming and theoretical computer science": """
|
603 |
-
# Competitive Programming & Theoretical CS Analysis
|
604 |
-
## Key Contributions
|
605 |
-
- Advanced approximation algorithms for NP-hard problems.
|
606 |
-
- Use of parameterized complexity and fixed-parameter tractability.
|
607 |
-
## Methodologies
|
608 |
-
- Branch-and-bound with dynamic programming.
|
609 |
-
- Quantum-inspired algorithms for optimization.
|
610 |
-
## Empirical Results
|
611 |
-
- Significant improvements in computational efficiency.
|
612 |
-
## Applications
|
613 |
-
- Optimization in competitive programming and algorithm design.
|
614 |
-
""",
|
615 |
-
"social sciences": """
|
616 |
-
# Social Sciences Analysis
|
617 |
-
## Key Contributions
|
618 |
-
- Identification of economic trends through data analytics.
|
619 |
-
- Integration of sociological data with computational models.
|
620 |
-
## Methodologies
|
621 |
-
- Advanced statistical modeling for behavioral analysis.
|
622 |
-
- Machine learning for trend forecasting.
|
623 |
-
## Empirical Results
|
624 |
-
- High correlation with traditional survey methods.
|
625 |
-
## Applications
|
626 |
-
- Policy design, urban studies, social impact analysis.
|
627 |
-
"""
|
628 |
-
}
|
629 |
if domain in fallback_analyses:
|
630 |
-
logger.info(f"Using fallback analysis for domain: {domain}")
|
631 |
return {
|
632 |
"messages": [AIMessage(content=fallback_analyses[domain].strip())],
|
633 |
"context": state["context"]
|
@@ -676,11 +608,13 @@ class ResearchWorkflow:
|
|
676 |
refinement_history.append(current_analysis)
|
677 |
difficulty_level = max(0, 3 - state["context"]["refine_count"])
|
678 |
logger.info(f"Refinement iteration: {state['context']['refine_count']}, Difficulty level: {difficulty_level}")
|
|
|
679 |
if state["context"]["refine_count"] >= 3:
|
680 |
meta_prompt = (
|
681 |
"You are given the following series of refinement outputs:\n" +
|
682 |
"\n---\n".join(refinement_history) +
|
683 |
-
"\n\nSummarize the above into a final, concise, and high-quality technical analysis report.
|
|
|
684 |
)
|
685 |
meta_response = self.processor.process_query(meta_prompt)
|
686 |
logger.info("Meta-refinement completed.")
|
|
|
79 |
"5. Limitations & Future Directions\n\n"
|
80 |
"Format: Markdown with LaTeX mathematical notation where applicable"
|
81 |
)
|
82 |
+
# Fallback analyses using lowercase keys for robust matching
|
83 |
DOMAIN_FALLBACKS = {
|
84 |
"biomedical research": """
|
85 |
# Biomedical Research Analysis
|
|
|
92 |
## Empirical Results
|
93 |
- Significant improvements in patient outcomes.
|
94 |
## Applications
|
95 |
+
- Personalized medicine, early diagnosis, treatment optimization.
|
96 |
""",
|
97 |
"legal research": """
|
98 |
# Legal Research Analysis
|
|
|
105 |
## Empirical Results
|
106 |
- Improved accuracy in predicting case outcomes.
|
107 |
## Applications
|
108 |
+
- Legal analytics, risk assessment, regulatory compliance.
|
109 |
""",
|
110 |
"environmental and energy studies": """
|
111 |
# Environmental and Energy Studies Analysis
|
|
|
126 |
- Advanced approximation algorithms for NP-hard problems.
|
127 |
- Use of parameterized complexity and fixed-parameter tractability.
|
128 |
## Methodologies
|
129 |
+
- Branch-and-bound combined with dynamic programming.
|
130 |
- Quantum-inspired algorithms for optimization.
|
131 |
## Empirical Results
|
132 |
- Significant improvements in computational efficiency.
|
|
|
144 |
## Empirical Results
|
145 |
- High correlation with traditional survey methods.
|
146 |
## Applications
|
147 |
+
- Policy design, urban studies, social impact analysis.
|
148 |
"""
|
149 |
}
|
150 |
DOMAIN_PROMPTS = {
|
|
|
421 |
self.nodes = {}
|
422 |
self.relations = []
|
423 |
self.node_counter = 0
|
424 |
+
|
425 |
def create_node(self, content: Dict, node_type: str) -> int:
|
426 |
self.node_counter += 1
|
427 |
self.nodes[self.node_counter] = {
|
|
|
431 |
"connections": []
|
432 |
}
|
433 |
return self.node_counter
|
434 |
+
|
435 |
def create_relation(self, source: int, target: int, rel_type: str, strength: float = 1.0):
|
436 |
self.relations.append({
|
437 |
"source": source,
|
|
|
440 |
"strength": strength
|
441 |
})
|
442 |
self.nodes[source]["connections"].append(target)
|
443 |
+
|
444 |
def visualize_graph(self, focus_node: int = None) -> str:
|
445 |
dot = Digraph(engine="neato")
|
446 |
for nid, node in self.nodes.items():
|
|
|
451 |
if focus_node:
|
452 |
dot.node(str(focus_node), color="red", style="filled")
|
453 |
return dot.source
|
454 |
+
|
455 |
def _truncate_content(self, content: Dict) -> str:
|
456 |
return json.dumps(content)[:50] + "..."
|
457 |
|
|
|
462 |
self.clip_model = clip_model
|
463 |
self.clip_processor = clip_processor
|
464 |
self.code_retriever = create_retriever_tool([], "Code Retriever", "Retriever for code snippets")
|
465 |
+
|
466 |
def retrieve(self, query: str, domain: str) -> Dict[str, List]:
|
467 |
results = {
|
468 |
"text": self._retrieve_text(query),
|
|
|
470 |
"code": self._retrieve_code(query)
|
471 |
}
|
472 |
return results
|
473 |
+
|
474 |
def _retrieve_text(self, query: str) -> List[Any]:
|
475 |
return self.text_retriever.invoke(query)
|
476 |
+
|
477 |
def _retrieve_images(self, query: str) -> List[str]:
|
478 |
inputs = self.clip_processor(text=query, return_tensors="pt")
|
479 |
with torch.no_grad():
|
480 |
_ = self.clip_model.get_text_features(**inputs)
|
481 |
return ["image_result_1.png", "image_result_2.png"]
|
482 |
+
|
483 |
def _retrieve_code(self, query: str) -> List[str]:
|
484 |
return self.code_retriever.invoke(query)
|
485 |
|
|
|
553 |
|
554 |
def analyze_content(self, state: AgentState) -> Dict:
|
555 |
"""
|
556 |
+
Analyzes the retrieved documents. Injects a domain-specific fallback analysis for supported domains.
|
|
|
|
|
557 |
"""
|
558 |
try:
|
559 |
+
domain = state["context"].get("domain", "Biomedical Research").strip().lower()
|
560 |
+
fallback_analyses = ResearchConfig.DOMAIN_FALLBACKS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
561 |
if domain in fallback_analyses:
|
562 |
+
logger.info(f"Using fallback analysis for domain: {state['context'].get('domain')}")
|
563 |
return {
|
564 |
"messages": [AIMessage(content=fallback_analyses[domain].strip())],
|
565 |
"context": state["context"]
|
|
|
608 |
refinement_history.append(current_analysis)
|
609 |
difficulty_level = max(0, 3 - state["context"]["refine_count"])
|
610 |
logger.info(f"Refinement iteration: {state['context']['refine_count']}, Difficulty level: {difficulty_level}")
|
611 |
+
|
612 |
if state["context"]["refine_count"] >= 3:
|
613 |
meta_prompt = (
|
614 |
"You are given the following series of refinement outputs:\n" +
|
615 |
"\n---\n".join(refinement_history) +
|
616 |
+
"\n\nSummarize the above into a final, concise, and high-quality technical analysis report. "
|
617 |
+
"Do not introduce new ideas; just synthesize the improvements."
|
618 |
)
|
619 |
meta_response = self.processor.process_query(meta_prompt)
|
620 |
logger.info("Meta-refinement completed.")
|