Spaces:
Runtime error
Runtime error
Update hf_app.py
Browse files
hf_app.py
CHANGED
|
@@ -2,17 +2,14 @@ import streamlit as st
|
|
| 2 |
from PyPDF2 import PdfReader
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# ---
|
| 6 |
-
from
|
| 7 |
|
| 8 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 9 |
from langchain_community.vectorstores import FAISS
|
| 10 |
from langchain_core.prompts import ChatPromptTemplate
|
| 11 |
from langchain_huggingface import HuggingFaceEndpoint, HuggingFaceEndpointEmbeddings, ChatHuggingFace
|
| 12 |
-
|
| 13 |
-
from langchain.chains.combine_documents import create_stuff_documents_chain
|
| 14 |
-
|
| 15 |
-
# --- PDF Functions (UPDATED) ---
|
| 16 |
@st.cache_data
|
| 17 |
def get_pdf_text(pdf_docs):
|
| 18 |
"""
|
|
@@ -67,6 +64,7 @@ def get_hf_llm():
|
|
| 67 |
llm = HuggingFaceEndpoint(
|
| 68 |
huggingfacehub_api_token=st.secrets["HF_API_TOKEN"],
|
| 69 |
repo_id="mistralai/Mistral-7B-Instruct-v0.2",
|
|
|
|
| 70 |
temperature=0.3,
|
| 71 |
max_new_tokens=512,
|
| 72 |
task="conversational"
|
|
@@ -115,20 +113,12 @@ def get_conversational_chain():
|
|
| 115 |
return None
|
| 116 |
|
| 117 |
system_prompt = """
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
3. If you are uncertain about the answer or if the PDFs and your knowledge do not provide enough information to form a conclusive answer, state clearly that the information is not sufficient to provide a definitive answer.
|
| 124 |
-
4. Always answer the question in details point by point.
|
| 125 |
|
| 126 |
-
**Important:** If the user prompt is not related to the PDF content, Just say "I don't know this answer".
|
| 127 |
-
**Important:** If the user prompt is not related to the PDF content, Never say the answer if you know then still don't say.
|
| 128 |
-
|
| 129 |
-
### CONTEXT ###
|
| 130 |
-
{context}
|
| 131 |
-
#################
|
| 132 |
"""
|
| 133 |
|
| 134 |
prompt = ChatPromptTemplate.from_messages([
|
|
@@ -137,7 +127,9 @@ def get_conversational_chain():
|
|
| 137 |
("human", "Question: {input}")
|
| 138 |
])
|
| 139 |
|
| 140 |
-
|
|
|
|
|
|
|
| 141 |
|
| 142 |
# --- User Input Handler (UPDATED) ---
|
| 143 |
def user_input(user_question, db):
|
|
@@ -162,7 +154,17 @@ def user_input(user_question, db):
|
|
| 162 |
|
| 163 |
# 3. Display Response
|
| 164 |
st.markdown("### Reply:")
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
# # 4. Display Page Numbers (New Feature)
|
| 168 |
# st.markdown("---")
|
|
@@ -185,7 +187,7 @@ def user_input(user_question, db):
|
|
| 185 |
# --- Main App ---
|
| 186 |
def main():
|
| 187 |
st.set_page_config(page_title="Chat PDF (Hugging Face)")
|
| 188 |
-
st.header("Chat with PDF using Mistral-7B-Instruct-v0.2")
|
| 189 |
|
| 190 |
if "faiss_db" not in st.session_state:
|
| 191 |
st.session_state.faiss_db = None
|
|
|
|
| 2 |
from PyPDF2 import PdfReader
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# --- UPDATED IMPORTS (LANGCHAIN v1.1 COMPATIBLE) ---
|
| 6 |
+
from langchain_core.documents import Document
|
| 7 |
|
| 8 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 9 |
from langchain_community.vectorstores import FAISS
|
| 10 |
from langchain_core.prompts import ChatPromptTemplate
|
| 11 |
from langchain_huggingface import HuggingFaceEndpoint, HuggingFaceEndpointEmbeddings, ChatHuggingFace
|
| 12 |
+
# ... rest of your code ...
|
|
|
|
|
|
|
|
|
|
| 13 |
@st.cache_data
|
| 14 |
def get_pdf_text(pdf_docs):
|
| 15 |
"""
|
|
|
|
| 64 |
llm = HuggingFaceEndpoint(
|
| 65 |
huggingfacehub_api_token=st.secrets["HF_API_TOKEN"],
|
| 66 |
repo_id="mistralai/Mistral-7B-Instruct-v0.2",
|
| 67 |
+
# repo_id="meta-llama/Llama-3.2-3B-Instruct",
|
| 68 |
temperature=0.3,
|
| 69 |
max_new_tokens=512,
|
| 70 |
task="conversational"
|
|
|
|
| 113 |
return None
|
| 114 |
|
| 115 |
system_prompt = """
|
| 116 |
+
You are a helpful AI assistant answering questions based on PDF documents.
|
| 117 |
+
If the answer is not in the context, say you don't know and do not give any text about the question, just say "I don't know."
|
| 118 |
+
Context:
|
| 119 |
+
{context}
|
| 120 |
+
**Note:** please do not answer questions that are not related to the context provided. If the context does not contain the answer, respond with "I don't know."
|
|
|
|
|
|
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
"""
|
| 123 |
|
| 124 |
prompt = ChatPromptTemplate.from_messages([
|
|
|
|
| 127 |
("human", "Question: {input}")
|
| 128 |
])
|
| 129 |
|
| 130 |
+
# Create a simple chain using the pipe operator (|)
|
| 131 |
+
chain = prompt | model
|
| 132 |
+
return chain
|
| 133 |
|
| 134 |
# --- User Input Handler (UPDATED) ---
|
| 135 |
def user_input(user_question, db):
|
|
|
|
| 154 |
|
| 155 |
# 3. Display Response
|
| 156 |
st.markdown("### Reply:")
|
| 157 |
+
# Extract just the content text from the response
|
| 158 |
+
if hasattr(response, 'content'):
|
| 159 |
+
# Response is an AIMessage object
|
| 160 |
+
st.write(response.content)
|
| 161 |
+
elif isinstance(response, dict) and "answer" in response:
|
| 162 |
+
st.write(response["answer"])
|
| 163 |
+
elif isinstance(response, dict) and "content" in response:
|
| 164 |
+
st.write(response["content"])
|
| 165 |
+
else:
|
| 166 |
+
# Try to get string representation
|
| 167 |
+
st.write(str(response))
|
| 168 |
|
| 169 |
# # 4. Display Page Numbers (New Feature)
|
| 170 |
# st.markdown("---")
|
|
|
|
| 187 |
# --- Main App ---
|
| 188 |
def main():
|
| 189 |
st.set_page_config(page_title="Chat PDF (Hugging Face)")
|
| 190 |
+
st.header("Chat with PDF using mistralai/Mistral-7B-Instruct-v0.2")
|
| 191 |
|
| 192 |
if "faiss_db" not in st.session_state:
|
| 193 |
st.session_state.faiss_db = None
|