File size: 25,927 Bytes
f1592a4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 |
import os
import streamlit as st
import pdfplumber
import requests
import google.generativeai as genai
from bs4 import BeautifulSoup
from langchain.schema import Document
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_pinecone import PineconeVectorStore
from langchain_groq import ChatGroq
from langchain.chains import create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.embeddings import Embeddings
from langchain_community.tools import DuckDuckGoSearchRun
from pinecone import Pinecone
from dotenv import load_dotenv
import numpy as np
import time
import random
from typing import List
import arxiv
import wikipedia
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from lxml import html
import base64
import os
import streamlit as st
import pdfplumber
import requests
import google.generativeai as genai
# Load environment variables
load_dotenv()
# Get API keys from environment variables
groq_key = os.getenv("GROQ_API_KEY")
pinecone_key = os.getenv("PINECONE_API_KEY")
gemini_key = os.getenv("GEMINI_API_KEY") or os.getenv("GOOGLE_API_KEY")
genai.configure(api_key=gemini_key)
# Check if all required API keys are available
if not gemini_key:
st.error("Gemini API key is missing. Please set either GEMINI_API_KEY or GOOGLE_API_KEY environment variable.")
st.set_page_config(
page_title="AI Research Assistant",
page_icon="π",
layout="wide",
initial_sidebar_state="expanded"
)
#-------------------------------------------------------------
# UTILITY FUNCTIONS
#-------------------------------------------------------------
# Gemini Embeddings class
class GeminiEmbeddings(Embeddings):
def __init__(self, api_key):
genai.configure(api_key=api_key)
self.model_name = "models/embedding-001"
def embed_documents(self, texts):
return [self._convert_to_float32(genai.embed_content(
model=self.model_name, content=text, task_type="retrieval_document"
)["embedding"]) for text in texts]
def embed_query(self, text):
response = genai.embed_content(
model=self.model_name, content=text, task_type="retrieval_query"
)
return self._convert_to_float32(response["embedding"])
@staticmethod
def _convert_to_float32(embedding):
return np.array(embedding, dtype=np.float32).tolist()
# PDF handling functions
def extract_text_from_pdf(pdf_path):
text = ""
try:
with pdfplumber.open(pdf_path) as pdf:
for page in pdf.pages:
extracted_text = page.extract_text()
if extracted_text:
text += extracted_text + "\n"
return text.strip()
except Exception as e:
st.error(f"Error extracting text from PDF: {e}")
return ""
def read_data_from_doc(uploaded_file):
docs = []
with pdfplumber.open(uploaded_file) as pdf:
for i, page in enumerate(pdf.pages):
text = page.extract_text() or ""
tables = page.extract_tables()
table_text = "\n".join([
"\n".join(["\t".join(cell if cell is not None else "" for cell in row) for row in table])
for table in tables if table
]) if tables else ""
images = page.images
image_text = f"[{len(images)} image(s) detected]" if images else ""
content = f"{text}\n\n{table_text}\n\n{image_text}".strip()
if content:
docs.append(Document(page_content=content, metadata={"page": i + 1}))
return docs
def make_chunks(docs, chunk_len=1000, chunk_overlap=200):
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=chunk_len, chunk_overlap=chunk_overlap
)
chunks = text_splitter.split_documents(docs)
return [Document(page_content=chunk.page_content, metadata=chunk.metadata) for chunk in chunks]
# Gemini model functions
def get_gemini_model(model_name="gemini-1.5-pro", temperature=0.4):
return genai.GenerativeModel(model_name)
def get_generation_config(temperature=0.4):
return {
"temperature": temperature,
"top_p": 1,
"top_k": 1,
"max_output_tokens": 2048,
}
def get_safety_settings():
return [
{"category": category, "threshold": "BLOCK_NONE"}
for category in [
"HARM_CATEGORY_HARASSMENT",
"HARM_CATEGORY_HATE_SPEECH",
"HARM_CATEGORY_SEXUALLY_EXPLICIT",
"HARM_CATEGORY_DANGEROUS_CONTENT",
]
]
def generate_gemini_response(model, prompt):
response = model.generate_content(
prompt,
generation_config=get_generation_config(),
safety_settings=get_safety_settings()
)
if response.candidates and len(response.candidates) > 0:
return response.candidates[0].content.parts[0].text
return ''
def summarize_text(text):
model = get_gemini_model()
prompt_text = f"Summarize the following research paper very concisely:\n{text[:5000]}" # Truncate to 5000 chars
summary = generate_gemini_response(model, prompt_text)
return summary
#-------------------------------------------------------------
# RESEARCH ASSISTANT MODULE
#-------------------------------------------------------------
def download_pdf(pdf_url, save_path="temp_paper.pdf"):
try:
response = requests.get(pdf_url)
if response.status_code == 200:
with open(save_path, "wb") as file:
file.write(response.content)
return save_path
except Exception as e:
st.error(f"Error downloading PDF: {e}")
return None
def search_arxiv(query, max_results=2):
client = arxiv.Client()
search = arxiv.Search(query=query, max_results=max_results, sort_by=arxiv.SortCriterion.Relevance)
arxiv_docs = []
for result in client.results(search):
pdf_link = next((link.href for link in result.links if 'pdf' in link.href), None)
# Download, extract, and summarize PDF if link exists
if pdf_link:
with st.spinner(f"Processing arXiv paper: {result.title}"):
pdf_path = download_pdf(pdf_link)
if pdf_path:
text = extract_text_from_pdf(pdf_path)
summary = summarize_text(text)
# Clean up downloaded file
if os.path.exists(pdf_path):
os.remove(pdf_path)
else:
summary = "PDF could not be downloaded."
else:
summary = "No PDF available."
content = f"""
**Title:** {result.title}
**Authors:** {', '.join(author.name for author in result.authors)}
**Published:** {result.published.strftime('%Y-%m-%d')}
**Abstract:** {result.summary}
**PDF Summary:** {summary}
**PDF Link:** {pdf_link if pdf_link else 'Not available'}
"""
arxiv_docs.append(Document(page_content=content, metadata={"source": "arXiv", "title": result.title}))
return arxiv_docs
def search_wikipedia(query, max_results=2):
try:
page_titles = wikipedia.search(query, results=max_results)
wiki_docs = []
for title in page_titles:
try:
with st.spinner(f"Processing Wikipedia article: {title}"):
page = wikipedia.page(title)
wiki_docs.append(Document(
page_content=page.content[:2000],
metadata={"source": "Wikipedia", "title": title}
))
except (wikipedia.exceptions.DisambiguationError, wikipedia.exceptions.PageError) as e:
st.warning(f"Error retrieving Wikipedia page {title}: {e}")
return wiki_docs
except Exception as e:
st.error(f"Error searching Wikipedia: {e}")
return []
class ResearchAssistant:
def __init__(self):
# Initialize LLM
self.llm = ChatGroq(
api_key=groq_key,
model="llama3-70b-8192",
temperature=0.2
)
# Set up the prompt template
self.prompt = ChatPromptTemplate.from_template("""
You are an expert research assistant. Use the following context to answer the question.
If you don't know the answer, say so, but try your best to find relevant information
from the provided context and additional context.
Context from user documents:
{context}
Additional context from research sources:
{additional_context}
Question: {input}
Answer:
""")
# Set up the question-answer chain
self.question_answer_chain = create_stuff_documents_chain(
self.llm, self.prompt
)
def retrieve_documents(self, query):
user_context = []
# Get documents from arXiv and Wikipedia
arxiv_docs = search_arxiv(query)
wiki_docs = search_wikipedia(query)
summarized_context = []
for doc in arxiv_docs:
summarized_context.append(f"**ArXiv - {doc.metadata.get('title', 'Unknown Title')}**:\n{doc.page_content}...")
for doc in wiki_docs:
summarized_context.append(f"**Wikipedia - {doc.metadata.get('title', 'Unknown Title')}**:\n{doc.page_content}...")
return user_context, summarized_context
def chat(self, question):
user_context, summarized_context = self.retrieve_documents(question)
input_data = {
"input": question,
"context": "\n\n".join(user_context),
"additional_context": "\n\n".join(summarized_context)
}
with st.spinner("Generating answer..."):
# Use the LLM directly
prompt_text = f"""
Question: {question}
Additional context:
{input_data['additional_context']}
Please provide a comprehensive answer based on the above information.
"""
response = self.llm.invoke(prompt_text)
return response.content, summarized_context
#-------------------------------------------------------------
# DOCUMENT QA MODULE
#-------------------------------------------------------------
# Initialize retrieval chain
@st.cache_resource(show_spinner=False)
def get_retrieval_chain(uploaded_file, model):
with st.spinner("Processing document... This may take a minute."):
# Configure embeddings
genai.configure(api_key=gemini_key)
embeddings = GeminiEmbeddings(api_key=gemini_key)
# Read and process document
docs = read_data_from_doc(uploaded_file)
splits = make_chunks(docs)
# Set up vector store
pc = Pinecone(api_key=pinecone_key)
# Check if index exists, create it if not
indexes = pc.list_indexes()
index_name = "research-rag"
if index_name not in [idx.name for idx in indexes]:
pc.create_index(
name=index_name,
dimension=768, # Dimension for embeddings
metric="cosine"
)
vectorstore = PineconeVectorStore.from_documents(
splits,
embeddings,
index_name=index_name,
)
retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 4})
# Set up LLM and chain
llm = ChatGroq(model_name=model, temperature=0.75, api_key=groq_key)
system_prompt = """
You are an AI assistant answering questions based on retrieved documents and additional context.
Use the provided context from both database retrieval and additional sources to answer the question.
- **Discard irrelevant context:** If one of the contexts (retrieved or additional) does not match the question, ignore it.
- **Highlight conflicting information:** If multiple sources provide conflicting information, explicitly mention it by saying:
- "According to the retrieved context, ... but as per internet sources, ..."
- "According to the retrieved context, ... but as per internet sources, ..."
- **Prioritize accuracy:** If neither context provides a relevant answer, say "I don't know" instead of guessing.
Provide concise yet informative answers, ensuring clarity and completeness.
Retrieved Context: {context}
Additional Context: {additional_context}
"""
prompt = ChatPromptTemplate.from_messages([
("system", system_prompt),
("human", "{input}\n\nRetrieved Context: {context}\n\nAdditional Context: {additional_context}"),
])
question_answer_chain = create_stuff_documents_chain(llm, prompt)
chain = create_retrieval_chain(retriever, question_answer_chain)
return chain
#-------------------------------------------------------------
# WEB SEARCH MODULE
#-------------------------------------------------------------
# Prompt creation functions
def create_search_prompt(query, context=""):
system_prompt = """You are a smart assistant designed to determine whether a query needs data from a web search or can be answered using a document database.
Consider the provided context if available.
If the query requires external information, No context is provided, Irrelevent context is present or latest information is required, then output the special token <SEARCH>
followed by relevant keywords extracted from the query to optimize for search engine results.
Ensure the keywords are concise and relevant. If document data is sufficient, simply return blank."""
if context:
return f"{system_prompt}\n\nContext: {context}\n\nQuery: {query}"
return f"{system_prompt}\n\nQuery: {query}"
def create_summary_prompt(content):
return f"""Please provide a comprehensive yet concise summary of the following content, highlighting the most important points and maintaining factual accuracy. Organize the information in a clear and coherent manner:
Content to summarize:
{content}
Summary:"""
# Web scraping functions
def init_selenium_driver():
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
driver = webdriver.Chrome(options=chrome_options)
return driver
def extract_static_page(url):
try:
response = requests.get(url, timeout=5)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'lxml')
text = soup.get_text(separator=" ", strip=True)
return text[:5000]
except requests.exceptions.RequestException as e:
st.error(f"Error fetching page: {e}")
return None
def extract_dynamic_page(url, driver):
try:
driver.get(url)
time.sleep(random.uniform(2, 5))
body = driver.find_element(By.TAG_NAME, "body")
ActionChains(driver).move_to_element(body).perform()
time.sleep(random.uniform(2, 5))
page_source = driver.page_source
tree = html.fromstring(page_source)
text = tree.xpath('//body//text()')
text_content = ' '.join(text).strip()
return text_content[:1000]
except Exception as e:
st.error(f"Error fetching dynamic page: {e}")
return None
def scrape_page(url):
if "javascript" in url or "dynamic" in url:
driver = init_selenium_driver()
text = extract_dynamic_page(url, driver)
driver.quit()
else:
text = extract_static_page(url)
return text
def scrape_web(urls, max_urls=5):
texts = []
for url in urls[:max_urls]:
text = scrape_page(url)
if text:
texts.append(text)
else:
st.warning(f"Failed to retrieve content from {url}")
return texts
# Main web search functions
def check_search_needed(model, query, context):
prompt = create_search_prompt(query, context)
response = generate_gemini_response(model, prompt)
if "<SEARCH>" in response:
search_terms = response.split("<SEARCH>")[1].strip()
return True, search_terms
return False, None
def summarize_content(model, content):
prompt = create_summary_prompt(content)
return generate_gemini_response(model, prompt)
def process_query(query, context=''):
with st.spinner("Processing query..."):
model = get_gemini_model()
search_tool = DuckDuckGoSearchRun()
needs_search, search_terms = check_search_needed(model, query, context)
result = {
"original_query": query,
"needs_search": needs_search,
"search_terms": search_terms,
"web_content": None,
"summary": None
}
if needs_search:
with st.spinner(f"Searching the web for: {search_terms}"):
search_results = search_tool.run(search_terms)
result["web_content"] = search_results
with st.spinner("Summarizing search results..."):
summary = summarize_content(model, search_results)
result["summary"] = summary
return result
#-------------------------------------------------------------
# MAIN APP
#-------------------------------------------------------------
def display_header():
st.title("π AI Research Assistant")
st.markdown("Your all-in-one tool for research, document analysis, and web search")
def main():
# App header
display_header()
# Sidebar navigation
with st.sidebar:
st.title("Navigation")
app_mode = st.radio("Choose a mode:",
["Research Assistant", "Document Q&A", "Web Search"])
st.markdown("---")
st.subheader("About")
st.markdown("""
This AI Research Assistant helps you find and analyze information from various sources:
- arXiv papers
- Wikipedia articles
- Your own uploaded documents
- Web search results
""")
# API keys status
st.markdown("---")
st.subheader("API Status")
if groq_key:
st.success("β
Groq API connected")
else:
st.error("β Groq API key missing")
if gemini_key:
st.success("β
Gemini API connected")
else:
st.error("β Gemini API key missing")
if pinecone_key:
st.success("β
Pinecone API connected")
else:
st.error("β Pinecone API key missing")
# Research Assistant Mode
if app_mode == "Research Assistant":
st.header("Research Assistant")
st.markdown("Ask research questions and get answers from arXiv papers and Wikipedia.")
# Initialize session state for chat history
if "research_history" not in st.session_state:
st.session_state.research_history = []
# Initialize Research Assistant
if "research_assistant" not in st.session_state:
with st.spinner("Initializing Research Assistant..."):
st.session_state.research_assistant = ResearchAssistant()
# Input area
with st.form(key="research_form"):
question = st.text_input("Your research question:", key="research_question")
submit_button = st.form_submit_button("Search")
# Clear chat button
if st.button("Clear Chat"):
st.session_state.research_history = []
st.rerun()
# Process query when submitted
if submit_button and question:
# Add user query to chat history
st.session_state.research_history.append({"role": "user", "content": question})
# Get response from assistant
answer, sources = st.session_state.research_assistant.chat(question)
# Add assistant response to chat history
st.session_state.research_history.append({
"role": "assistant",
"content": answer,
"sources": sources
})
# Display chat history
for message in st.session_state.research_history:
if message["role"] == "user":
st.write(f"π€ **You:** {message['content']}")
else:
st.write(f"π€ **AI Assistant:**")
st.markdown(message["content"])
# Display sources in expandable section
if message.get("sources"):
with st.expander("View Sources"):
for i, source in enumerate(message["sources"], 1):
st.markdown(f"**Source {i}:**")
st.markdown(source)
st.markdown("---")
# Document Q&A Mode
elif app_mode == "Document Q&A":
st.header("Document Q&A")
st.markdown("Upload a PDF document and ask questions about it.")
# Model selection
model_name = st.selectbox(
"Select Groq Model",
[
"llama3-70b-8192",
"gemma2-9b-it",
"llama-3.3-70b-versatile",
"llama-3.1-8b-instant",
"llama-guard-3-8b",
"mixtral-8x7b-32768",
"deepseek-r1-distill-llama-70b",
"llama-3.2-1b-preview"
],
index=0
)
# Initialize session state for conversation history
if 'document_conversation' not in st.session_state:
st.session_state.document_conversation = []
# File upload
uploaded_file = st.file_uploader("Upload a PDF document", type="pdf")
if uploaded_file:
try:
chain = get_retrieval_chain(
uploaded_file,
model_name
)
# Show success message
st.success("Document processed successfully! You can now ask questions.")
# Display conversation history
for q, a in st.session_state.document_conversation:
with st.chat_message("user"):
st.write(q)
with st.chat_message("assistant"):
st.write(a)
# Question input
question = st.chat_input("Ask a question about your document...")
if question:
with st.chat_message("user"):
st.write(question)
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
additional_context = "" # Can be modified to add external context if needed
result = chain.invoke({
"input": question,
"additional_context": additional_context
})
answer = result['answer']
st.write(answer)
# Store in conversation history
st.session_state.document_conversation.append((question, answer))
except Exception as e:
st.error(f"An error occurred: {str(e)}")
elif not (groq_key and gemini_key and pinecone_key):
st.warning("Please make sure all API keys are properly configured.")
# Web Search Mode
else:
st.header("Web Search")
st.markdown("Search the web for answers to your questions.")
# Input area
with st.form("web_query_form"):
query = st.text_area("Enter your research question", height=100,
placeholder="E.g., What are the latest developments in quantum computing?")
context = st.text_area("Optional: Add any context", height=100,
placeholder="Add any additional context that might help with the research")
submit_button = st.form_submit_button("π Research")
if submit_button and query:
result = process_query(query, context)
if result["needs_search"]:
st.success("Research completed!")
with st.expander("Search Details", expanded=False):
st.subheader("Search Terms Used")
st.info(result["search_terms"])
st.subheader("Raw Web Content")
st.text_area("Web Content", result["web_content"], height=200)
st.subheader("Summary of Findings")
st.markdown(result["summary"])
else:
st.info("Based on the analysis, no web search was needed for this query.")
if __name__ == "__main__":
main() |