Update app.py
Browse files
app.py
CHANGED
|
@@ -1,77 +1,57 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import streamlit as st
|
| 3 |
-
import
|
| 4 |
-
from
|
| 5 |
-
from
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
)
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
temperature=0.7,
|
| 41 |
-
max_tokens=1500
|
| 42 |
)
|
| 43 |
-
return response.choices[0].message.content
|
| 44 |
-
|
| 45 |
-
def main():
|
| 46 |
-
st.title("📝 Pro SEO Blog Writer")
|
| 47 |
-
|
| 48 |
-
st.subheader("Enter three blog URLs:")
|
| 49 |
-
url1 = st.text_input("Blog URL 1")
|
| 50 |
-
url2 = st.text_input("Blog URL 2")
|
| 51 |
-
url3 = st.text_input("Blog URL 3")
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
content1 = extract_text_from_url(url1)
|
| 60 |
-
content2 = extract_text_from_url(url2)
|
| 61 |
-
content3 = extract_text_from_url(url3)
|
| 62 |
-
|
| 63 |
-
combined_content = content1 + "\n\n" + content2 + "\n\n" + content3
|
| 64 |
-
keywords = [kw.strip() for kw in keywords_input.split(",")]
|
| 65 |
-
|
| 66 |
-
new_blog = generate_blog(combined_content, keywords)
|
| 67 |
-
|
| 68 |
-
st.success("✅ Blog generated successfully!")
|
| 69 |
-
st.subheader("Generated Blog:")
|
| 70 |
-
st.write(new_blog)
|
| 71 |
-
|
| 72 |
-
st.download_button("Download Blog as TXT", data=new_blog, file_name="seo_blog.txt")
|
| 73 |
-
else:
|
| 74 |
-
st.warning("Please fill in all fields.")
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 3 |
+
from langchain_community.vectorstores import FAISS
|
| 4 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 5 |
+
from langchain.chains import RetrievalQA
|
| 6 |
+
from langchain_community.llms import Groq
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Set up page
|
| 10 |
+
st.set_page_config(page_title="Chat with PDF")
|
| 11 |
+
|
| 12 |
+
# Sidebar: enter Groq API key
|
| 13 |
+
st.sidebar.title("Configuration")
|
| 14 |
+
groq_api_key = st.sidebar.text_input("Enter your Groq API Key", type="password")
|
| 15 |
+
|
| 16 |
+
# Main heading
|
| 17 |
+
st.title("📄 PDF Chatbot using LangChain + FAISS + Groq")
|
| 18 |
+
st.markdown("This app answers your questions based on a predefined PDF.")
|
| 19 |
+
|
| 20 |
+
# Path to your preloaded PDF
|
| 21 |
+
PDF_PATH = "src/acca.pdf"
|
| 22 |
+
|
| 23 |
+
# Process PDF and create retriever (cached)
|
| 24 |
+
@st.cache_resource
|
| 25 |
+
def load_retriever(pdf_path):
|
| 26 |
+
loader = PyPDFLoader(pdf_path)
|
| 27 |
+
documents = loader.load_and_split()
|
| 28 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 29 |
+
db = FAISS.from_documents(documents, embeddings)
|
| 30 |
+
return db.as_retriever()
|
| 31 |
+
|
| 32 |
+
# Validate API key
|
| 33 |
+
if not groq_api_key:
|
| 34 |
+
st.warning("Please enter your Groq API key.")
|
| 35 |
+
else:
|
| 36 |
+
retriever = load_retriever(PDF_PATH)
|
| 37 |
+
|
| 38 |
+
# Initialize Groq LLM
|
| 39 |
+
llm = Groq(
|
| 40 |
+
model="llama3-8b-8192",
|
| 41 |
+
api_key=groq_api_key,
|
| 42 |
temperature=0.7,
|
|
|
|
| 43 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# QA chain
|
| 46 |
+
qa_chain = RetrievalQA.from_chain_type(
|
| 47 |
+
llm=llm,
|
| 48 |
+
chain_type="stuff",
|
| 49 |
+
retriever=retriever
|
| 50 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
# Input field for questions
|
| 53 |
+
user_input = st.text_input("Ask a question about the PDF:")
|
| 54 |
+
if user_input:
|
| 55 |
+
with st.spinner("Generating response..."):
|
| 56 |
+
answer = qa_chain.run(user_input)
|
| 57 |
+
st.success(answer)
|