chburhan64 commited on
Commit
9ae830c
Β·
verified Β·
1 Parent(s): bf06fa6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -102
app.py CHANGED
@@ -1,17 +1,18 @@
1
  import streamlit as st
2
  import os
3
- import time
4
  from dotenv import load_dotenv
5
- import PyPDF2
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, RetrievalQA
14
- from langchain_core.prompts import ChatPromptTemplate
 
 
 
 
 
15
 
16
  # Load environment variables
17
  load_dotenv()
@@ -28,66 +29,6 @@ llm = ChatGroq(groq_api_key=groq_api_key, model_name="Llama3-8b-8192")
28
  # Load embedding model
29
  embedding = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
30
 
31
- # Prompt Templates
32
- summary_prompt = ChatPromptTemplate.from_template("""
33
- You are a helpful assistant. Summarize the following document clearly and accurately:
34
- <context>
35
- {context}
36
- </context>
37
- """)
38
-
39
- gap_prompt = ChatPromptTemplate.from_template("""
40
- Analyze the following summary and identify key research gaps, unanswered questions, or limitations:
41
- {summary}
42
- """)
43
-
44
- idea_prompt = ChatPromptTemplate.from_template("""
45
- Given the research gaps:
46
- {gaps}
47
- Suggest 2-3 original research project ideas or questions that address these gaps. Explain why they are valuable.
48
- """)
49
-
50
- debate_prompt = ChatPromptTemplate.from_template("""
51
- Act as two researchers discussing a paper.
52
- Supporter: Defends the core idea of the document.
53
- Critic: Challenges its assumptions, methods, or impact.
54
- Use the following summary as reference:
55
- {summary}
56
- Generate a short conversation between them.
57
- """)
58
-
59
- translate_prompt = ChatPromptTemplate.from_template("""
60
- Translate the following content into {language}, preserving meaning and academic tone:
61
- {content}
62
- """)
63
-
64
- citation_prompt = ChatPromptTemplate.from_template("""
65
- Generate an APA-style citation based on the document content:
66
- <context>
67
- {context}
68
- </context>
69
- """)
70
-
71
- # Extract & process PDFs
72
- def process_pdfs(uploaded_files):
73
- documents = []
74
- for file in uploaded_files:
75
- reader = PyPDF2.PdfReader(file)
76
- text = ""
77
- for page in reader.pages:
78
- text += page.extract_text() or ""
79
- documents.append(Document(page_content=text, metadata={"source": file.name}))
80
- splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
81
- return splitter.split_documents(documents)
82
-
83
- # Create vector store
84
- def create_vector_store(documents):
85
- return FAISS.from_documents(documents, embedding)
86
-
87
- # Chain runner helpers
88
- def run_chain(chain, input_dict):
89
- return chain.invoke(input_dict)
90
-
91
  # File uploader
92
  uploaded_files = st.file_uploader("πŸ“ Upload one or more PDF files", type=["pdf"], accept_multiple_files=True)
93
 
@@ -95,7 +36,7 @@ if uploaded_files and st.button("πŸ“š Process Documents"):
95
  with st.spinner("Processing documents and generating vector store..."):
96
  documents = process_pdfs(uploaded_files)
97
  st.session_state.documents = documents
98
- st.session_state.vectorstore = create_vector_store(documents)
99
  st.success("βœ… Document vector store created!")
100
 
101
  # Agent Activation
@@ -115,9 +56,7 @@ if "documents" in st.session_state:
115
  query = st.text_input("πŸ’¬ Ask a question about the paper:")
116
  if query and st.button("πŸš€ Ask Question"):
117
  with st.spinner("Searching paper for answer..."):
118
- retriever = st.session_state.vectorstore.as_retriever()
119
- qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever)
120
- output = qa_chain.run(query)
121
  st.session_state["last_agent_output"] = output
122
 
123
  # Handle other tasks
@@ -127,32 +66,19 @@ if "documents" in st.session_state:
127
  output = ""
128
 
129
  if task == "Summarize document":
130
- chain = create_stuff_documents_chain(llm, summary_prompt)
131
- output = run_chain(chain, {"context": docs})
132
 
133
  elif task == "Identify research gaps":
134
- chain1 = create_stuff_documents_chain(llm, summary_prompt)
135
- summary = run_chain(chain1, {"context": docs})
136
- chain2 = LLMChain(llm=llm, prompt=gap_prompt)
137
- output = run_chain(chain2, {"summary": summary})
138
 
139
  elif task == "Suggest research ideas":
140
- chain1 = create_stuff_documents_chain(llm, summary_prompt)
141
- summary = run_chain(chain1, {"context": docs})
142
- chain2 = LLMChain(llm=llm, prompt=gap_prompt)
143
- gaps = run_chain(chain2, {"summary": summary})
144
- chain3 = LLMChain(llm=llm, prompt=idea_prompt)
145
- output = run_chain(chain3, {"gaps": gaps})
146
 
147
  elif task == "Simulate a debate":
148
- chain = create_stuff_documents_chain(llm, summary_prompt)
149
- summary = run_chain(chain, {"context": docs})
150
- debate_chain = LLMChain(llm=llm, prompt=debate_prompt)
151
- output = run_chain(debate_chain, {"summary": summary})
152
 
153
  elif task == "Generate citation":
154
- citation_chain = create_stuff_documents_chain(llm, citation_prompt)
155
- output = run_chain(citation_chain, {"context": docs})
156
 
157
  if output:
158
  st.session_state["last_agent_output"] = output
@@ -176,16 +102,6 @@ if "last_agent_output" in st.session_state:
176
  user_language = selected_language
177
 
178
  if user_language:
179
- if isinstance(output, dict):
180
- combined_text = "\n\n".join(str(v) for v in output.values())
181
- else:
182
- combined_text = str(output)
183
-
184
- translate_chain = LLMChain(llm=llm, prompt=translate_prompt)
185
- translated = translate_chain.invoke({
186
- "language": user_language,
187
- "content": combined_text
188
- })
189
-
190
  st.markdown(f"### 🌐 Translated Response ({user_language})")
191
  st.write(translated)
 
1
  import streamlit as st
2
  import os
 
3
  from dotenv import load_dotenv
 
 
4
  from langchain_groq import ChatGroq
 
5
  from langchain_community.embeddings import HuggingFaceEmbeddings
6
+
7
+ # Import all modules
8
+ from document_processor import process_pdfs, create_vector_store
9
+ from summarizer import summarize_document
10
+ from gap_analyzer import identify_research_gaps
11
+ from idea_generator import suggest_research_ideas
12
+ from debate_simulator import simulate_debate
13
+ from citation_generator import generate_citation
14
+ from chat_handler import chat_with_paper
15
+ from translator import translate_text
16
 
17
  # Load environment variables
18
  load_dotenv()
 
29
  # Load embedding model
30
  embedding = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  # File uploader
33
  uploaded_files = st.file_uploader("πŸ“ Upload one or more PDF files", type=["pdf"], accept_multiple_files=True)
34
 
 
36
  with st.spinner("Processing documents and generating vector store..."):
37
  documents = process_pdfs(uploaded_files)
38
  st.session_state.documents = documents
39
+ st.session_state.vectorstore = create_vector_store(documents, embedding)
40
  st.success("βœ… Document vector store created!")
41
 
42
  # Agent Activation
 
56
  query = st.text_input("πŸ’¬ Ask a question about the paper:")
57
  if query and st.button("πŸš€ Ask Question"):
58
  with st.spinner("Searching paper for answer..."):
59
+ output = chat_with_paper(llm, st.session_state.vectorstore, query)
 
 
60
  st.session_state["last_agent_output"] = output
61
 
62
  # Handle other tasks
 
66
  output = ""
67
 
68
  if task == "Summarize document":
69
+ output = summarize_document(llm, docs)
 
70
 
71
  elif task == "Identify research gaps":
72
+ output = identify_research_gaps(llm, docs)
 
 
 
73
 
74
  elif task == "Suggest research ideas":
75
+ output = suggest_research_ideas(llm, docs)
 
 
 
 
 
76
 
77
  elif task == "Simulate a debate":
78
+ output = simulate_debate(llm, docs)
 
 
 
79
 
80
  elif task == "Generate citation":
81
+ output = generate_citation(llm, docs)
 
82
 
83
  if output:
84
  st.session_state["last_agent_output"] = output
 
102
  user_language = selected_language
103
 
104
  if user_language:
105
+ translated = translate_text(llm, output, user_language)
 
 
 
 
 
 
 
 
 
 
106
  st.markdown(f"### 🌐 Translated Response ({user_language})")
107
  st.write(translated)