File size: 7,983 Bytes
bf18e69
bee2b96
631b794
bee2b96
 
 
 
7b54e65
bee2b96
 
 
 
 
bf18e69
4132a28
 
3759483
c2777d8
 
 
bf18e69
 
bee2b96
 
2cc1efc
3759483
2cc1efc
e70a2d0
345a26b
7b54e65
d187736
7b54e65
bf18e69
bee2b96
bf18e69
bee2b96
 
 
 
d800d23
bee2b96
bf18e69
d800d23
 
 
 
 
 
bee2b96
bf18e69
d800d23
 
 
 
 
 
c2777d8
d800d23
c2777d8
bee2b96
 
d800d23
bee2b96
1511464
bee2b96
 
 
 
 
 
 
 
 
 
 
 
 
 
d800d23
bee2b96
d800d23
bf18e69
d800d23
 
 
 
 
 
 
 
 
bf18e69
 
d800d23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf18e69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d800d23
bf18e69
 
30b68de
bf18e69
bee2b96
55a1f5b
bee2b96
bf18e69
 
bee2b96
55a1f5b
 
 
 
 
 
 
 
 
 
 
 
bee2b96
55a1f5b
03ed832
55a1f5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bee2b96
e70a2d0
39c89fc
55a1f5b
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import gradio as gr
import os
import subprocess
import fitz
from dotenv import load_dotenv
from langchain_community.document_loaders import UnstructuredPDFLoader
from langchain_community.vectorstores import FAISS
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_text_splitters import CharacterTextSplitter
from langchain_groq import ChatGroq
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain
from gtts import gTTS
import sys
import pytesseract
from pdf2image import convert_from_path
from huggingface_hub import Repository, login
from huggingface_hub import hf_hub_download




# Load environment variables
load_dotenv()
secret_key = os.getenv("GROQ_API_KEY")
hf_key = os.getenv("HF_TOKEN")

os.environ["GROQ_API_KEY"] = secret_key
login(token=hf_key,add_to_git_credential=True)

embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/paraphrase-multilingual-mpnet-base-v2")

# Ensure the necessary folders exist
UPLOAD_FOLDER = 'uploads/'
AUDIO_FOLDER = 'audio/'
for folder in [UPLOAD_FOLDER, AUDIO_FOLDER]:
    if not os.path.exists(folder):
        os.makedirs(folder)

vectorstore=None
def load_pdf(file_path):
    """Load and preprocess Arabic text from a PDF file."""
    try:
        pages = convert_from_path(file_path, 500)
    except Exception as e:
        print(f"Error loading PDF: {e}")
        return []

    documents = []
    for pageNum, imgBlob in enumerate(pages):
        try:
            text = pytesseract.image_to_string(imgBlob, lang="ara")
            documents.append(text)
        except Exception as e:
            print(f"Error processing page {pageNum}: {e}")
            documents.append("")  # Append empty string for pages where OCR failed

    return documents

def prepare_vectorstore(data):
    text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=20, separator="\n")
    texts = text_splitter.split_documents(data)
    vectorstore = FAISS.from_texts(texts, embeddings)
    
    return vectorstore

def create_chain(vectorstore):
    llm = ChatGroq(model="gemma2-9b-it", temperature=0)
    retriever = vectorstore.as_retriever()
    memory = ConversationBufferMemory(llm=llm, output_key="answer", memory_key="chat_history", return_messages=True)
    chain = ConversationalRetrievalChain.from_llm(
        llm=llm,
        retriever=retriever,
        memory=memory,
        verbose=False,
        chain_type="map_reduce"
    )
    return chain
    
def process_pdf(pdf_file):
    global vectorstore
    file_path = os.path.join(UPLOAD_FOLDER, pdf_file.name)
    try:
        with open(file_path, "wb") as f:
            f.write(pdf_file.read())
        data = load_pdf(file_path)
        vectorstore = prepare_vectorstore(data)
        return "PDF processed successfully. You can now start chatting!"
    except Exception as e:
        print(f"Error processing PDF: {e}")
        return "Error processing PDF."

def chat(user_input, history):
    if vectorstore is None:
        return "Please process a PDF file first.", ""

    try:
        chain = create_chain(vectorstore)
        prompt = f"""
        You are an expert Arabic-language assistant specialized in analyzing and responding to queries about Arabic PDF documents. Your responses should be precise, informative, and reflect the professional tone and structure expected in formal Arabic communication. Focus on extracting and presenting relevant information from the document clearly and systematically, while avoiding colloquial or informal language.

        When responding, ensure the following:
           - Your answer directly reflects the content of the document.
           - If the requested information is not available in the document, clearly state that.
           - Keep your response concise yet comprehensive, addressing the question fully.
           - Always respond in formal Arabic, without using English.

        Question: {user_input}
        Helpful Answer:"""

        response = chain({"question": prompt})
        assistant_response = response["answer"]

        # Generate audio file
        audio_file = f"response_{len(history)}.mp3"
        try:
            tts = gTTS(text=assistant_response, lang='ar')
            tts.save(os.path.join(AUDIO_FOLDER, audio_file))
        except Exception as e:
            print(f"Error generating audio file: {e}")
            audio_file = ""  # Fallback if audio generation fails

        return assistant_response, audio_file

    except Exception as e:
        print(f"Error during chat: {e}")
        return "An error occurred while processing your request.", ""
        
custom_css = """
body {
    font-family: 'Noto Kufi Arabic', sans-serif;
    background: linear-gradient(135deg, #799351 0%, #A67B5B 100%);
    background-size: cover;
    background-position: center;
    background-attachment: fixed;
}

.gradio-container {
    max-width: 800px !important;
    margin: auto !important;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 20px;
    box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
    backdrop-filter: blur(4px);
    border: 1px solid rgba(255, 255, 255, 0.18);
    padding: 20px;
}

h1, h2, h3 {
    color: #1A4D2E;
    font-weight: bold;
    text-align: center;
}

p {
    color: #A89F91;
}

.gradio-button {
    background-color: #5F6F65 !important;
    color: #FFFFFF !important;
}

.gradio-button:hover {
    background-color: #FFFFFF !important;
    color: #5F6F65 !important;
}

.chat-message {
    border-radius: 10px;
    padding: 10px;
    margin-bottom: 10px;
}

.chat-message.user {
    background-color: #E7F0DC;
}

.chat-message.bot {
    background-color: #F7EED3;
}

.chat-message::before {
    content: '';
    display: inline-block;
    width: 24px;
    height: 24px;
    background-size: contain;
    background-repeat: no-repeat;
    margin-right: 10px;
    vertical-align: middle;
}

.chat-message.user::before {
    content: '👤';
}

.chat-message.bot::before {
    content: '🤖';
}
"""

# Gradio interface
with gr.Blocks(css=custom_css) as demo:
    gr.Markdown("# ديمو بوت للقاء مركز حضرموت للدراسات التاريخية")
    gr.Markdown("## المنعقد السبت 14 - سبتمبر 2024")
    
    # File input and process button
    with gr.Row():
        pdf_input = gr.File(label="اختر ملف PDF للدردشة")
        process_button = gr.Button("رفع وبدء الدردشة")
    
    # Chat interface and audio output
    chat_interface = gr.ChatInterface(
        chat,
        title="الدردشة مع البوت",
        description="اسأل أي سؤال عن محتوى الملف PDF",
        theme="soft",
        examples=["ما هو موضوع الوثيقة؟", "من هم الأشخاص المذكورون؟", "ما هي التواريخ الرئيسية المذكورة؟"],
        cache_examples=True,
        retry_btn=None,
        undo_btn="مسح آخر رسالة",
        clear_btn="مسح المحادثة",
    )
    
    audio_output = gr.Audio(label="الرد الصوتي")
    
    # State to store the vectorstore
    vectorstore_state = gr.State()

    # Ensure chat interface is disabled until PDF is processed
    process_button.click(
        fn=process_pdf, 
        inputs=[pdf_input], 
        outputs=[chat_interface.textbox, vectorstore_state]  # Store the vectorstore in the state
    )

    # Enable chat only after PDF is processed and vectorstore is ready
    def handle_chat(user_input, history, vectorstore):
        if vectorstore is None:
            return "Please upload and process a PDF first.", ""
        return chat(user_input, history, vectorstore)

    # Use the state to pass the vectorstore to the chat
    chat_interface.submit(
        fn=handle_chat,
        inputs=[chat_interface.textbox, chat_interface.chatbot, vectorstore_state],  # Pass the vectorstore as input
        outputs=[audio_output]
    )

demo.launch()