Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,30 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
import time
|
| 4 |
-
|
| 5 |
-
import
|
|
|
|
| 6 |
|
|
|
|
| 7 |
from langchain_groq import ChatGroq
|
| 8 |
from langchain_community.vectorstores import FAISS
|
| 9 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 10 |
from langchain_core.documents import Document
|
| 11 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 12 |
from langchain.chains.combine_documents import create_stuff_documents_chain
|
| 13 |
-
from langchain.chains import LLMChain
|
| 14 |
from langchain_core.prompts import ChatPromptTemplate
|
| 15 |
|
| 16 |
# Load environment variables
|
| 17 |
load_dotenv()
|
| 18 |
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 19 |
|
| 20 |
-
# Streamlit UI setup
|
| 21 |
st.set_page_config(page_title="Multi-Agent Research Assistant", layout="wide")
|
| 22 |
st.title("π€ Multi-Agent Research Assistant")
|
| 23 |
-
st.markdown("
|
| 24 |
|
| 25 |
-
# Load
|
| 26 |
llm = ChatGroq(groq_api_key=groq_api_key, model_name="Llama3-8b-8192")
|
| 27 |
-
|
| 28 |
-
# Load embedding model
|
| 29 |
embedding = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 30 |
|
| 31 |
# Prompt Templates
|
|
@@ -38,82 +37,64 @@ You are a helpful assistant. Summarize the following document clearly and accura
|
|
| 38 |
|
| 39 |
gap_prompt = ChatPromptTemplate.from_template("""
|
| 40 |
Analyze the following summary and identify key research gaps, unanswered questions, or limitations:
|
| 41 |
-
|
| 42 |
{summary}
|
| 43 |
""")
|
| 44 |
|
| 45 |
idea_prompt = ChatPromptTemplate.from_template("""
|
| 46 |
Given the research gaps:
|
| 47 |
{gaps}
|
| 48 |
-
|
| 49 |
Suggest 2-3 original research project ideas or questions that address these gaps. Explain why they are valuable.
|
| 50 |
""")
|
| 51 |
|
| 52 |
debate_prompt = ChatPromptTemplate.from_template("""
|
| 53 |
Act as two researchers discussing a paper.
|
| 54 |
-
|
| 55 |
Supporter: Defends the core idea of the document.
|
| 56 |
Critic: Challenges its assumptions, methods, or impact.
|
| 57 |
-
|
| 58 |
Use the following summary as reference:
|
| 59 |
{summary}
|
| 60 |
-
|
| 61 |
Generate a short conversation between them.
|
| 62 |
""")
|
| 63 |
|
| 64 |
translate_prompt = ChatPromptTemplate.from_template("""
|
| 65 |
Translate the following content into {language}, preserving meaning and academic tone:
|
| 66 |
-
|
| 67 |
{content}
|
| 68 |
""")
|
| 69 |
|
| 70 |
-
|
| 71 |
-
Generate an APA-style citation based on the document content:
|
| 72 |
-
|
| 73 |
-
<context>
|
| 74 |
-
{context}
|
| 75 |
-
</context>
|
| 76 |
-
""")
|
| 77 |
-
|
| 78 |
-
# Extract & process PDFs
|
| 79 |
def process_pdfs(uploaded_files):
|
| 80 |
documents = []
|
| 81 |
for file in uploaded_files:
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
for page in reader.pages:
|
| 85 |
-
text += page.extract_text() or ""
|
| 86 |
documents.append(Document(page_content=text, metadata={"source": file.name}))
|
| 87 |
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 88 |
return splitter.split_documents(documents)
|
| 89 |
|
| 90 |
-
# Create vector store
|
| 91 |
def create_vector_store(documents):
|
| 92 |
return FAISS.from_documents(documents, embedding)
|
| 93 |
|
| 94 |
-
# Chain runner helpers
|
| 95 |
def run_chain(chain, input_dict):
|
| 96 |
return chain.invoke(input_dict)
|
| 97 |
|
| 98 |
-
# File uploader
|
| 99 |
uploaded_files = st.file_uploader("π Upload one or more PDF files", type=["pdf"], accept_multiple_files=True)
|
| 100 |
|
| 101 |
if uploaded_files and st.button("π Process Documents"):
|
| 102 |
-
with st.spinner("Processing
|
| 103 |
documents = process_pdfs(uploaded_files)
|
| 104 |
st.session_state.documents = documents
|
| 105 |
st.session_state.vectorstore = create_vector_store(documents)
|
| 106 |
st.success("β
Document vector store created!")
|
| 107 |
|
| 108 |
-
# Agent Activation
|
| 109 |
if "documents" in st.session_state:
|
| 110 |
-
st.subheader("π
|
| 111 |
-
task = st.selectbox("
|
| 112 |
"Summarize document",
|
| 113 |
"Identify research gaps",
|
| 114 |
"Suggest research ideas",
|
| 115 |
"Simulate a debate",
|
| 116 |
-
"Generate citation"
|
|
|
|
|
|
|
| 117 |
])
|
| 118 |
|
| 119 |
if st.button("π Run Agent"):
|
|
@@ -121,73 +102,89 @@ if "documents" in st.session_state:
|
|
| 121 |
docs = st.session_state.documents[:10]
|
| 122 |
results = {}
|
| 123 |
|
| 124 |
-
# Summarization
|
| 125 |
if task == "Summarize document":
|
| 126 |
chain = create_stuff_documents_chain(llm, summary_prompt)
|
| 127 |
summary = run_chain(chain, {"context": docs})
|
| 128 |
st.session_state["last_agent_output"] = summary
|
| 129 |
|
| 130 |
-
# Gap analysis
|
| 131 |
elif task == "Identify research gaps":
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
chain2 = LLMChain(llm=llm, prompt=gap_prompt)
|
| 135 |
-
gaps = run_chain(chain2, {"summary": summary})
|
| 136 |
st.session_state["last_agent_output"] = gaps
|
| 137 |
|
| 138 |
-
# Idea generation
|
| 139 |
elif task == "Suggest research ideas":
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
gaps = run_chain(chain2, {"summary": summary})
|
| 144 |
-
chain3 = LLMChain(llm=llm, prompt=idea_prompt)
|
| 145 |
-
ideas = run_chain(chain3, {"gaps": gaps})
|
| 146 |
st.session_state["last_agent_output"] = ideas
|
| 147 |
|
| 148 |
-
# Debate agent
|
| 149 |
elif task == "Simulate a debate":
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
debate_chain = LLMChain(llm=llm, prompt=debate_prompt)
|
| 153 |
-
debate = run_chain(debate_chain, {"summary": summary})
|
| 154 |
st.session_state["last_agent_output"] = debate
|
| 155 |
|
| 156 |
-
# Citation agent
|
| 157 |
elif task == "Generate citation":
|
| 158 |
-
citation_chain = create_stuff_documents_chain(llm,
|
| 159 |
citation = run_chain(citation_chain, {"context": docs})
|
| 160 |
st.session_state["last_agent_output"] = citation
|
| 161 |
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
if "last_agent_output" in st.session_state:
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
default_languages = ["Spanish", "French", "German", "Chinese", "Urdu", "Other"]
|
| 174 |
-
selected_language = st.selectbox("Choose
|
| 175 |
if selected_language == "Other":
|
| 176 |
-
user_language = st.text_input("
|
| 177 |
else:
|
| 178 |
user_language = selected_language
|
| 179 |
-
|
| 180 |
if user_language:
|
| 181 |
-
if isinstance(output, dict):
|
| 182 |
-
combined_text = "\n\n".join(str(v) for v in output.values())
|
| 183 |
-
else:
|
| 184 |
-
combined_text = str(output)
|
| 185 |
-
|
| 186 |
translate_chain = LLMChain(llm=llm, prompt=translate_prompt)
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
})
|
| 191 |
-
|
| 192 |
st.markdown(f"### π Translated Response ({user_language})")
|
| 193 |
st.write(translated)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
import time
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import pdfplumber
|
| 7 |
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
from langchain_groq import ChatGroq
|
| 10 |
from langchain_community.vectorstores import FAISS
|
| 11 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 12 |
from langchain_core.documents import Document
|
| 13 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 14 |
from langchain.chains.combine_documents import create_stuff_documents_chain
|
| 15 |
+
from langchain.chains import LLMChain, RetrievalQA
|
| 16 |
from langchain_core.prompts import ChatPromptTemplate
|
| 17 |
|
| 18 |
# Load environment variables
|
| 19 |
load_dotenv()
|
| 20 |
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 21 |
|
|
|
|
| 22 |
st.set_page_config(page_title="Multi-Agent Research Assistant", layout="wide")
|
| 23 |
st.title("π€ Multi-Agent Research Assistant")
|
| 24 |
+
st.markdown("Upload your PDF research paper and explore multiple intelligent agents: summarize, question-answer, extract visuals, translate, and more!")
|
| 25 |
|
| 26 |
+
# Load models
|
| 27 |
llm = ChatGroq(groq_api_key=groq_api_key, model_name="Llama3-8b-8192")
|
|
|
|
|
|
|
| 28 |
embedding = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 29 |
|
| 30 |
# Prompt Templates
|
|
|
|
| 37 |
|
| 38 |
gap_prompt = ChatPromptTemplate.from_template("""
|
| 39 |
Analyze the following summary and identify key research gaps, unanswered questions, or limitations:
|
|
|
|
| 40 |
{summary}
|
| 41 |
""")
|
| 42 |
|
| 43 |
idea_prompt = ChatPromptTemplate.from_template("""
|
| 44 |
Given the research gaps:
|
| 45 |
{gaps}
|
|
|
|
| 46 |
Suggest 2-3 original research project ideas or questions that address these gaps. Explain why they are valuable.
|
| 47 |
""")
|
| 48 |
|
| 49 |
debate_prompt = ChatPromptTemplate.from_template("""
|
| 50 |
Act as two researchers discussing a paper.
|
|
|
|
| 51 |
Supporter: Defends the core idea of the document.
|
| 52 |
Critic: Challenges its assumptions, methods, or impact.
|
|
|
|
| 53 |
Use the following summary as reference:
|
| 54 |
{summary}
|
|
|
|
| 55 |
Generate a short conversation between them.
|
| 56 |
""")
|
| 57 |
|
| 58 |
translate_prompt = ChatPromptTemplate.from_template("""
|
| 59 |
Translate the following content into {language}, preserving meaning and academic tone:
|
|
|
|
| 60 |
{content}
|
| 61 |
""")
|
| 62 |
|
| 63 |
+
# PDF processing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
def process_pdfs(uploaded_files):
|
| 65 |
documents = []
|
| 66 |
for file in uploaded_files:
|
| 67 |
+
with pdfplumber.open(file) as pdf:
|
| 68 |
+
text = "\n".join(page.extract_text() or "" for page in pdf.pages)
|
|
|
|
|
|
|
| 69 |
documents.append(Document(page_content=text, metadata={"source": file.name}))
|
| 70 |
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 71 |
return splitter.split_documents(documents)
|
| 72 |
|
|
|
|
| 73 |
def create_vector_store(documents):
|
| 74 |
return FAISS.from_documents(documents, embedding)
|
| 75 |
|
|
|
|
| 76 |
def run_chain(chain, input_dict):
|
| 77 |
return chain.invoke(input_dict)
|
| 78 |
|
|
|
|
| 79 |
uploaded_files = st.file_uploader("π Upload one or more PDF files", type=["pdf"], accept_multiple_files=True)
|
| 80 |
|
| 81 |
if uploaded_files and st.button("π Process Documents"):
|
| 82 |
+
with st.spinner("Processing and embedding..."):
|
| 83 |
documents = process_pdfs(uploaded_files)
|
| 84 |
st.session_state.documents = documents
|
| 85 |
st.session_state.vectorstore = create_vector_store(documents)
|
| 86 |
st.success("β
Document vector store created!")
|
| 87 |
|
|
|
|
| 88 |
if "documents" in st.session_state:
|
| 89 |
+
st.subheader("π Choose an agent task:")
|
| 90 |
+
task = st.selectbox("Task:", [
|
| 91 |
"Summarize document",
|
| 92 |
"Identify research gaps",
|
| 93 |
"Suggest research ideas",
|
| 94 |
"Simulate a debate",
|
| 95 |
+
"Generate citation",
|
| 96 |
+
"Chat with Paper",
|
| 97 |
+
"Generate Chart + Insight"
|
| 98 |
])
|
| 99 |
|
| 100 |
if st.button("π Run Agent"):
|
|
|
|
| 102 |
docs = st.session_state.documents[:10]
|
| 103 |
results = {}
|
| 104 |
|
|
|
|
| 105 |
if task == "Summarize document":
|
| 106 |
chain = create_stuff_documents_chain(llm, summary_prompt)
|
| 107 |
summary = run_chain(chain, {"context": docs})
|
| 108 |
st.session_state["last_agent_output"] = summary
|
| 109 |
|
|
|
|
| 110 |
elif task == "Identify research gaps":
|
| 111 |
+
summary = run_chain(create_stuff_documents_chain(llm, summary_prompt), {"context": docs})
|
| 112 |
+
gaps = run_chain(LLMChain(llm=llm, prompt=gap_prompt), {"summary": summary})
|
|
|
|
|
|
|
| 113 |
st.session_state["last_agent_output"] = gaps
|
| 114 |
|
|
|
|
| 115 |
elif task == "Suggest research ideas":
|
| 116 |
+
summary = run_chain(create_stuff_documents_chain(llm, summary_prompt), {"context": docs})
|
| 117 |
+
gaps = run_chain(LLMChain(llm=llm, prompt=gap_prompt), {"summary": summary})
|
| 118 |
+
ideas = run_chain(LLMChain(llm=llm, prompt=idea_prompt), {"gaps": gaps})
|
|
|
|
|
|
|
|
|
|
| 119 |
st.session_state["last_agent_output"] = ideas
|
| 120 |
|
|
|
|
| 121 |
elif task == "Simulate a debate":
|
| 122 |
+
summary = run_chain(create_stuff_documents_chain(llm, summary_prompt), {"context": docs})
|
| 123 |
+
debate = run_chain(LLMChain(llm=llm, prompt=debate_prompt), {"summary": summary})
|
|
|
|
|
|
|
| 124 |
st.session_state["last_agent_output"] = debate
|
| 125 |
|
|
|
|
| 126 |
elif task == "Generate citation":
|
| 127 |
+
citation_chain = create_stuff_documents_chain(llm, translate_prompt)
|
| 128 |
citation = run_chain(citation_chain, {"context": docs})
|
| 129 |
st.session_state["last_agent_output"] = citation
|
| 130 |
|
| 131 |
+
elif task == "Chat with Paper":
|
| 132 |
+
user_question = st.text_input("Ask a question about the paper:")
|
| 133 |
+
if user_question:
|
| 134 |
+
qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=st.session_state.vectorstore.as_retriever())
|
| 135 |
+
answer = qa_chain.run(user_question)
|
| 136 |
+
st.session_state["last_agent_output"] = answer
|
| 137 |
+
|
| 138 |
+
elif task == "Generate Chart + Insight":
|
| 139 |
+
numbers = []
|
| 140 |
+
for doc in docs:
|
| 141 |
+
for line in doc.page_content.split("\n"):
|
| 142 |
+
for word in line.split():
|
| 143 |
+
try:
|
| 144 |
+
num = float(word)
|
| 145 |
+
numbers.append(num)
|
| 146 |
+
except:
|
| 147 |
+
pass
|
| 148 |
+
if numbers:
|
| 149 |
+
fig, ax = plt.subplots()
|
| 150 |
+
pd.Series(numbers[:20]).plot(kind="bar", ax=ax)
|
| 151 |
+
st.pyplot(fig)
|
| 152 |
+
explain_prompt = ChatPromptTemplate.from_template("Analyze this data: {data}")
|
| 153 |
+
insight = run_chain(LLMChain(llm=llm, prompt=explain_prompt), {"data": numbers[:20]})
|
| 154 |
+
st.session_state["last_agent_output"] = insight
|
| 155 |
+
else:
|
| 156 |
+
st.write("No numeric data found.")
|
| 157 |
+
|
| 158 |
+
# Display Output
|
| 159 |
if "last_agent_output" in st.session_state:
|
| 160 |
+
st.markdown("### π€ Agent Output")
|
| 161 |
+
st.write(st.session_state["last_agent_output"])
|
| 162 |
+
|
| 163 |
+
# Feedback agent (simple RLHF prototype)
|
| 164 |
+
st.markdown("#### π¬ Was this helpful?")
|
| 165 |
+
col1, col2 = st.columns(2)
|
| 166 |
+
if col1.button("π Yes"):
|
| 167 |
+
with open("feedback_log.csv", "a") as f:
|
| 168 |
+
f.write(f"{task},Yes\n")
|
| 169 |
+
st.success("Thanks for your feedback!")
|
| 170 |
+
if col2.button("π No"):
|
| 171 |
+
with open("feedback_log.csv", "a") as f:
|
| 172 |
+
f.write(f"{task},No\n")
|
| 173 |
+
st.info("Thanks! We'll improve it.")
|
| 174 |
+
|
| 175 |
+
# Translation Option
|
| 176 |
+
if st.toggle("π Translate the response?"):
|
| 177 |
default_languages = ["Spanish", "French", "German", "Chinese", "Urdu", "Other"]
|
| 178 |
+
selected_language = st.selectbox("Choose language:", default_languages)
|
| 179 |
if selected_language == "Other":
|
| 180 |
+
user_language = st.text_input("Enter language:")
|
| 181 |
else:
|
| 182 |
user_language = selected_language
|
|
|
|
| 183 |
if user_language:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
translate_chain = LLMChain(llm=llm, prompt=translate_prompt)
|
| 185 |
+
content = st.session_state["last_agent_output"]
|
| 186 |
+
if isinstance(content, dict):
|
| 187 |
+
content = "\n".join(str(v) for v in content.values())
|
| 188 |
+
translated = translate_chain.invoke({"language": user_language, "content": content})
|
|
|
|
| 189 |
st.markdown(f"### π Translated Response ({user_language})")
|
| 190 |
st.write(translated)
|