Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
# ------------------------------
|
2 |
-
# Imports & Dependencies
|
3 |
# ------------------------------
|
4 |
from langchain_openai import OpenAIEmbeddings
|
5 |
from langchain_community.vectorstores import Chroma
|
@@ -9,453 +9,339 @@ from langgraph.graph import END, StateGraph
|
|
9 |
from langgraph.prebuilt import ToolNode
|
10 |
from langgraph.graph.message import add_messages
|
11 |
from typing_extensions import TypedDict, Annotated
|
12 |
-
from typing import Sequence
|
13 |
import chromadb
|
14 |
import re
|
15 |
import os
|
16 |
import streamlit as st
|
17 |
import requests
|
|
|
|
|
18 |
from langchain.tools.retriever import create_retriever_tool
|
|
|
19 |
|
20 |
# ------------------------------
|
21 |
-
# Configuration
|
22 |
# ------------------------------
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
"Academic Paper Summary: Why Transformers Became the Mainstream Architecture in Natural Language Processing",
|
50 |
-
"Latest Trends in Machine Learning Methods Using Quantum Computing"
|
51 |
-
]
|
52 |
-
|
53 |
-
development_texts = [
|
54 |
-
"Project A: UI Design Completed, API Integration in Progress",
|
55 |
-
"Project B: Testing New Feature X, Bug Fixes Needed",
|
56 |
-
"Product Y: In the Performance Optimization Stage Before Release"
|
57 |
-
]
|
58 |
-
|
59 |
-
# ------------------------------
|
60 |
-
# Text Splitting & Document Creation
|
61 |
-
# ------------------------------
|
62 |
-
splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=10)
|
63 |
-
research_docs = splitter.create_documents(research_texts)
|
64 |
-
development_docs = splitter.create_documents(development_texts)
|
65 |
|
66 |
# ------------------------------
|
67 |
-
#
|
68 |
# ------------------------------
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
)
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
collection_name
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
-
|
89 |
-
|
|
|
|
|
90 |
|
91 |
# ------------------------------
|
92 |
-
#
|
93 |
# ------------------------------
|
94 |
-
|
95 |
-
|
96 |
-
"
|
97 |
-
"Search information from the research database."
|
98 |
)
|
99 |
|
100 |
-
|
101 |
-
|
102 |
-
"
|
103 |
-
"Search information from the development database."
|
104 |
)
|
105 |
|
106 |
-
tools = [research_tool, development_tool]
|
107 |
-
|
108 |
# ------------------------------
|
109 |
-
#
|
110 |
# ------------------------------
|
111 |
-
class
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
"""
|
132 |
-
|
133 |
-
headers = {
|
134 |
-
"Accept": "application/json",
|
135 |
-
"Authorization": f"Bearer {DEEPSEEK_API_KEY}",
|
136 |
-
"Content-Type": "application/json"
|
137 |
-
}
|
138 |
-
|
139 |
-
data = {
|
140 |
-
"model": "deepseek-chat",
|
141 |
-
"messages": [{"role": "user", "content": prompt}],
|
142 |
-
"temperature": 0.7,
|
143 |
-
"max_tokens": 1024
|
144 |
-
}
|
145 |
-
|
146 |
-
try:
|
147 |
-
response = requests.post(
|
148 |
-
"https://api.deepseek.com/v1/chat/completions",
|
149 |
-
headers=headers,
|
150 |
-
json=data,
|
151 |
-
verify=False,
|
152 |
-
timeout=30
|
153 |
-
)
|
154 |
-
response.raise_for_status()
|
155 |
|
156 |
-
|
157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
|
159 |
-
|
160 |
-
query = response_text.split("SEARCH_RESEARCH:")[1].strip()
|
161 |
-
results = research_retriever.invoke(query)
|
162 |
-
return {"messages": [AIMessage(content=f'Action: research_db_tool\n{{"query": "{query}"}}\n\nResults: {str(results)}')]}
|
163 |
-
|
164 |
-
elif "SEARCH_DEV:" in response_text:
|
165 |
-
query = response_text.split("SEARCH_DEV:")[1].strip()
|
166 |
-
results = development_retriever.invoke(query)
|
167 |
-
return {"messages": [AIMessage(content=f'Action: development_db_tool\n{{"query": "{query}"}}\n\nResults: {str(results)}')]}
|
168 |
-
|
169 |
-
else:
|
170 |
-
return {"messages": [AIMessage(content=response_text)]}
|
171 |
-
|
172 |
-
except Exception as e:
|
173 |
-
error_msg = f"API Error: {str(e)}"
|
174 |
-
if "Insufficient Balance" in str(e):
|
175 |
-
error_msg += "\n\nPlease check your DeepSeek API account balance."
|
176 |
-
return {"messages": [AIMessage(content=error_msg)]}
|
177 |
-
|
178 |
-
def simple_grade_documents(state: AgentState):
|
179 |
-
messages = state["messages"]
|
180 |
-
last_message = messages[-1]
|
181 |
-
print("Evaluating message:", last_message.content)
|
182 |
-
|
183 |
-
if "Results: [Document" in last_message.content:
|
184 |
-
print("---DOCS FOUND, GO TO GENERATE---")
|
185 |
-
return "generate"
|
186 |
-
else:
|
187 |
-
print("---NO DOCS FOUND, TRY REWRITE---")
|
188 |
-
return "rewrite"
|
189 |
-
|
190 |
-
def generate(state: AgentState):
|
191 |
-
print("---GENERATE FINAL ANSWER---")
|
192 |
-
messages = state["messages"]
|
193 |
-
question = messages[0].content if isinstance(messages[0], tuple) else messages[0].content
|
194 |
-
last_message = messages[-1]
|
195 |
-
|
196 |
-
docs = ""
|
197 |
-
if "Results: [" in last_message.content:
|
198 |
-
results_start = last_message.content.find("Results: [")
|
199 |
-
docs = last_message.content[results_start:]
|
200 |
-
print("Documents found:", docs)
|
201 |
-
|
202 |
-
headers = {
|
203 |
-
"Accept": "application/json",
|
204 |
-
"Authorization": f"Bearer {DEEPSEEK_API_KEY}",
|
205 |
-
"Content-Type": "application/json"
|
206 |
-
}
|
207 |
-
|
208 |
-
prompt = f"""Based on these research documents, summarize the latest advancements in AI:
|
209 |
-
Question: {question}
|
210 |
-
Documents: {docs}
|
211 |
-
Focus on extracting and synthesizing the key findings from the research papers.
|
212 |
-
"""
|
213 |
-
|
214 |
-
data = {
|
215 |
-
"model": "deepseek-chat",
|
216 |
-
"messages": [{
|
217 |
-
"role": "user",
|
218 |
-
"content": prompt
|
219 |
-
}],
|
220 |
-
"temperature": 0.7,
|
221 |
-
"max_tokens": 1024
|
222 |
-
}
|
223 |
-
|
224 |
-
try:
|
225 |
-
print("Sending generate request to API...")
|
226 |
-
response = requests.post(
|
227 |
-
"https://api.deepseek.com/v1/chat/completions",
|
228 |
-
headers=headers,
|
229 |
-
json=data,
|
230 |
-
verify=False,
|
231 |
-
timeout=30
|
232 |
-
)
|
233 |
-
response.raise_for_status()
|
234 |
-
|
235 |
-
response_text = response.json()['choices'][0]['message']['content']
|
236 |
-
print("Final Answer:", response_text)
|
237 |
-
return {"messages": [AIMessage(content=response_text)]}
|
238 |
-
except Exception as e:
|
239 |
-
error_msg = f"Generation Error: {str(e)}"
|
240 |
-
return {"messages": [AIMessage(content=error_msg)]}
|
241 |
-
|
242 |
-
def rewrite(state: AgentState):
|
243 |
-
print("---REWRITE QUESTION---")
|
244 |
-
messages = state["messages"]
|
245 |
-
original_question = messages[0].content if len(messages) > 0 else "N/A"
|
246 |
-
|
247 |
-
headers = {
|
248 |
-
"Accept": "application/json",
|
249 |
-
"Authorization": f"Bearer {DEEPSEEK_API_KEY}",
|
250 |
-
"Content-Type": "application/json"
|
251 |
-
}
|
252 |
-
|
253 |
-
data = {
|
254 |
-
"model": "deepseek-chat",
|
255 |
-
"messages": [{
|
256 |
-
"role": "user",
|
257 |
-
"content": f"Rewrite this question to be more specific and clearer: {original_question}"
|
258 |
-
}],
|
259 |
-
"temperature": 0.7,
|
260 |
-
"max_tokens": 1024
|
261 |
-
}
|
262 |
-
|
263 |
-
try:
|
264 |
-
print("Sending rewrite request...")
|
265 |
-
response = requests.post(
|
266 |
-
"https://api.deepseek.com/v1/chat/completions",
|
267 |
-
headers=headers,
|
268 |
-
json=data,
|
269 |
-
verify=False,
|
270 |
-
timeout=30
|
271 |
-
)
|
272 |
-
response.raise_for_status()
|
273 |
-
|
274 |
-
response_text = response.json()['choices'][0]['message']['content']
|
275 |
-
print("Rewritten question:", response_text)
|
276 |
-
return {"messages": [AIMessage(content=response_text)]}
|
277 |
-
except Exception as e:
|
278 |
-
error_msg = f"Rewrite Error: {str(e)}"
|
279 |
-
return {"messages": [AIMessage(content=error_msg)]}
|
280 |
-
|
281 |
-
tools_pattern = re.compile(r"Action: .*")
|
282 |
-
|
283 |
-
def custom_tools_condition(state: AgentState):
|
284 |
-
messages = state["messages"]
|
285 |
-
last_message = messages[-1]
|
286 |
-
content = last_message.content
|
287 |
-
|
288 |
-
print("Checking tools condition:", content)
|
289 |
-
if tools_pattern.match(content):
|
290 |
-
print("Moving to retrieve...")
|
291 |
-
return "tools"
|
292 |
-
print("Moving to END...")
|
293 |
-
return END
|
294 |
|
295 |
# ------------------------------
|
296 |
-
#
|
297 |
# ------------------------------
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
)
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
# Compile the workflow
|
333 |
-
app = workflow.compile()
|
334 |
|
335 |
# ------------------------------
|
336 |
-
#
|
337 |
# ------------------------------
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
344 |
|
345 |
# ------------------------------
|
346 |
-
#
|
347 |
# ------------------------------
|
348 |
def main():
|
|
|
|
|
349 |
st.set_page_config(
|
350 |
-
page_title="AI Research
|
351 |
layout="wide",
|
352 |
-
initial_sidebar_state="expanded"
|
|
|
|
|
|
|
|
|
|
|
353 |
)
|
354 |
-
|
355 |
-
st.markdown("""
|
356 |
-
<style>
|
357 |
-
.stApp {
|
358 |
-
background-color: #1a1a1a;
|
359 |
-
color: #ffffff;
|
360 |
-
}
|
361 |
|
362 |
-
.
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
.stButton > button:hover {
|
374 |
-
background-color: #45a049;
|
375 |
-
transform: scale(1.02);
|
376 |
-
}
|
377 |
|
378 |
-
.
|
379 |
-
|
380 |
-
border-left: 5px solid #2196F3;
|
381 |
-
}
|
382 |
|
383 |
-
|
384 |
-
|
385 |
-
|
|
|
|
|
|
|
|
|
386 |
|
387 |
-
.st-expander {
|
388 |
-
background-color: #2d2d2d;
|
389 |
-
border: 1px solid #3d3d3d;
|
390 |
-
}
|
391 |
-
</style>
|
392 |
-
""", unsafe_allow_html=True)
|
393 |
-
|
394 |
-
with st.sidebar:
|
395 |
-
st.header("π Available Data")
|
396 |
-
st.subheader("Research Database")
|
397 |
-
for text in research_texts:
|
398 |
-
st.markdown(f'<div class="data-box research-box" style="padding: 15px; margin: 10px 0; border-radius: 5px;">{text}</div>', unsafe_allow_html=True)
|
399 |
-
|
400 |
-
st.subheader("Development Database")
|
401 |
-
for text in development_texts:
|
402 |
-
st.markdown(f'<div class="data-box dev-box" style="padding: 15px; margin: 10px 0; border-radius: 5px;">{text}</div>', unsafe_allow_html=True)
|
403 |
-
|
404 |
-
st.title("π€ AI Research & Development Assistant")
|
405 |
-
st.markdown("---")
|
406 |
-
|
407 |
-
query = st.text_area("Enter your question:", height=100, placeholder="e.g., What is the latest advancement in AI research?")
|
408 |
-
|
409 |
col1, col2 = st.columns([1, 2])
|
410 |
with col1:
|
411 |
-
if st.button("
|
412 |
-
if query:
|
|
|
|
|
|
|
|
|
413 |
try:
|
414 |
-
|
415 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
416 |
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
elif 'generate' in event:
|
429 |
-
content = event['generate']['messages'][0].content
|
430 |
-
if "Error" in content:
|
431 |
-
st.error(content)
|
432 |
-
else:
|
433 |
-
st.markdown("### β¨ Final Answer:")
|
434 |
-
st.success(content)
|
435 |
except Exception as e:
|
|
|
436 |
st.error(f"""
|
437 |
-
**
|
438 |
{str(e)}
|
439 |
-
|
440 |
-
- API key configuration
|
441 |
-
-
|
442 |
-
-
|
443 |
""")
|
444 |
-
|
445 |
-
|
|
|
|
|
446 |
|
447 |
with col2:
|
448 |
st.markdown("""
|
449 |
-
|
450 |
-
1.
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
|
455 |
-
|
456 |
-
-
|
457 |
-
-
|
458 |
-
|
|
|
|
|
|
|
|
|
459 |
""")
|
460 |
|
461 |
if __name__ == "__main__":
|
|
|
1 |
# ------------------------------
|
2 |
+
# Imports & Dependencies (Enhanced)
|
3 |
# ------------------------------
|
4 |
from langchain_openai import OpenAIEmbeddings
|
5 |
from langchain_community.vectorstores import Chroma
|
|
|
9 |
from langgraph.prebuilt import ToolNode
|
10 |
from langgraph.graph.message import add_messages
|
11 |
from typing_extensions import TypedDict, Annotated
|
12 |
+
from typing import Sequence, List, Dict, Any
|
13 |
import chromadb
|
14 |
import re
|
15 |
import os
|
16 |
import streamlit as st
|
17 |
import requests
|
18 |
+
import time
|
19 |
+
import hashlib
|
20 |
from langchain.tools.retriever import create_retriever_tool
|
21 |
+
from datetime import datetime
|
22 |
|
23 |
# ------------------------------
|
24 |
+
# Enhanced Configuration
|
25 |
# ------------------------------
|
26 |
+
class AppConfig:
|
27 |
+
def __init__(self):
|
28 |
+
self.DEEPSEEK_API_KEY = os.environ.get("DEEPSEEK_API_KEY")
|
29 |
+
self.CHROMA_PATH = "chroma_db"
|
30 |
+
self.MAX_RETRIES = 3
|
31 |
+
self.RETRY_DELAY = 1.5
|
32 |
+
self.DOCUMENT_CHUNK_SIZE = 300 # Increased from 100
|
33 |
+
self.DOCUMENT_OVERLAP = 50 # Added overlap for context preservation
|
34 |
+
self.SEARCH_K = 5 # Number of documents to retrieve
|
35 |
+
self.SEARCH_TYPE = "mmr" # Maximal Marginal Relevance
|
36 |
+
|
37 |
+
self.validate_config()
|
38 |
+
|
39 |
+
def validate_config(self):
|
40 |
+
if not self.DEEPSEEK_API_KEY:
|
41 |
+
st.error("""
|
42 |
+
**Critical Configuration Missing**
|
43 |
+
π DeepSeek API key not found in environment variables.
|
44 |
+
Please configure through Hugging Face Space secrets:
|
45 |
+
1. Go to Space Settings β Repository secrets
|
46 |
+
2. Add secret: Name=DEEPSEEK_API_KEY, Value=your_api_key
|
47 |
+
3. Rebuild Space
|
48 |
+
""")
|
49 |
+
st.stop()
|
50 |
+
|
51 |
+
config = AppConfig()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
# ------------------------------
|
54 |
+
# Enhanced ChromaDB Setup
|
55 |
# ------------------------------
|
56 |
+
class ChromaManager:
|
57 |
+
def __init__(self):
|
58 |
+
os.makedirs(config.CHROMA_PATH, exist_ok=True)
|
59 |
+
self.client = chromadb.PersistentClient(path=config.CHROMA_PATH)
|
60 |
+
self.embeddings = OpenAIEmbeddings(
|
61 |
+
model="text-embedding-3-large",
|
62 |
+
# dimensions=1024 # Optional for large-scale deployments
|
63 |
+
)
|
64 |
+
|
65 |
+
def create_collection(self, documents: List[str], collection_name: str) -> Chroma:
|
66 |
+
"""Enhanced document processing with optimized chunking"""
|
67 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
68 |
+
chunk_size=config.DOCUMENT_CHUNK_SIZE,
|
69 |
+
chunk_overlap=config.DOCUMENT_OVERLAP,
|
70 |
+
separators=["\n\n", "\n", "γ", " "]
|
71 |
+
)
|
72 |
+
docs = text_splitter.create_documents(documents)
|
73 |
+
return Chroma.from_documents(
|
74 |
+
documents=docs,
|
75 |
+
embedding=self.embeddings,
|
76 |
+
client=self.client,
|
77 |
+
collection_name=collection_name
|
78 |
+
)
|
79 |
|
80 |
+
# Initialize Chroma with improved parameters
|
81 |
+
chroma_manager = ChromaManager()
|
82 |
+
research_collection = chroma_manager.create_collection(research_texts, "research_collection")
|
83 |
+
dev_collection = chroma_manager.create_collection(development_texts, "development_collection")
|
84 |
|
85 |
# ------------------------------
|
86 |
+
# Enhanced Retriever Configuration
|
87 |
# ------------------------------
|
88 |
+
research_retriever = research_collection.as_retriever(
|
89 |
+
search_type=config.SEARCH_TYPE,
|
90 |
+
search_kwargs={"k": config.SEARCH_K, "fetch_k": config.SEARCH_K * 2}
|
|
|
91 |
)
|
92 |
|
93 |
+
development_retriever = dev_collection.as_retriever(
|
94 |
+
search_type=config.SEARCH_TYPE,
|
95 |
+
search_kwargs={"k": config.SEARCH_K, "fetch_k": config.SEARCH_K * 2}
|
|
|
96 |
)
|
97 |
|
|
|
|
|
98 |
# ------------------------------
|
99 |
+
# Enhanced Document Processing
|
100 |
# ------------------------------
|
101 |
+
class DocumentProcessor:
|
102 |
+
@staticmethod
|
103 |
+
def deduplicate_documents(docs: List[Any]) -> List[Any]:
|
104 |
+
"""Advanced deduplication using content hashing"""
|
105 |
+
seen = set()
|
106 |
+
unique_docs = []
|
107 |
+
for doc in docs:
|
108 |
+
content_hash = hashlib.md5(doc.page_content.encode()).hexdigest()
|
109 |
+
if content_hash not in seen:
|
110 |
+
unique_docs.append(doc)
|
111 |
+
seen.add(content_hash)
|
112 |
+
return unique_docs
|
113 |
+
|
114 |
+
@staticmethod
|
115 |
+
def extract_key_points(docs: List[Any]) -> str:
|
116 |
+
"""Semantic analysis of retrieved documents"""
|
117 |
+
key_points = []
|
118 |
+
categories = {
|
119 |
+
"quantum": ["quantum", "qpu", "qubit"],
|
120 |
+
"vision": ["image", "recognition", "vision"],
|
121 |
+
"nlp": ["transformer", "language", "llm"]
|
122 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
+
for doc in docs:
|
125 |
+
content = doc.page_content.lower()
|
126 |
+
# Categorization logic
|
127 |
+
if any(kw in content for kw in categories["quantum"]):
|
128 |
+
key_points.append("- Quantum computing integration showing promising results")
|
129 |
+
if any(kw in content for kw in categories["vision"]):
|
130 |
+
key_points.append("- Computer vision models achieving state-of-the-art accuracy")
|
131 |
+
if any(kw in content for kw in categories["nlp"]):
|
132 |
+
key_points.append("- NLP architectures evolving with memory-augmented transformers")
|
133 |
|
134 |
+
return "\n".join(list(set(key_points))) # Remove duplicates
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
# ------------------------------
|
137 |
+
# Enhanced Agent Workflow (Additions)
|
138 |
# ------------------------------
|
139 |
+
class EnhancedAgent:
|
140 |
+
def __init__(self):
|
141 |
+
self.session_stats = {
|
142 |
+
"processing_times": [],
|
143 |
+
"doc_counts": [],
|
144 |
+
"error_count": 0
|
145 |
+
}
|
146 |
+
|
147 |
+
def api_request_with_retry(self, endpoint: str, payload: Dict) -> Dict:
|
148 |
+
"""Robust API handling with exponential backoff"""
|
149 |
+
headers = {
|
150 |
+
"Authorization": f"Bearer {config.DEEPSEEK_API_KEY}",
|
151 |
+
"Content-Type": "application/json"
|
152 |
+
}
|
153 |
+
|
154 |
+
for attempt in range(config.MAX_RETRIES):
|
155 |
+
try:
|
156 |
+
response = requests.post(
|
157 |
+
endpoint,
|
158 |
+
headers=headers,
|
159 |
+
json=payload,
|
160 |
+
timeout=30,
|
161 |
+
verify=False
|
162 |
+
)
|
163 |
+
response.raise_for_status()
|
164 |
+
return response.json()
|
165 |
+
except requests.exceptions.HTTPError as e:
|
166 |
+
if e.response.status_code == 429:
|
167 |
+
delay = config.RETRY_DELAY ** (attempt + 1)
|
168 |
+
time.sleep(delay)
|
169 |
+
continue
|
170 |
+
raise
|
171 |
+
raise Exception(f"API request failed after {config.MAX_RETRIES} attempts")
|
|
|
|
|
|
|
172 |
|
173 |
# ------------------------------
|
174 |
+
# Enhanced Streamlit UI (Dark Professional Theme)
|
175 |
# ------------------------------
|
176 |
+
class UITheme:
|
177 |
+
primary_color = "#2E86C1"
|
178 |
+
secondary_color = "#28B463"
|
179 |
+
background_color = "#1A1A1A"
|
180 |
+
text_color = "#EAECEE"
|
181 |
+
|
182 |
+
@classmethod
|
183 |
+
def apply(cls):
|
184 |
+
st.markdown(f"""
|
185 |
+
<style>
|
186 |
+
.stApp {{
|
187 |
+
background-color: {cls.background_color};
|
188 |
+
color: {cls.text_color};
|
189 |
+
}}
|
190 |
+
.stTextArea textarea {{
|
191 |
+
background-color: #2D2D2D !important;
|
192 |
+
color: {cls.text_color} !important;
|
193 |
+
border: 1px solid {cls.primary_color};
|
194 |
+
}}
|
195 |
+
.stButton > button {{
|
196 |
+
background-color: {cls.primary_color};
|
197 |
+
color: white;
|
198 |
+
border: none;
|
199 |
+
padding: 12px 28px;
|
200 |
+
border-radius: 6px;
|
201 |
+
transition: all 0.3s ease;
|
202 |
+
font-weight: 500;
|
203 |
+
}}
|
204 |
+
.stButton > button:hover {{
|
205 |
+
background-color: {cls.secondary_color};
|
206 |
+
transform: translateY(-1px);
|
207 |
+
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
|
208 |
+
}}
|
209 |
+
.data-box {{
|
210 |
+
background-color: #2D2D2D;
|
211 |
+
border-left: 4px solid {cls.primary_color};
|
212 |
+
padding: 18px;
|
213 |
+
margin: 14px 0;
|
214 |
+
border-radius: 8px;
|
215 |
+
box-shadow: 0 2px 8px rgba(0,0,0,0.15);
|
216 |
+
}}
|
217 |
+
.st-expander {{
|
218 |
+
background-color: #2D2D2D;
|
219 |
+
border: 1px solid #3D3D3D;
|
220 |
+
border-radius: 6px;
|
221 |
+
margin: 12px 0;
|
222 |
+
}}
|
223 |
+
.stAlert {{
|
224 |
+
background-color: #423a2d !important;
|
225 |
+
border: 1px solid #E67E22 !important;
|
226 |
+
}}
|
227 |
+
</style>
|
228 |
+
""", unsafe_allow_html=True)
|
229 |
|
230 |
# ------------------------------
|
231 |
+
# Enhanced Main Application
|
232 |
# ------------------------------
|
233 |
def main():
|
234 |
+
UITheme.apply()
|
235 |
+
|
236 |
st.set_page_config(
|
237 |
+
page_title="AI Research Assistant Pro",
|
238 |
layout="wide",
|
239 |
+
initial_sidebar_state="expanded",
|
240 |
+
menu_items={
|
241 |
+
'Get Help': 'https://example.com/docs',
|
242 |
+
'Report a bug': 'https://example.com/issues',
|
243 |
+
'About': "v2.1 | Enhanced Research Assistant"
|
244 |
+
}
|
245 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
246 |
|
247 |
+
with st.sidebar:
|
248 |
+
st.header("π Knowledge Bases")
|
249 |
+
with st.expander("Research Database", expanded=True):
|
250 |
+
for text in research_texts:
|
251 |
+
st.markdown(f'<div class="data-box research-box">{text}</div>',
|
252 |
+
unsafe_allow_html=True)
|
253 |
+
|
254 |
+
with st.expander("Development Database"):
|
255 |
+
for text in development_texts:
|
256 |
+
st.markdown(f'<div class="data-box dev-box">{text}</div>',
|
257 |
+
unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
258 |
|
259 |
+
st.title("π¬ AI Research Assistant Pro")
|
260 |
+
st.markdown("---")
|
|
|
|
|
261 |
|
262 |
+
# Enhanced query input with examples
|
263 |
+
query = st.text_area(
|
264 |
+
"Research Query Input",
|
265 |
+
height=120,
|
266 |
+
placeholder="Enter your research question...\nExample: What are recent breakthroughs in quantum machine learning?",
|
267 |
+
help="Be specific about domains (e.g., computer vision, NLP) for better results"
|
268 |
+
)
|
269 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
270 |
col1, col2 = st.columns([1, 2])
|
271 |
with col1:
|
272 |
+
if st.button("π Analyze Documents", use_container_width=True):
|
273 |
+
if not query:
|
274 |
+
st.warning("β οΈ Please enter a research question")
|
275 |
+
return
|
276 |
+
|
277 |
+
with st.status("Processing Workflow...", expanded=True) as status:
|
278 |
try:
|
279 |
+
start_time = time.time()
|
280 |
+
|
281 |
+
# Document Retrieval Phase
|
282 |
+
status.update(label="π Retrieving Relevant Documents", state="running")
|
283 |
+
events = process_question(query, app, {"configurable": {"thread_id": "1"}})
|
284 |
+
|
285 |
+
# Processing Phase
|
286 |
+
status.update(label="π Analyzing Content", state="running")
|
287 |
+
processed_data = []
|
288 |
+
|
289 |
+
for event in events:
|
290 |
+
if 'agent' in event:
|
291 |
+
content = event['agent']['messages'][0].content
|
292 |
+
if "Results:" in content:
|
293 |
+
docs_str = content.split("Results: ")[1]
|
294 |
+
docs = eval(docs_str)
|
295 |
+
unique_docs = DocumentProcessor.deduplicate_documents(docs)
|
296 |
+
key_points = DocumentProcessor.extract_key_points(unique_docs)
|
297 |
+
processed_data.append(key_points)
|
298 |
+
|
299 |
+
with st.expander("π Retrieved Documents", expanded=False):
|
300 |
+
st.info(f"Found {len(unique_docs)} unique documents")
|
301 |
+
st.write(docs_str)
|
302 |
|
303 |
+
elif 'generate' in event:
|
304 |
+
final_answer = event['generate']['messages'][0].content
|
305 |
+
status.update(label="β
Analysis Complete", state="complete")
|
306 |
+
|
307 |
+
st.markdown("## π Research Summary")
|
308 |
+
st.markdown(final_answer)
|
309 |
+
|
310 |
+
# Performance metrics
|
311 |
+
proc_time = time.time() - start_time
|
312 |
+
st.caption(f"β±οΈ Processed in {proc_time:.2f}s | {len(processed_data)} document clusters")
|
313 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
314 |
except Exception as e:
|
315 |
+
status.update(label="β Processing Failed", state="error")
|
316 |
st.error(f"""
|
317 |
+
**Critical Error**
|
318 |
{str(e)}
|
319 |
+
Recommended Actions:
|
320 |
+
- Verify API key configuration
|
321 |
+
- Check service status
|
322 |
+
- Simplify query complexity
|
323 |
""")
|
324 |
+
# Log error with timestamp
|
325 |
+
error_log = f"{datetime.now()} | {str(e)}\n"
|
326 |
+
with open("error_log.txt", "a") as f:
|
327 |
+
f.write(error_log)
|
328 |
|
329 |
with col2:
|
330 |
st.markdown("""
|
331 |
+
## π Usage Guide
|
332 |
+
**1. Query Formulation**
|
333 |
+
- Be domain-specific (e.g., "quantum NLP")
|
334 |
+
- Include timeframes (e.g., "2023-2024 advances")
|
335 |
+
|
336 |
+
**2. Results Interpretation**
|
337 |
+
- Expand document sections for sources
|
338 |
+
- Key points highlight technical breakthroughs
|
339 |
+
- Summary shows commercial implications
|
340 |
+
|
341 |
+
**3. Advanced Features**
|
342 |
+
- `CTRL+Enter` for quick reruns
|
343 |
+
- Click documents for raw context
|
344 |
+
- Export results via screenshot
|
345 |
""")
|
346 |
|
347 |
if __name__ == "__main__":
|