Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -17,11 +17,27 @@ import streamlit as st
|
|
17 |
import requests
|
18 |
from langchain.tools.retriever import create_retriever_tool
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
# Create directory for Chroma persistence
|
21 |
os.makedirs("chroma_db", exist_ok=True)
|
22 |
|
23 |
# ------------------------------
|
24 |
-
# ChromaDB Client Configuration
|
25 |
# ------------------------------
|
26 |
chroma_client = chromadb.PersistentClient(path="chroma_db")
|
27 |
|
@@ -116,7 +132,7 @@ Otherwise, just answer directly.
|
|
116 |
|
117 |
headers = {
|
118 |
"Accept": "application/json",
|
119 |
-
"Authorization": f"Bearer
|
120 |
"Content-Type": "application/json"
|
121 |
}
|
122 |
|
@@ -127,14 +143,16 @@ Otherwise, just answer directly.
|
|
127 |
"max_tokens": 1024
|
128 |
}
|
129 |
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
|
|
|
|
138 |
response_text = response.json()['choices'][0]['message']['content']
|
139 |
print("Raw response:", response_text)
|
140 |
|
@@ -150,8 +168,12 @@ Otherwise, just answer directly.
|
|
150 |
|
151 |
else:
|
152 |
return {"messages": [AIMessage(content=response_text)]}
|
153 |
-
|
154 |
-
|
|
|
|
|
|
|
|
|
155 |
|
156 |
def simple_grade_documents(state: AgentState):
|
157 |
messages = state["messages"]
|
@@ -179,7 +201,7 @@ def generate(state: AgentState):
|
|
179 |
|
180 |
headers = {
|
181 |
"Accept": "application/json",
|
182 |
-
"Authorization": f"Bearer
|
183 |
"Content-Type": "application/json"
|
184 |
}
|
185 |
|
@@ -199,20 +221,23 @@ Focus on extracting and synthesizing the key findings from the research papers.
|
|
199 |
"max_tokens": 1024
|
200 |
}
|
201 |
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
|
|
|
|
211 |
response_text = response.json()['choices'][0]['message']['content']
|
212 |
print("Final Answer:", response_text)
|
213 |
return {"messages": [AIMessage(content=response_text)]}
|
214 |
-
|
215 |
-
|
|
|
216 |
|
217 |
def rewrite(state: AgentState):
|
218 |
print("---REWRITE QUESTION---")
|
@@ -221,7 +246,7 @@ def rewrite(state: AgentState):
|
|
221 |
|
222 |
headers = {
|
223 |
"Accept": "application/json",
|
224 |
-
"Authorization": f"Bearer
|
225 |
"Content-Type": "application/json"
|
226 |
}
|
227 |
|
@@ -235,23 +260,23 @@ def rewrite(state: AgentState):
|
|
235 |
"max_tokens": 1024
|
236 |
}
|
237 |
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
if response.status_code == 200:
|
250 |
response_text = response.json()['choices'][0]['message']['content']
|
251 |
print("Rewritten question:", response_text)
|
252 |
return {"messages": [AIMessage(content=response_text)]}
|
253 |
-
|
254 |
-
|
|
|
255 |
|
256 |
tools_pattern = re.compile(r"Action: .*")
|
257 |
|
@@ -340,13 +365,8 @@ def main():
|
|
340 |
}
|
341 |
|
342 |
.stButton > button {
|
343 |
-
width: 100%;
|
344 |
-
margin-top: 20px;
|
345 |
background-color: #4CAF50;
|
346 |
color: white;
|
347 |
-
border: none;
|
348 |
-
padding: 10px 24px;
|
349 |
-
border-radius: 4px;
|
350 |
transition: all 0.3s;
|
351 |
}
|
352 |
|
@@ -356,14 +376,7 @@ def main():
|
|
356 |
}
|
357 |
|
358 |
.data-box {
|
359 |
-
padding: 20px;
|
360 |
-
border-radius: 10px;
|
361 |
-
margin: 10px 0;
|
362 |
background-color: #2d2d2d;
|
363 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
|
364 |
-
}
|
365 |
-
|
366 |
-
.research-box {
|
367 |
border-left: 5px solid #2196F3;
|
368 |
}
|
369 |
|
@@ -374,53 +387,19 @@ def main():
|
|
374 |
.st-expander {
|
375 |
background-color: #2d2d2d;
|
376 |
border: 1px solid #3d3d3d;
|
377 |
-
border-radius: 4px;
|
378 |
-
}
|
379 |
-
|
380 |
-
.st-expander label {
|
381 |
-
color: #ffffff !important;
|
382 |
-
}
|
383 |
-
|
384 |
-
.stAlert {
|
385 |
-
background-color: #2d2d2d !important;
|
386 |
-
}
|
387 |
-
|
388 |
-
.st-bb {
|
389 |
-
color: white !important;
|
390 |
-
}
|
391 |
-
|
392 |
-
h1, h2, h3, h4, h5, h6 {
|
393 |
-
color: #ffffff !important;
|
394 |
-
}
|
395 |
-
|
396 |
-
.stMarkdown {
|
397 |
-
color: #e0e0e0;
|
398 |
-
}
|
399 |
-
|
400 |
-
.stWarning {
|
401 |
-
background-color: #423a2d !important;
|
402 |
}
|
403 |
</style>
|
404 |
""", unsafe_allow_html=True)
|
405 |
|
406 |
with st.sidebar:
|
407 |
-
st.markdown("""
|
408 |
-
<style>
|
409 |
-
.sidebar .sidebar-content {
|
410 |
-
background-color: #2d2d2d;
|
411 |
-
}
|
412 |
-
</style>
|
413 |
-
""", unsafe_allow_html=True)
|
414 |
-
|
415 |
st.header("π Available Data")
|
416 |
-
|
417 |
st.subheader("Research Database")
|
418 |
for text in research_texts:
|
419 |
-
st.markdown(f'<div class="data-box research-box">{text}</div>', unsafe_allow_html=True)
|
420 |
|
421 |
st.subheader("Development Database")
|
422 |
for text in development_texts:
|
423 |
-
st.markdown(f'<div class="data-box dev-box">{text}</div>', unsafe_allow_html=True)
|
424 |
|
425 |
st.title("π€ AI Research & Development Assistant")
|
426 |
st.markdown("---")
|
@@ -431,36 +410,53 @@ def main():
|
|
431 |
with col1:
|
432 |
if st.button("π Get Answer", use_container_width=True):
|
433 |
if query:
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
-
|
446 |
-
|
447 |
-
|
448 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
449 |
else:
|
450 |
st.warning("β οΈ Please enter a question first!")
|
451 |
|
452 |
with col2:
|
453 |
st.markdown("""
|
454 |
### π― How to Use
|
455 |
-
1.
|
456 |
-
2. Click
|
457 |
-
3.
|
458 |
-
|
|
|
459 |
### π‘ Example Questions
|
460 |
-
- What
|
461 |
-
-
|
462 |
-
-
|
463 |
-
"""
|
464 |
|
465 |
if __name__ == "__main__":
|
466 |
main()
|
|
|
17 |
import requests
|
18 |
from langchain.tools.retriever import create_retriever_tool
|
19 |
|
20 |
+
# ------------------------------
|
21 |
+
# Configuration
|
22 |
+
# ------------------------------
|
23 |
+
# Get DeepSeek API key from Hugging Face Space secrets
|
24 |
+
DEEPSEEK_API_KEY = os.environ.get("DEEPSEEK_API_KEY")
|
25 |
+
|
26 |
+
if not DEEPSEEK_API_KEY:
|
27 |
+
st.error("""
|
28 |
+
**Missing API Configuration**
|
29 |
+
Please configure your DeepSeek API key in Hugging Face Space secrets:
|
30 |
+
1. Go to your Space's Settings
|
31 |
+
2. Click on 'Repository secrets'
|
32 |
+
3. Add a secret named DEEPSEEK_API_KEY
|
33 |
+
""")
|
34 |
+
st.stop()
|
35 |
+
|
36 |
# Create directory for Chroma persistence
|
37 |
os.makedirs("chroma_db", exist_ok=True)
|
38 |
|
39 |
# ------------------------------
|
40 |
+
# ChromaDB Client Configuration
|
41 |
# ------------------------------
|
42 |
chroma_client = chromadb.PersistentClient(path="chroma_db")
|
43 |
|
|
|
132 |
|
133 |
headers = {
|
134 |
"Accept": "application/json",
|
135 |
+
"Authorization": f"Bearer {DEEPSEEK_API_KEY}",
|
136 |
"Content-Type": "application/json"
|
137 |
}
|
138 |
|
|
|
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 |
response_text = response.json()['choices'][0]['message']['content']
|
157 |
print("Raw response:", response_text)
|
158 |
|
|
|
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"]
|
|
|
201 |
|
202 |
headers = {
|
203 |
"Accept": "application/json",
|
204 |
+
"Authorization": f"Bearer {DEEPSEEK_API_KEY}",
|
205 |
"Content-Type": "application/json"
|
206 |
}
|
207 |
|
|
|
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---")
|
|
|
246 |
|
247 |
headers = {
|
248 |
"Accept": "application/json",
|
249 |
+
"Authorization": f"Bearer {DEEPSEEK_API_KEY}",
|
250 |
"Content-Type": "application/json"
|
251 |
}
|
252 |
|
|
|
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 |
|
|
|
365 |
}
|
366 |
|
367 |
.stButton > button {
|
|
|
|
|
368 |
background-color: #4CAF50;
|
369 |
color: white;
|
|
|
|
|
|
|
370 |
transition: all 0.3s;
|
371 |
}
|
372 |
|
|
|
376 |
}
|
377 |
|
378 |
.data-box {
|
|
|
|
|
|
|
379 |
background-color: #2d2d2d;
|
|
|
|
|
|
|
|
|
380 |
border-left: 5px solid #2196F3;
|
381 |
}
|
382 |
|
|
|
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("---")
|
|
|
410 |
with col1:
|
411 |
if st.button("π Get Answer", use_container_width=True):
|
412 |
if query:
|
413 |
+
try:
|
414 |
+
with st.spinner('Processing your question...'):
|
415 |
+
events = process_question(query, app, {"configurable": {"thread_id": "1"}})
|
416 |
+
|
417 |
+
for event in events:
|
418 |
+
if 'agent' in event:
|
419 |
+
with st.expander("π Processing Step", expanded=True):
|
420 |
+
content = event['agent']['messages'][0].content
|
421 |
+
if "Error" in content:
|
422 |
+
st.error(content)
|
423 |
+
elif "Results:" in content:
|
424 |
+
st.markdown("### π Retrieved Documents:")
|
425 |
+
docs_start = content.find("Results:")
|
426 |
+
docs = content[docs_start:]
|
427 |
+
st.info(docs)
|
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 |
+
**Processing Error**
|
438 |
+
{str(e)}
|
439 |
+
Please check:
|
440 |
+
- API key configuration
|
441 |
+
- Account balance
|
442 |
+
- Network connection
|
443 |
+
""")
|
444 |
else:
|
445 |
st.warning("β οΈ Please enter a question first!")
|
446 |
|
447 |
with col2:
|
448 |
st.markdown("""
|
449 |
### π― How to Use
|
450 |
+
1. Enter your question in the text box
|
451 |
+
2. Click the search button
|
452 |
+
3. Review processing steps
|
453 |
+
4. See final answer
|
454 |
+
|
455 |
### π‘ Example Questions
|
456 |
+
- What's new in AI image recognition?
|
457 |
+
- How is Project B progressing?
|
458 |
+
- Recent machine learning trends?
|
459 |
+
""")
|
460 |
|
461 |
if __name__ == "__main__":
|
462 |
main()
|