Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
# ------------------------------
|
2 |
-
# UniversalResearch AI System with Refinement Counter and
|
3 |
# ------------------------------
|
4 |
import logging
|
5 |
import os
|
@@ -53,15 +53,14 @@ class AgentState(TypedDict):
|
|
53 |
class ResearchConfig:
|
54 |
"""
|
55 |
Generic configuration for the UniversalResearch AI System.
|
56 |
-
|
57 |
"""
|
58 |
-
DEEPSEEK_API_KEY = os.environ.get("DEEPSEEK_API_KEY")
|
59 |
CHROMA_PATH = "chroma_db"
|
60 |
CHUNK_SIZE = 512
|
61 |
CHUNK_OVERLAP = 64
|
62 |
MAX_CONCURRENT_REQUESTS = 5
|
63 |
EMBEDDING_DIMENSIONS = 1536
|
64 |
-
# An optional map can be used to list pre-loaded or featured research topics.
|
65 |
DOCUMENT_MAP = {
|
66 |
"Sample Research Document 1": "Topic A Overview",
|
67 |
"Sample Research Document 2": "Topic B Analysis",
|
@@ -78,11 +77,12 @@ class ResearchConfig:
|
|
78 |
"Format your response in Markdown with LaTeX mathematical notation where applicable."
|
79 |
)
|
80 |
|
|
|
81 |
if not ResearchConfig.DEEPSEEK_API_KEY:
|
82 |
st.error(
|
83 |
"""**Research Portal Configuration Required**
|
84 |
1. Obtain your DeepSeek API key from [platform.deepseek.com](https://platform.deepseek.com/)
|
85 |
-
2. Set the secret: `DEEPSEEK_API_KEY` in your
|
86 |
3. Rebuild your deployment."""
|
87 |
)
|
88 |
st.stop()
|
@@ -92,8 +92,8 @@ if not ResearchConfig.DEEPSEEK_API_KEY:
|
|
92 |
# ------------------------------
|
93 |
class UniversalDocumentManager:
|
94 |
"""
|
95 |
-
Manages
|
96 |
-
Documents are split into
|
97 |
"""
|
98 |
def __init__(self) -> None:
|
99 |
try:
|
@@ -140,7 +140,6 @@ class UniversalDocumentManager:
|
|
140 |
|
141 |
# Initialize document collections for multiple research domains
|
142 |
udm = UniversalDocumentManager()
|
143 |
-
# Example collections β these can be updated with any research domain documents.
|
144 |
research_docs = udm.create_collection([
|
145 |
"Research Report: Novel AI Techniques in Renewable Energy",
|
146 |
"Academic Paper: Advances in Quantum Computing for Data Analysis",
|
@@ -326,7 +325,11 @@ class ResearchWorkflow:
|
|
326 |
logger.info(f"Retrieved {len(docs)} documents for query.")
|
327 |
return {
|
328 |
"messages": [AIMessage(content=f"Retrieved {len(docs)} documents")],
|
329 |
-
"context": {
|
|
|
|
|
|
|
|
|
330 |
}
|
331 |
except Exception as e:
|
332 |
return self._error_state(f"Retrieval Error: {str(e)}")
|
@@ -344,8 +347,15 @@ class ResearchWorkflow:
|
|
344 |
return self._error_state(response["error"])
|
345 |
logger.info("Content analysis completed.")
|
346 |
return {
|
347 |
-
"messages": [
|
348 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
349 |
}
|
350 |
except Exception as e:
|
351 |
return self._error_state(f"Analysis Error: {str(e)}")
|
@@ -363,7 +373,11 @@ class ResearchWorkflow:
|
|
363 |
response = self.processor.process_query(validation_prompt)
|
364 |
logger.info("Output validation completed.")
|
365 |
return {
|
366 |
-
"messages": [
|
|
|
|
|
|
|
|
|
367 |
}
|
368 |
|
369 |
def refine_results(self, state: AgentState) -> Dict:
|
@@ -381,7 +395,11 @@ class ResearchWorkflow:
|
|
381 |
response = self.processor.process_query(refinement_prompt)
|
382 |
logger.info("Refinement completed.")
|
383 |
return {
|
384 |
-
"messages": [
|
|
|
|
|
|
|
|
|
385 |
"context": state["context"]
|
386 |
}
|
387 |
|
@@ -480,7 +498,6 @@ class ResearchInterface:
|
|
480 |
with st.sidebar:
|
481 |
st.title("π Research Database")
|
482 |
st.subheader("Featured Research Topics")
|
483 |
-
# Display featured research topics from the DOCUMENT_MAP.
|
484 |
for title, short in ResearchConfig.DOCUMENT_MAP.items():
|
485 |
with st.expander(short):
|
486 |
st.markdown(f"```\n{title}\n```")
|
|
|
1 |
# ------------------------------
|
2 |
+
# UniversalResearch AI System with Refinement Counter and DEEPSEEK_API_KEY
|
3 |
# ------------------------------
|
4 |
import logging
|
5 |
import os
|
|
|
53 |
class ResearchConfig:
|
54 |
"""
|
55 |
Generic configuration for the UniversalResearch AI System.
|
56 |
+
Make sure to set DEEPSEEK_API_KEY in your environment or HF Space secrets.
|
57 |
"""
|
58 |
+
DEEPSEEK_API_KEY = os.environ.get("DEEPSEEK_API_KEY") # Updated to reference DEEPSEEK_API_KEY
|
59 |
CHROMA_PATH = "chroma_db"
|
60 |
CHUNK_SIZE = 512
|
61 |
CHUNK_OVERLAP = 64
|
62 |
MAX_CONCURRENT_REQUESTS = 5
|
63 |
EMBEDDING_DIMENSIONS = 1536
|
|
|
64 |
DOCUMENT_MAP = {
|
65 |
"Sample Research Document 1": "Topic A Overview",
|
66 |
"Sample Research Document 2": "Topic B Analysis",
|
|
|
77 |
"Format your response in Markdown with LaTeX mathematical notation where applicable."
|
78 |
)
|
79 |
|
80 |
+
# Early check for missing API key
|
81 |
if not ResearchConfig.DEEPSEEK_API_KEY:
|
82 |
st.error(
|
83 |
"""**Research Portal Configuration Required**
|
84 |
1. Obtain your DeepSeek API key from [platform.deepseek.com](https://platform.deepseek.com/)
|
85 |
+
2. Set the secret: `DEEPSEEK_API_KEY` in your Space settings
|
86 |
3. Rebuild your deployment."""
|
87 |
)
|
88 |
st.stop()
|
|
|
92 |
# ------------------------------
|
93 |
class UniversalDocumentManager:
|
94 |
"""
|
95 |
+
Manages creation of document collections for any research domain.
|
96 |
+
Documents are split into chunks and embedded using OpenAI embeddings.
|
97 |
"""
|
98 |
def __init__(self) -> None:
|
99 |
try:
|
|
|
140 |
|
141 |
# Initialize document collections for multiple research domains
|
142 |
udm = UniversalDocumentManager()
|
|
|
143 |
research_docs = udm.create_collection([
|
144 |
"Research Report: Novel AI Techniques in Renewable Energy",
|
145 |
"Academic Paper: Advances in Quantum Computing for Data Analysis",
|
|
|
325 |
logger.info(f"Retrieved {len(docs)} documents for query.")
|
326 |
return {
|
327 |
"messages": [AIMessage(content=f"Retrieved {len(docs)} documents")],
|
328 |
+
"context": {
|
329 |
+
"documents": docs,
|
330 |
+
"retrieval_time": time.time(),
|
331 |
+
"refine_count": state["context"].get("refine_count", 0)
|
332 |
+
}
|
333 |
}
|
334 |
except Exception as e:
|
335 |
return self._error_state(f"Retrieval Error: {str(e)}")
|
|
|
347 |
return self._error_state(response["error"])
|
348 |
logger.info("Content analysis completed.")
|
349 |
return {
|
350 |
+
"messages": [
|
351 |
+
AIMessage(
|
352 |
+
content=response.get('choices', [{}])[0].get('message', {}).get('content', '')
|
353 |
+
)
|
354 |
+
],
|
355 |
+
"context": {
|
356 |
+
"analysis": response,
|
357 |
+
"refine_count": state["context"].get("refine_count", 0)
|
358 |
+
}
|
359 |
}
|
360 |
except Exception as e:
|
361 |
return self._error_state(f"Analysis Error: {str(e)}")
|
|
|
373 |
response = self.processor.process_query(validation_prompt)
|
374 |
logger.info("Output validation completed.")
|
375 |
return {
|
376 |
+
"messages": [
|
377 |
+
AIMessage(
|
378 |
+
content=analysis + f"\n\nValidation: {response.get('choices', [{}])[0].get('message', {}).get('content', '')}"
|
379 |
+
)
|
380 |
+
]
|
381 |
}
|
382 |
|
383 |
def refine_results(self, state: AgentState) -> Dict:
|
|
|
395 |
response = self.processor.process_query(refinement_prompt)
|
396 |
logger.info("Refinement completed.")
|
397 |
return {
|
398 |
+
"messages": [
|
399 |
+
AIMessage(
|
400 |
+
content=response.get('choices', [{}])[0].get('message', {}).get('content', '')
|
401 |
+
)
|
402 |
+
],
|
403 |
"context": state["context"]
|
404 |
}
|
405 |
|
|
|
498 |
with st.sidebar:
|
499 |
st.title("π Research Database")
|
500 |
st.subheader("Featured Research Topics")
|
|
|
501 |
for title, short in ResearchConfig.DOCUMENT_MAP.items():
|
502 |
with st.expander(short):
|
503 |
st.markdown(f"```\n{title}\n```")
|