Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,30 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
-
from langchain_community.document_loaders import PyPDFLoader
|
| 4 |
-
from langchain.text_splitter import CharacterTextSplitter
|
| 5 |
-
from langchain_community.vectorstores import FAISS
|
| 6 |
-
from langchain.embeddings import HuggingFaceEmbeddings
|
| 7 |
-
from langchain.chains import RetrievalQA
|
| 8 |
-
from langchain_groq import ChatGroq # NEW import
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
st.set_page_config(page_title="Groq PDF Chatbot")
|
| 14 |
-
st.title("📄 Chat with your PDF using Groq + LLaMA3")
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
# Load and split the PDF
|
| 25 |
-
# -------------------------------
|
| 26 |
-
pdf_path = "docs/acca.pdf" # Make sure this file is in your Space
|
| 27 |
-
loader = PyPDFLoader(pdf_path)
|
| 28 |
-
pages = loader.load()
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
# -------------------------------
|
| 36 |
-
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 37 |
-
vectorstore = FAISS.from_documents(docs, embeddings)
|
| 38 |
-
|
| 39 |
-
# -------------------------------
|
| 40 |
-
# Groq LLM setup
|
| 41 |
-
# -------------------------------
|
| 42 |
-
llm = ChatGroq(
|
| 43 |
-
temperature=0.7,
|
| 44 |
-
model_name="llama3-8b-8192", # Make sure to use correct lowercase name
|
| 45 |
-
groq_api_key=api_key
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
qa_chain = RetrievalQA.from_chain_type(
|
| 49 |
-
llm=llm,
|
| 50 |
-
retriever=vectorstore.as_retriever(),
|
| 51 |
-
return_source_documents=True
|
| 52 |
-
)
|
| 53 |
-
|
| 54 |
-
# -------------------------------
|
| 55 |
-
# User input and response
|
| 56 |
-
# -------------------------------
|
| 57 |
-
query = st.text_input("Ask a question based on the PDF:")
|
| 58 |
-
if query:
|
| 59 |
-
with st.spinner("Generating answer..."):
|
| 60 |
-
result = qa_chain.invoke(query)
|
| 61 |
-
st.subheader("📌 Answer")
|
| 62 |
-
st.write(result["result"])
|
|
|
|
| 1 |
+
import spacy
|
| 2 |
+
import pytextrank
|
| 3 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Load NLP model and add TextRank once
|
| 6 |
+
nlp = spacy.load("en_core_web_lg")
|
| 7 |
+
nlp.add_pipe("textrank")
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
def summarize_text(input_text):
|
| 10 |
+
doc = nlp(input_text)
|
| 11 |
+
summary = "\n".join([f"• {sent.text}" for sent in doc._.textrank.summary(limit_phrases=2, limit_sentences=2)])
|
| 12 |
+
return summary
|
| 13 |
|
| 14 |
+
def main():
|
| 15 |
+
st.title("TextRank Text Summarizer")
|
| 16 |
+
st.write("This app generates a concise summary from your input text using TextRank.")
|
| 17 |
|
| 18 |
+
input_text = st.text_area("Enter the text you want to summarize:", height=300)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
if st.button("Summarize"):
|
| 21 |
+
if input_text.strip():
|
| 22 |
+
with st.spinner("Generating summary..."):
|
| 23 |
+
summary = summarize_text(input_text)
|
| 24 |
+
st.subheader("Summary:")
|
| 25 |
+
st.write(summary)
|
| 26 |
+
else:
|
| 27 |
+
st.warning("Please enter some text to summarize.")
|
| 28 |
|
| 29 |
+
if __name__ == "__main__":
|
| 30 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|