Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,13 +4,13 @@ from io import BytesIO
|
|
| 4 |
import os
|
| 5 |
import tempfile
|
| 6 |
|
| 7 |
-
#
|
| 8 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 9 |
-
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
| 10 |
from langchain_community.vectorstores import FAISS
|
| 11 |
-
from
|
| 12 |
-
from
|
| 13 |
-
from
|
| 14 |
|
| 15 |
# --- Configuration ---
|
| 16 |
# Get API key from Hugging Face Secrets
|
|
@@ -74,18 +74,26 @@ def get_conversational_chain(api_key):
|
|
| 74 |
"""
|
| 75 |
model = ChatGoogleGenerativeAI(model="gemini-2.0-flash-exp", temperature=0, google_api_key=api_key)
|
| 76 |
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
| 77 |
-
|
|
|
|
| 78 |
return chain
|
| 79 |
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
def user_input(user_question, api_key):
|
| 82 |
"""Process user question and return answer from the PDF context."""
|
| 83 |
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=api_key)
|
| 84 |
new_db = FAISS.load_local(FAISS_INDEX_PATH, embeddings, allow_dangerous_deserialization=True)
|
| 85 |
docs = new_db.similarity_search(user_question)
|
|
|
|
| 86 |
chain = get_conversational_chain(api_key)
|
| 87 |
-
|
| 88 |
-
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
@st.cache_resource
|
|
|
|
| 4 |
import os
|
| 5 |
import tempfile
|
| 6 |
|
| 7 |
+
# Updated imports for current LangChain
|
| 8 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 9 |
+
from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
|
| 10 |
from langchain_community.vectorstores import FAISS
|
| 11 |
+
from langchain_core.prompts import PromptTemplate
|
| 12 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 13 |
+
from langchain_core.runnables import RunnablePassthrough
|
| 14 |
|
| 15 |
# --- Configuration ---
|
| 16 |
# Get API key from Hugging Face Secrets
|
|
|
|
| 74 |
"""
|
| 75 |
model = ChatGoogleGenerativeAI(model="gemini-2.0-flash-exp", temperature=0, google_api_key=api_key)
|
| 76 |
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
| 77 |
+
|
| 78 |
+
chain = prompt | model | StrOutputParser()
|
| 79 |
return chain
|
| 80 |
|
| 81 |
|
| 82 |
+
def format_docs(docs):
|
| 83 |
+
"""Format documents into a single string for context."""
|
| 84 |
+
return "\n\n".join(doc.page_content for doc in docs)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
def user_input(user_question, api_key):
|
| 88 |
"""Process user question and return answer from the PDF context."""
|
| 89 |
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=api_key)
|
| 90 |
new_db = FAISS.load_local(FAISS_INDEX_PATH, embeddings, allow_dangerous_deserialization=True)
|
| 91 |
docs = new_db.similarity_search(user_question)
|
| 92 |
+
|
| 93 |
chain = get_conversational_chain(api_key)
|
| 94 |
+
context = format_docs(docs)
|
| 95 |
+
response = chain.invoke({"context": context, "question": user_question})
|
| 96 |
+
return response
|
| 97 |
|
| 98 |
|
| 99 |
@st.cache_resource
|