File size: 3,817 Bytes
f9d300b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import streamlit as st
from base64 import b64encode
import os
from dotenv import load_dotenv
from io import BytesIO
from PyPDF2 import PdfReader
from langchain_community.document_loaders import PyPDFLoader, UnstructuredPDFLoader, OnlinePDFLoader
from tempfile import NamedTemporaryFile

import google.generativeai as genai

load_dotenv() ## load all the environemnt variables
## Configure Genai Key

genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))

def prepare_prompt(question, context):
    if context is None:
        return "Please upload a PDF first."
    
    prompt= f"""
    You are an expert in analyzing the context and providing accurate and comprehensive answers based on the context. 
    Use the context prvided below and answer comprehensively to the question at the end.
    
    Context: {context}
    
    Question:{question}

    """

    return prompt



def get_gemini_response(prompt):
    print(prompt)
    
    model=genai.GenerativeModel('gemini-pro')
    response=model.generate_content(prompt)
    return response.text

def extract_text(uploaded_file):
    """Extracts text from each page of a PDF using fitz.

    Args:
        pdf_bytes (bytes): The PDF content in bytes format.

    Returns:
        list: A list containing the extracted text from each page.
    """

    pages = []
    if isinstance(uploaded_file, str):  # Handle URL case
        loader = OnlinePDFLoader(uploaded_file)
        print("Fetching Url")
    else:
        pdf_reader = PdfReader(uploaded_file)  # Handle uploaded file case

    pages = []
    for page in pdf_reader.pages:
        pages.append(page.extract_text())
        
    st.session_state["text"] = text 
    

    return pages


st.set_page_config(page_title="Waiwoph  App", layout="wide")

# Beautiful interface elements
st.title("Talk to your files")
st.write("Upload a PDF document and enter your questions.")

uploaded_file = st.file_uploader("Choose a PDF file:", type="pdf")
text = None  # Initialize text to store extracted content
convo=""
if st.session_state.get("convo") is not None:
    convo=st.session_state.get("convo")
if uploaded_file is not None:
    text = extract_text(uploaded_file)
    st.success("PDF uploaded successfully!")

if text is not None:
    questions = st.text_input("Ask Your Questions:")
    answer_button = st.button("Ask", key="find_answers_button")  # Initially enabled

    if answer_button:
        is_processing = False  # Flag to track processing state
        for question in questions.splitlines():
            if not is_processing:
                #answer_button.disabled = True  # Disable button before processing
                is_processing = True
            with st.spinner("Processing..."):
                prompt=prepare_prompt(question.strip(),text)
                response=get_gemini_response(prompt)
                convo=f'''{convo}  \n\n**User:** {question}  \n**Waiwoph:** {response}'''
                st.write(convo)
                st.session_state["convo"] = convo 
            # Clear question text area after each response
            questions = ""
            #st.text_input("Ask Your Questions:", value=questions)  # Clear questions
        is_processing = False  # Reset processing flag
        #answer_button.disabled = False  # Re-enable button after all processed



    if answer_button:
        for question in questions.splitlines():  # Split questions at line breaks
            prompt=prepare_prompt(question.strip(),text)
            response=get_gemini_response(prompt)
            convo=f'''{convo}  \n**User:** {question}  \n**Waiwoph:** {response}'''
            st.write(convo)
            st.session_state["convo"] = convo 
            print(convo)
            #st.write("**Answer:**",response)
            #st.text_area("Search Result", response, height=500)