File size: 5,139 Bytes
bee2b96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
276c077
bee2b96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import fitz
import textwrap
from dotenv import load_dotenv
from langchain_community.document_loaders import UnstructuredPDFLoader
from langchain_community.vectorstores import FAISS
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_text_splitters import CharacterTextSplitter
from langchain_groq import ChatGroq
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain
from pdf2image import convert_from_path
import pytesseract
from gtts import gTTS
import uuid
import gradio as gr

# Load environment variables
load_dotenv()
os.environ["GROQ_API_KEY"] = "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"

# File directories
UPLOAD_FOLDER = 'uploads/'
AUDIO_FOLDER = 'static/audio/'

# Ensure directories exist
for folder in [UPLOAD_FOLDER, AUDIO_FOLDER]:
    if not os.path.exists(folder):
        os.makedirs(folder)

def load_pdf(file_path):
    """
    Load and preprocess Arabic text from a PDF file.
    """
    pages = convert_from_path(file_path, 500)
    documents = []
    for imgBlob in pages:
        # Perform OCR on each image
        text = pytesseract.image_to_string(imgBlob, lang="ara")
        documents.append(text)
    return documents

def prepare_vectorstore(data):
    embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/paraphrase-multilingual-mpnet-base-v2")
    text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=20, separator="\n")
    texts = data
    vectorstore = FAISS.from_texts(texts, embeddings)

    # Save FAISS index to disk
    vectorstore.save_local("faiss_index")

    return vectorstore

def load_vectorstore():
    embeddings = HuggingFaceEmbeddings()
    vectorstore = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
    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):
    if pdf_file is not None:
        file_path = os.path.join(UPLOAD_FOLDER, pdf_file.name)
        pdf_file.save(file_path)

        # Load PDF, prepare vectorstore
        data = load_pdf(file_path)
        vectorstore = prepare_vectorstore(data)
        chain = create_chain(vectorstore)
        
        return chain, f"تم تحميل الملف '{pdf_file.name}' بنجاح!"
    return None, "الرجاء تحميل ملف PDF ."

def chat_with_bot(user_input, chain):
    if chain is None:
        return "يرجى تحميل ملف PDF أولاً."

    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.\n

        Question: {user_input}\n
        Helpful Answer:"""
    
    response = chain({"question": prompt})
    assistant_response = response["answer"]
    
    # Generate and save audio response
    audio_id = str(uuid.uuid4())
    audio_file = f"{audio_id}.mp3"
    tts = gTTS(text=assistant_response, lang='ar')
    tts.save(os.path.join(AUDIO_FOLDER, audio_file))

    return assistant_response, f"{AUDIO_FOLDER}/{audio_file}"

# Gradio app interface
def chatbot_interface(pdf_file, user_input):
    chain, message = process_pdf(pdf_file)
    
    if user_input and chain:
        response_text, audio_path = chat_with_bot(user_input, chain)
        return response_text, audio_path
    else:
        return "يرجى إدخال السؤال.", None

with gr.Blocks() as demo:
    gr.Markdown("<h1 style='text-align:center;'>ديمو بوت للقاء مركز حضرموت</h1>")
    
    with gr.Row():
        pdf_input = gr.File(label="اختر ملف 📑 PDF للدردشة", type="file")
    
    with gr.Row():
        user_input = gr.Textbox(label="سؤالك")
    
    with gr.Row():
        submit_button = gr.Button("رفع وبدء الدردشة")
    
    with gr.Row():
        output_text = gr.Textbox(label="الرد")
        audio_output = gr.Audio(label="الرد الصوتي")
    
    submit_button.click(chatbot_interface, inputs=[pdf_input, user_input], outputs=[output_text, audio_output])

demo.launch()