File size: 6,651 Bytes
b292afc
 
dde83d9
 
2c2fb6b
8d577a4
720b5a8
b292afc
 
 
8d577a4
b292afc
 
8d577a4
dde83d9
b292afc
 
dde83d9
8d577a4
 
b292afc
dde83d9
b292afc
 
 
 
 
 
dde83d9
f1f85e2
8d577a4
 
b292afc
8d577a4
 
 
 
720b5a8
8d577a4
b292afc
 
 
 
 
 
 
b619688
c721836
8d577a4
b292afc
8d577a4
 
 
 
2dadb13
b292afc
8d577a4
 
3570600
8d577a4
 
b292afc
8d577a4
 
 
 
 
 
 
 
 
b292afc
c721836
8d577a4
b292afc
 
 
8d577a4
 
b292afc
 
 
 
8d577a4
b292afc
8d577a4
b292afc
 
 
 
8d577a4
 
b292afc
8d577a4
 
 
 
2c2fb6b
a3b67f3
b292afc
 
11b7607
b292afc
8d577a4
b292afc
8d577a4
b292afc
 
 
 
 
 
 
 
 
 
 
 
8d577a4
b292afc
8d577a4
b292afc
 
 
 
 
 
 
 
8d577a4
b292afc
 
 
 
8d577a4
b292afc
 
6867b0e
8d577a4
 
 
 
 
6867b0e
 
b292afc
 
 
 
6867b0e
8d577a4
 
 
 
 
6867b0e
 
b292afc
 
2c2fb6b
b292afc
8d577a4
 
 
6867b0e
8d577a4
 
 
 
 
6867b0e
 
b292afc
 
 
 
8d577a4
339553b
a3b67f3
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import os
import fitz  # PyMuPDF
import streamlit as st
import google.generativeai as genai
from dotenv import load_dotenv
from google.api_core.exceptions import GoogleAPIError, InvalidArgument

from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
from langchain_community.vectorstores import FAISS
from langchain.chains.question_answering import load_qa_chain
from langchain.prompts import PromptTemplate

# Load environment variables
load_dotenv()
api_key = os.getenv("GOOGLE_API_KEY")
genai.configure(api_key=api_key)


# ✅ Function to read all PDF files (Farsi + English)
def get_pdf_text(pdf_docs):
    text = ""
    for pdf in pdf_docs:
        with fitz.open(stream=pdf.read(), filetype="pdf") as doc:
            for page in doc:
                page_text = page.get_text("text")
                if page_text:
                    text += page_text + "\n"
    return text


# ✅ Function to split text into chunks
def get_text_chunks(text):
    splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=300)
    chunks = splitter.split_text(text)
    return chunks


# ✅ Function to get embeddings for each chunk and save to vector store
def get_vector_store(chunks):
    try:
        embeddings = GoogleGenerativeAIEmbeddings(model="models/text-embedding-004")
        vector_store = FAISS.from_texts(chunks, embedding=embeddings)
        vector_store.save_local("faiss_index")
    except Exception as e:
        raise RuntimeError(f"Error creating vector store: {e}")


# ✅ Function to get conversational chain
def get_conversational_chain():
    prompt_template = """
    You are a helpful assistant. 
    Answer the question as detailed as possible using the provided context. 
    If the answer is unclear, summarize the most relevant part of the context. 
    Do not make up answers. if the question is asked in Farsi, provide the answer in the same asking language.

    Context:
    {context}

    Question:
    {question}

    Answer:
    """
    try:
        model = ChatGoogleGenerativeAI(model="gemini-1.5-flash", client=genai, temperature=0.3)
        prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
        chain = load_qa_chain(llm=model, chain_type="stuff", prompt=prompt)
        return chain
    except Exception as e:
        raise RuntimeError(f"Error creating conversational chain: {e}")


# ✅ Function to clear chat history
def clear_chat_history():
    st.session_state.messages = [{"role": "assistant", "content": "در خدمتیم"}]


# ✅ Function to handle user input
def user_input(user_question):
    try:
        embeddings = GoogleGenerativeAIEmbeddings(model="models/text-embedding-004")
        new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
        docs = new_db.similarity_search(user_question, k=15)
        chain = get_conversational_chain()
        response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
        return response
    except Exception as e:
        raise RuntimeError(f"Error while answering: {e}")


# ✅ Main function to run the Streamlit app
def main():
    st.set_page_config(
        page_title="Chatbot",
        layout="wide",
        initial_sidebar_state="expanded"
    )

    if "uploaded" not in st.session_state:
        st.session_state.uploaded = False

    if not st.session_state.uploaded:
        # Upload Page
        st.title("Your personal assistant ...")
        pdf_docs = st.file_uploader("فایل پی دی اف مورد نظر را آپلود کنید ", accept_multiple_files=True)
        if st.button("تایید"):
            if pdf_docs:
                try:
                    st.info("در حال پردازش ...")
                    raw_text = get_pdf_text(pdf_docs)
                    text_chunks = get_text_chunks(raw_text)
                    get_vector_store(text_chunks)
                    st.session_state.uploaded = True
                    st.success("پردازش موفق شد ✅")
                except RuntimeError as e:
                    st.error(str(e))
            else:
                st.error("لطفا حداقل یک فایل را آپلود کنید")
    else:
        # Chat Page
        st.title("Assistant ready ...")
        st.write("میتونین سوالتونو بپرسین 👇")

        if st.button("بازگشت به صفحه آپلود"):
            st.session_state.uploaded = False
            clear_chat_history()
            st.rerun()

        st.button('حذف مکالمه', on_click=clear_chat_history)

        if "messages" not in st.session_state:
            st.session_state.messages = [{"role": "assistant", "content": "در خدمتیم"}]

        # Show history
        for message in st.session_state.messages:
            with st.chat_message(message["role"]):
                st.markdown(
                    f"""
                    <div style="direction: rtl; text-align: right; font-size: 16px;">
                        {message["content"]}
                    </div>
                    """,
                    unsafe_allow_html=True
                )

        if prompt := st.chat_input():
            st.session_state.messages.append({"role": "user", "content": prompt})
            with st.chat_message("user"):
                st.markdown(
                    f"""
                    <div style="direction: rtl; text-align: right; font-size: 16px;">
                        {prompt}
                    </div>
                    """,
                    unsafe_allow_html=True
                )

            if st.session_state.messages[-1]["role"] != "assistant":
                try:
                    with st.chat_message("assistant"):
                        response = user_input(prompt)
                        if response:
                            full_response = response['output_text']
                            st.markdown(
                                f"""
                                <div style="direction: rtl; text-align: right; font-size: 16px;">
                                    {full_response}
                                </div>
                                """,
                                unsafe_allow_html=True
                            )
                            st.session_state.messages.append({"role": "assistant", "content": full_response})
                except RuntimeError as e:
                    st.error(str(e))


if __name__ == "__main__":
    main()