chatbot_pdf_search / chatbot_pdf_search
Geinji's picture
Update chatbot_pdf_search
00ab020 verified
# Constants
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions" # Replace with your Groq endpoint
GROQ_SECRET_KEY = st.secrets["groq_api_key"] # Replace with your secret key
HUGGINGFACE_MODEL = "mistralai/Mistral-Small-Instruct-2409" # Replace with your Hugging Face model
# Load the Hugging Face model for question-answering
qa_pipeline = pipeline("question-answering", model=HUGGINGFACE_MODEL)
# Function to extract text from PDF
def extract_text_from_pdf(pdf_file):
reader = PyPDF2.PdfReader(pdf_file)
text = ''
for page in reader.pages:
text += page.extract_text() + '\n'
return text
# Function to run inference using Groq
def groq_inference(question, context):
headers = {
"Authorization": f"Bearer {GROQ_SECRET_KEY}",
"Content-Type": "application/json"
}
payload = {
"question": question,
"context": context
}
response = requests.post(GROQ_API_URL, headers=headers, json=payload)
if response.status_code == 200:
return response.json()['answer']
else:
return "Error: Unable to get response from Groq."
# Streamlit UI
st.title("Document Chatbot")
st.write("Upload a PDF document to interact with it!")
# File uploader
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
if uploaded_file:
# Extract text from the uploaded PDF
document_text = extract_text_from_pdf(uploaded_file)
st.write("Document successfully uploaded and processed.")
# Chat interface
user_question = st.text_input("Ask a question about the document:")
if user_question:
# First try to get the answer from Hugging Face model
hf_answer = qa_pipeline(question=user_question, context=document_text)['answer']
st.write("Answer from Hugging Face Model:", hf_answer)
# Then try to get the answer from Groq
groq_answer = groq_inference(user_question, document_text)
st.write("Answer from Groq:", groq_answer)