File size: 2,184 Bytes
f39f2e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
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
41
42
43
44
45
46
47
48
49
50
import streamlit as st
from langchain_community.chat_models import ChatOpenAI
from langchain_community.callbacks import get_openai_callback
from langchain.chains.question_answering import load_qa_chain

from utils.process_data import process_text, pdf_to_text

MODEL = st.secrets["MODEL4"]

st.set_page_config(page_title="Summarizer with LLM QA", page_icon="βš–οΈ")
st.title("Summarize Text")
st.subheader("πŸš— πŸ”— LLM/Question Answering")

maxw = st.slider('MAX words', 50, 1000, step=10, value=200)
minw = st.slider('MIN words', 10, 500, step=10, value=50)

sentence = st.text_area('Please paste your article:', height=50)
button = st.button("Summarize")
query = f"Summarize the content of the uploaded PDF file in more that {minw} words and less than {maxw} words. Focus on capturing the main ideas and key points discussed in the document. Use your own words and ensure clarity and coherence in the summary."

with st.spinner("Generating Summary.."):
    if button and sentence:
        knowledgeBase = process_text(sentence)
        docs = knowledgeBase.similarity_search(query)
        llm = ChatOpenAI(model=MODEL, temperature=0.1, openai_api_key=st.secrets["OPENAI_API_KEY"])
        chain = load_qa_chain(llm, chain_type='stuff')
        with get_openai_callback() as cost:
            response = chain.run(input_documents=docs, question=query)
            print(cost)
        st.subheader('Summary Results:')
        st.write(response)

st.divider()

st.subheader('πŸš™πŸ”— Summarize PDF')
pdf_path = st.file_uploader('Upload your PDF Document', type='pdf')
button2 = st.button("Summarize PDF")

if pdf_path is not None and button2:
    text = pdf_to_text(pdf_path)
    knowledgeBase = process_text(text)
    with st.spinner("Generating PDF Summary.."):
        docs = knowledgeBase.similarity_search(query)
        llm = ChatOpenAI(model=MODEL, temperature=0.1, openai_api_key=st.secrets["OPENAI_API_KEY"])
        chain = load_qa_chain(llm, chain_type='stuff')
        with get_openai_callback() as cost:
            response2 = chain.run(input_documents=docs, question=query)
            print(cost)
        st.subheader('Summary Results:')
        st.write(response2)