Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234569 unmodified lines1617181920216 unmodified lines2829303132333431 unmodified lines666768697071724 unmodified lines777879808182831 unmodified line85868788899095 unmodified lines18618718818919019119218 unmodified lines2112122132142152162172182191 unmodified line22122222322422522622740 unmodified lines2682692702712722732742752762772782792802812822832842852862872882892902912922934 unmodified lines29829930030130230330430530630730830931031131231311 unmodified lines32532632732832933033120 unmodified lines35235335435535635735836 unmodified lines3953963973983994004014024034044054064074084094104114124134144154167 unmodified lines42442542642742842943035 unmodified lines46646746846947047147247347447547663 unmodified lines5405415425435445452 unmodified lines54854955055155255355411 unmodified lines56656756856957057157241 unmodified lines614615616617618619# ------------------------------# Enhanced NeuroResearch AI System with Domain Adaptability,# Refinement Counter, Dynamic Difficulty Gradient, and Meta-Refinement Inspired by LADDER# ------------------------------import loggingimport os9 unmodified linesimport chromadbimport requestsimport streamlit as st# LangChain and LangGraph importsfrom langchain_openai import OpenAIEmbeddings6 unmodified linesfrom typing_extensions import TypedDict, Annotatedfrom langchain.tools.retriever import create_retriever_tool# Increase Python's recursion limit at the very start (if needed)sys.setrecursionlimit(1000)# ------------------------------31 unmodified lines"Latest Trends in Machine Learning Methods Using Quantum Computing":"Quantum ML Frontiers"}# Base analysis template remains unchanged.ANALYSIS_TEMPLATE = ("Analyze these technical documents with scientific rigor:\n{context}\n\n""Respond with:\n"4 unmodified lines"5. Limitations & Future Directions\n\n""Format: Markdown with LaTeX mathematical notation where applicable")# Domain-specific prompt additions for customizable analysisDOMAIN_PROMPTS = {"Biomedical Research": "Consider clinical trial design, patient outcomes, and recent biomedical breakthroughs.","Legal Research": "Emphasize legal precedents, case law, and nuanced statutory interpretations.",1 unmodified line"Competitive Programming and Theoretical Computer Science": "Focus on algorithmic complexity, innovative proofs, and computational techniques.","Social Sciences": "Concentrate on economic trends, sociological data, and correlations impacting public policy."}if not ResearchConfig.DEEPSEEK_API_KEY:st.error(95 unmodified linesFor now, domain differentiation is minimal; however, you can extend this method to use domain-specific collections."""try:# For demonstration, we use the "research" collection for all domains.return self.research_retriever.invoke(query)except Exception as e:logger.exception(f"Retrieval error for domain '{domain}'.")18 unmodified linesProcesses a query by sending multiple API requests in parallel."""futures = []for _ in range(3): # Triple redundancy for reliabilityfutures.append(self.executor.submit(self._execute_api_request, prompt))results = []for future in as_completed(futures):try:1 unmodified lineexcept Exception as e:logger.exception("Error during API request execution.")st.error(f"Processing Error: {str(e)}")return self._consensus_check(results)def _execute_api_request(self, prompt: str) -> Dict:40 unmodified linesreturn max(valid_results, key=lambda x: len(x.get('choices', [{}])[0].get('message', {}).get('content', '')))# ------------------------------# Research Workflow Engine# ------------------------------class ResearchWorkflow:"""Defines the multi-step research workflow using a state graph."""def __init__(self) -> None:self.processor = CognitiveProcessor()self.workflow = StateGraph(AgentState)self._build_workflow()self.app = self.workflow.compile()def _build_workflow(self) -> None:# Define nodesself.workflow.add_node("ingest", self.ingest_query)self.workflow.add_node("retrieve", self.retrieve_documents)self.workflow.add_node("analyze", self.analyze_content)self.workflow.add_node("validate", self.validate_output)self.workflow.add_node("refine", self.refine_results)# Set entry point and edgesself.workflow.set_entry_point("ingest")self.workflow.add_edge("ingest", "retrieve")self.workflow.add_edge("retrieve", "analyze")4 unmodified lines)self.workflow.add_edge("validate", END)self.workflow.add_edge("refine", "retrieve")def ingest_query(self, state: AgentState) -> Dict:"""Ingests the research query and initializes the refinement counter, refinement history, and domain."""try:query = state["messages"][-1].content# Retrieve domain from the query context if available; otherwise, default to "Biomedical Research"domain = state.get("domain", "Biomedical Research")# Initialize context with raw query, selected domain, refinement counter, and empty refinement historynew_context = {"raw_query": query, "domain": domain, "refine_count": 0, "refinement_history": []}logger.info(f"Query ingested. Domain: {domain}")return {11 unmodified lines"""try:query = state["context"]["raw_query"]# For demonstration, we use the "research" collection for all domains.docs = retriever.retrieve(query, state["context"].get("domain", "Biomedical Research"))logger.info(f"Retrieved {len(docs)} documents for query.")return {20 unmodified linesdocs_text = "\n\n".join([d.page_content for d in docs])domain = state["context"].get("domain", "Biomedical Research")domain_prompt = ResearchConfig.DOMAIN_PROMPTS.get(domain, "")# Combine domain-specific instructions with the base analysis templatefull_prompt = f"{domain_prompt}\n\n" + ResearchConfig.ANALYSIS_TEMPLATE.format(context=docs_text)response = self.processor.process_query(full_prompt)if "error" in response:36 unmodified linesdef refine_results(self, state: AgentState) -> Dict:"""Refines the analysis report if validation fails.Implements an innovative meta-refinement mechanism inspired by LADDER.Tracks refinement history, uses a dynamic difficulty gradient, and if the refinement count exceeds a threshold,summarizes the history into a final output."""try:current_count = state["context"].get("refine_count", 0)state["context"]["refine_count"] = current_count + 1# Append current analysis to refinement historyrefinement_history = state["context"].setdefault("refinement_history", [])current_analysis = state["messages"][-1].contentrefinement_history.append(current_analysis)# Compute a "difficulty level" (from 3 to 0) based on refinement countdifficulty_level = max(0, 3 - state["context"]["refine_count"])logger.info(f"Refinement iteration: {state['context']['refine_count']}, Difficulty level: {difficulty_level}")# If refinement count exceeds threshold, perform meta-refinement by summarizing the historyif state["context"]["refine_count"] >= 3:meta_prompt = ("You are given the following series of refinement outputs:\n" +7 unmodified lines"context": state["context"]}else:# Standard refinement with a dynamic difficulty promptrefinement_prompt = (f"Refine this analysis (current difficulty level: {difficulty_level}):\n{current_analysis}\n\n""Improve the following aspects:\n1. Technical precision\n2. Empirical grounding\n3. Theoretical coherence\n\n"35 unmodified lines}# ------------------------------# Research Interface (Streamlit UI)# ------------------------------class ResearchInterface:"""Provides the Streamlit-based interface for executing the research workflow."""def __init__(self) -> None:self.workflow = ResearchWorkflow()63 unmodified linesst.subheader("Analysis Metrics")st.metric("Vector Collections", 2)st.metric("Embedding Dimensions", ResearchConfig.EMBEDDING_DIMENSIONS)def _build_main_interface(self) -> None:st.title("🧠 NeuroResearch AI")2 unmodified linesheight=200,placeholder="Enter technical research question...")# Domain selection for research use casesdomain = st.selectbox("Select Research Domain:",options=[11 unmodified linesdef _execute_analysis(self, query: str, domain: str) -> None:try:with st.spinner("Initializing Quantum Analysis..."):# Pass domain into the context by adding it to the initial stateresults = self.workflow.app.stream({"messages": [HumanMessage(content=query)],"context": {"domain": domain},41 unmodified linesst.warning("⚠️ Validation Issues Detected")with st.expander("View Validation Details", expanded=True):st.markdown(content)if __name__ == "__main__":ResearchInterface()12345679 unmodified lines17181920212223246 unmodified lines3132333435363731 unmodified lines6970717273744 unmodified lines7980818283841 unmodified line868788899091929394959697989995 unmodified lines19519619719819920018 unmodified lines2192202212222232242252261 unmodified line22822923023123223340 unmodified lines2742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344 unmodified lines43944044144244344444544644744844945045145245345445545611 unmodified lines46846947047147247320 unmodified lines49449549649749849936 unmodified lines5365375385395405415425435445455465475485495505515525535547 unmodified lines56256356456556656735 unmodified lines60360460560660760860961061161261361463 unmodified lines6786796806816826836846856866876886896906912 unmodified lines69469569669769869911 unmodified lines71171271371471571641 unmodified lines758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813# ------------------------------# Enhanced NeuroResearch AI System with Domain Adaptability,# Refinement Counter, Dynamic Difficulty Gradient, Meta-Refinement Inspired by LADDER,# Quantum Knowledge Graph & Multi-Modal Enhancements# ------------------------------import loggingimport os9 unmodified linesimport chromadbimport requestsimport streamlit as stfrom PIL import Imageimport torch# LangChain and LangGraph importsfrom langchain_openai import OpenAIEmbeddings6 unmodified linesfrom typing_extensions import TypedDict, Annotatedfrom langchain.tools.retriever import create_retriever_tool# Increase Python's recursion limit (if needed)sys.setrecursionlimit(1000)# ------------------------------31 unmodified lines"Latest Trends in Machine Learning Methods Using Quantum Computing":"Quantum ML Frontiers"}ANALYSIS_TEMPLATE = ("Analyze these technical documents with scientific rigor:\n{context}\n\n""Respond with:\n"4 unmodified lines"5. Limitations & Future Directions\n\n""Format: Markdown with LaTeX mathematical notation where applicable")DOMAIN_PROMPTS = {"Biomedical Research": "Consider clinical trial design, patient outcomes, and recent biomedical breakthroughs.","Legal Research": "Emphasize legal precedents, case law, and nuanced statutory interpretations.",1 unmodified line"Competitive Programming and Theoretical Computer Science": "Focus on algorithmic complexity, innovative proofs, and computational techniques.","Social Sciences": "Concentrate on economic trends, sociological data, and correlations impacting public policy."}ENSEMBLE_MODELS = {"deepseek-chat": {"max_tokens": 2000, "temp": 0.7},"deepseek-coder": {"max_tokens": 2500, "temp": 0.5}}CLIP_SETTINGS = {"model": "openai/clip-vit-large-patch14","image_db": "image_vectors"}if not ResearchConfig.DEEPSEEK_API_KEY:st.error(95 unmodified linesFor now, domain differentiation is minimal; however, you can extend this method to use domain-specific collections."""try:return self.research_retriever.invoke(query)except Exception as e:logger.exception(f"Retrieval error for domain '{domain}'.")18 unmodified linesProcesses a query by sending multiple API requests in parallel."""futures = []for _ in range(3):futures.append(self.executor.submit(self._execute_api_request, prompt))results = []for future in as_completed(futures):try:1 unmodified lineexcept Exception as e:logger.exception("Error during API request execution.")st.error(f"Processing Error: {str(e)}")return self._consensus_check(results)def _execute_api_request(self, prompt: str) -> Dict:40 unmodified linesreturn max(valid_results, key=lambda x: len(x.get('choices', [{}])[0].get('message', {}).get('content', '')))# ------------------------------# Enhanced Cognitive Processor with Ensemble & Knowledge Graph Integration# ------------------------------class EnhancedCognitiveProcessor(CognitiveProcessor):"""Extended with ensemble processing and knowledge graph integration."""def __init__(self) -> None:super().__init__()self.knowledge_graph = QuantumKnowledgeGraph()self.ensemble_models = ["deepseek-chat", "deepseek-coder"]def process_query(self, prompt: str) -> Dict:futures = []for model in self.ensemble_models:futures.append(self.executor.submit(self._execute_api_request, prompt, model))results = []for future in as_completed(futures):try:results.append(future.result())except Exception as e:logger.error(f"Model processing error: {str(e)}")best_response = self._consensus_check(results)self._update_knowledge_graph(best_response)return best_responsedef _execute_api_request(self, prompt: str, model: str) -> Dict:headers = {"Authorization": f"Bearer {ResearchConfig.DEEPSEEK_API_KEY}","Content-Type": "application/json","X-Research-Session": self.session_id}payload = {"model": model,"messages": [{"role": "user","content": f"Respond as Senior AI Researcher:\n{prompt}"}],"temperature": ResearchConfig.ENSEMBLE_MODELS[model]["temp"],"max_tokens": ResearchConfig.ENSEMBLE_MODELS[model]["max_tokens"],"top_p": 0.9}try:response = requests.post("https://api.deepseek.com/v1/chat/completions",headers=headers,json=payload,timeout=45)response.raise_for_status()logger.info(f"API request successful for model {model}.")return response.json()except requests.exceptions.RequestException as e:logger.exception(f"API request failed for model {model}.")return {"error": str(e)}def _update_knowledge_graph(self, response: Dict):content = response.get('choices', [{}])[0].get('message', {}).get('content', '')node_id = self.knowledge_graph.create_node({"content": content}, "analysis")if self.knowledge_graph.node_counter > 1:self.knowledge_graph.create_relation(node_id - 1, node_id, "evolution", strength=0.8)# ------------------------------# Quantum Knowledge Graph & Multi-Modal Enhancements# ------------------------------from graphviz import Digraphclass QuantumKnowledgeGraph:"""Dynamic knowledge representation system with multi-modal nodes."""def __init__(self):self.nodes = {}self.relations = []self.node_counter = 0def create_node(self, content: Dict, node_type: str) -> int:self.node_counter += 1self.nodes[self.node_counter] = {"id": self.node_counter,"content": content,"type": node_type,"connections": []}return self.node_counterdef create_relation(self, source: int, target: int, rel_type: str, strength: float = 1.0):self.relations.append({"source": source,"target": target,"type": rel_type,"strength": strength})self.nodes[source]["connections"].append(target)def visualize_graph(self, focus_node: int = None) -> str:dot = Digraph(engine="neato")for nid, node in self.nodes.items():label = f"{node['type']}\n{self._truncate_content(node['content'])}"dot.node(str(nid), label)for rel in self.relations:dot.edge(str(rel["source"]), str(rel["target"]), label=rel["type"])if focus_node:dot.node(str(focus_node), color="red", style="filled")return dot.sourcedef _truncate_content(self, content: Dict) -> str:return json.dumps(content)[:50] + "..."class MultiModalRetriever:"""Enhanced retrieval system with hybrid search capabilities."""def __init__(self, text_retriever, clip_model, clip_processor):self.text_retriever = text_retrieverself.clip_model = clip_modelself.clip_processor = clip_processorself.code_retriever = create_retriever_tool([]) # BM25 retriever placeholderdef retrieve(self, query: str, domain: str) -> Dict[str, List]:results = {"text": self._retrieve_text(query),"images": self._retrieve_images(query),"code": self._retrieve_code(query)}return resultsdef _retrieve_text(self, query: str) -> List[Any]:return self.text_retriever.invoke(query)def _retrieve_images(self, query: str) -> List[str]:inputs = self.clip_processor(text=query, return_tensors="pt")with torch.no_grad():text_emb = self.clip_model.get_text_features(**inputs)return ["image_result_1.png", "image_result_2.png"]def _retrieve_code(self, query: str) -> List[str]:return self.code_retriever.invoke(query)# ------------------------------# Enhanced Research Workflow# ------------------------------class ResearchWorkflow:"""Defines the multi-step research workflow using a state graph."""def __init__(self) -> None:self.processor = EnhancedCognitiveProcessor()self.workflow = StateGraph(AgentState)self._build_workflow()self.app = self.workflow.compile()def _build_workflow(self) -> None:# Define nodes from base workflowself.workflow.add_node("ingest", self.ingest_query)self.workflow.add_node("retrieve", self.retrieve_documents)self.workflow.add_node("analyze", self.analyze_content)self.workflow.add_node("validate", self.validate_output)self.workflow.add_node("refine", self.refine_results)# Set base edgesself.workflow.set_entry_point("ingest")self.workflow.add_edge("ingest", "retrieve")self.workflow.add_edge("retrieve", "analyze")4 unmodified lines)self.workflow.add_edge("validate", END)self.workflow.add_edge("refine", "retrieve")# Extended node for multi-modal enhancementself.workflow.add_node("enhance", self.enhance_analysis)self.workflow.add_edge("validate", "enhance")self.workflow.add_edge("enhance", END)def ingest_query(self, state: AgentState) -> Dict:"""Ingests the research query and initializes the context with query, domain, refinement counter, and history."""try:query = state["messages"][-1].contentdomain = state.get("domain", "Biomedical Research")new_context = {"raw_query": query, "domain": domain, "refine_count": 0, "refinement_history": []}logger.info(f"Query ingested. Domain: {domain}")return {11 unmodified lines"""try:query = state["context"]["raw_query"]docs = retriever.retrieve(query, state["context"].get("domain", "Biomedical Research"))logger.info(f"Retrieved {len(docs)} documents for query.")return {20 unmodified linesdocs_text = "\n\n".join([d.page_content for d in docs])domain = state["context"].get("domain", "Biomedical Research")domain_prompt = ResearchConfig.DOMAIN_PROMPTS.get(domain, "")full_prompt = f"{domain_prompt}\n\n" + ResearchConfig.ANALYSIS_TEMPLATE.format(context=docs_text)response = self.processor.process_query(full_prompt)if "error" in response:36 unmodified linesdef refine_results(self, state: AgentState) -> Dict:"""Refines the analysis report if validation fails.Implements a meta-refinement mechanism inspired by LADDER.Tracks refinement history, uses a dynamic difficulty gradient, and if the refinement count exceeds a threshold,summarizes the history into a final output."""try:current_count = state["context"].get("refine_count", 0)state["context"]["refine_count"] = current_count + 1refinement_history = state["context"].setdefault("refinement_history", [])current_analysis = state["messages"][-1].contentrefinement_history.append(current_analysis)difficulty_level = max(0, 3 - state["context"]["refine_count"])logger.info(f"Refinement iteration: {state['context']['refine_count']}, Difficulty level: {difficulty_level}")if state["context"]["refine_count"] >= 3:meta_prompt = ("You are given the following series of refinement outputs:\n" +7 unmodified lines"context": state["context"]}else:refinement_prompt = (f"Refine this analysis (current difficulty level: {difficulty_level}):\n{current_analysis}\n\n""Improve the following aspects:\n1. Technical precision\n2. Empirical grounding\n3. Theoretical coherence\n\n"35 unmodified lines}# ------------------------------# Enhanced Research Interface# ------------------------------class ResearchInterface:"""Provides the Streamlit-based interface for executing the research workflow.Extended with collaboration features and knowledge visualization."""def __init__(self) -> None:self.workflow = ResearchWorkflow()63 unmodified linesst.subheader("Analysis Metrics")st.metric("Vector Collections", 2)st.metric("Embedding Dimensions", ResearchConfig.EMBEDDING_DIMENSIONS)with st.sidebar.expander("Collaboration Hub"):st.subheader("Live Research Team")st.write("👩💻 Researcher A")st.write("👨🔬 Researcher B")st.write("🤖 AI Assistant")st.subheader("Knowledge Graph")if st.button("🕸 View Current Graph"):self._display_knowledge_graph()def _build_main_interface(self) -> None:st.title("🧠 NeuroResearch AI")2 unmodified linesheight=200,placeholder="Enter technical research question...")domain = st.selectbox("Select Research Domain:",options=[11 unmodified linesdef _execute_analysis(self, query: str, domain: str) -> None:try:with st.spinner("Initializing Quantum Analysis..."):results = self.workflow.app.stream({"messages": [HumanMessage(content=query)],"context": {"domain": domain},41 unmodified linesst.warning("⚠️ Validation Issues Detected")with st.expander("View Validation Details", expanded=True):st.markdown(content)elif 'enhance' in event:with st.container():content = event['enhance']['messages'][0].contentwith st.expander("Enhanced Multi-Modal Analysis Report", expanded=True):st.markdown(content)def _display_knowledge_graph(self) -> None:graph = self.workflow.processor.knowledge_graph.visualize_graph()st.graphviz_chart(graph)# ------------------------------# Multi-Modal Retriever Initialization# ------------------------------from transformers import CLIPProcessor, CLIPModelclip_model = CLIPModel.from_pretrained(ResearchConfig.CLIP_SETTINGS["model"])clip_processor = CLIPProcessor.from_pretrained(ResearchConfig.CLIP_SETTINGS["model"])multi_retriever = MultiModalRetriever(retriever.research_retriever, clip_model, clip_processor)# ------------------------------# Updated Document Processing for Multi-Modal Documents# ------------------------------class QuantumDocumentManager(QuantumDocumentManager):"""Extended with multi-modal document handling."""def create_image_collection(self, image_paths: List[str]):embeddings = []for img_path in image_paths:image = Image.open(img_path)inputs = clip_processor(images=image, return_tensors="pt")with torch.no_grad():emb = clip_model.get_image_features(**inputs)embeddings.append(emb.numpy())return Chroma.from_embeddings(embeddings=embeddings,documents=image_paths,collection_name="neuro_images")# Initialize image collectionqdm.create_image_collection(["data/images/quantum_computing.png","data/images/neural_arch.png"])# ------------------------------# Execute the Application# ------------------------------class ResearchInterface(ResearchInterface):"""Extended with domain adaptability, collaboration, and graph visualization."""def _build_main_interface(self) -> None:super()._build_main_interface()if __name__ == "__main__":ResearchInterface()
app.py+219-25