|
import os |
|
import streamlit as st |
|
from langchain import PromptTemplate, LLMChain |
|
from langchain_together import Together |
|
import pdfplumber |
|
|
|
|
|
os.environ['TOGETHER_API_KEY'] = "d88cb7414e4039a84d2ed63f1b47daaaa4230c4c53a422045d8a30a9a3bc87d8" |
|
|
|
def extract_text_from_pdf(pdf_file, max_pages=16): |
|
text = "" |
|
with pdfplumber.open(pdf_file) as pdf: |
|
for i, page in enumerate(pdf.pages): |
|
if i >= max_pages: |
|
break |
|
text += page.extract_text() + "\n" |
|
return text |
|
|
|
def Bot(text, question): |
|
chat_template = """ |
|
Based on the provided context: {text} |
|
Please answer the following question: {Questions} |
|
Only provide answers that are directly related to the context. If the question is unrelated, respond with "I don't know". |
|
""" |
|
prompt = PromptTemplate( |
|
input_variables=['text', 'Questions'], |
|
template=chat_template |
|
) |
|
llama3 = Together(model="meta-llama/Llama-3-70b-chat-hf", max_tokens=50) |
|
Generated_chat = LLMChain(llm=llama3, prompt=prompt) |
|
|
|
try: |
|
response = Generated_chat.invoke({ |
|
"text": text, |
|
"Questions": question |
|
}) |
|
|
|
response_text = response['text'] |
|
|
|
response_text = response_text.replace("assistant", "") |
|
|
|
|
|
words = response_text.split() |
|
seen = set() |
|
filtered_words = [word for word in words if word.lower() not in seen and not seen.add(word.lower())] |
|
response_text = ' '.join(filtered_words) |
|
response_text = response_text.strip() |
|
if not response_text.endswith('.'): |
|
response_text += '.' |
|
|
|
return response_text |
|
except Exception as e: |
|
return f"Error in generating response: {e}" |
|
|
|
def ChatBot(document, question): |
|
greetings = ["hi", "hello", "hey", "greetings", "what's up", "howdy"] |
|
question_lower = question.lower().strip() |
|
if question_lower in greetings or any(question_lower.startswith(greeting) for greeting in greetings): |
|
return "Hello! How can I assist you with the document today?" |
|
|
|
text = extract_text_from_pdf(document) |
|
response = Bot(text, question) |
|
return response |
|
|
|
st.title("PDF ChatBot") |
|
|
|
uploaded_file = st.file_uploader("Upload PDF Document", type="pdf") |
|
question = st.text_input("Ask a Question", placeholder="Type your question here...") |
|
|
|
if uploaded_file and question: |
|
with st.spinner('Processing...'): |
|
response = ChatBot(uploaded_file, question) |
|
st.write(response) |
|
|
|
|
|
|
|
st.sidebar.image("profile.jpg", width=200) |
|
|
|
st.sidebar.title("Haseeb Ahmed") |
|
st.sidebar.write("AI/ML Engineer") |
|
st.sidebar.markdown("[Visit us at](https://www.linkedin.com/in/muhammad-haseeb-ahmed-1954b5230/)") |