MohammedNasser commited on
Commit
bee2b96
·
verified ·
1 Parent(s): a725b51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +140 -62
app.py CHANGED
@@ -1,63 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
- )
60
-
61
-
62
- if __name__ == "__main__":
63
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import fitz
3
+ import textwrap
4
+ from dotenv import load_dotenv
5
+ from langchain_community.document_loaders import UnstructuredPDFLoader
6
+ from langchain_community.vectorstores import FAISS
7
+ from langchain_community.embeddings import HuggingFaceEmbeddings
8
+ from langchain_text_splitters import CharacterTextSplitter
9
+ from langchain_groq import ChatGroq
10
+ from langchain.memory import ConversationBufferMemory
11
+ from langchain.chains import ConversationalRetrievalChain
12
+ from pdf2image import convert_from_path
13
+ import pytesseract
14
+ from gtts import gTTS
15
+ import uuid
16
  import gradio as gr
17
+
18
+ # Load environment variables
19
+ load_dotenv()
20
+ os.environ["GROQ_API_KEY"] = "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"
21
+
22
+ # File directories
23
+ UPLOAD_FOLDER = 'uploads/'
24
+ AUDIO_FOLDER = 'static/audio/'
25
+
26
+ # Ensure directories exist
27
+ for folder in [UPLOAD_FOLDER, AUDIO_FOLDER]:
28
+ if not os.path.exists(folder):
29
+ os.makedirs(folder)
30
+
31
+ def load_pdf(file_path):
32
+ """
33
+ Load and preprocess Arabic text from a PDF file.
34
+ """
35
+ pages = convert_from_path(file_path, 500)
36
+ documents = []
37
+ for imgBlob in pages:
38
+ # Perform OCR on each image
39
+ text = pytesseract.image_to_string(imgBlob, lang="ara")
40
+ documents.append(text)
41
+ return documents
42
+
43
+ def prepare_vectorstore(data):
44
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/paraphrase-multilingual-mpnet-base-v2")
45
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=20, separator="\n")
46
+ texts = data
47
+ vectorstore = FAISS.from_texts(texts, embeddings)
48
+
49
+ # Save FAISS index to disk
50
+ vectorstore.save_local("faiss_index")
51
+
52
+ return vectorstore
53
+
54
+ def load_vectorstore():
55
+ embeddings = HuggingFaceEmbeddings()
56
+ vectorstore = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
57
+ return vectorstore
58
+
59
+ def create_chain(vectorstore):
60
+ llm = ChatGroq(model="gemma2-9b-it", temperature=0)
61
+ retriever = vectorstore.as_retriever()
62
+ memory = ConversationBufferMemory(llm=llm, output_key="answer", memory_key="chat_history", return_messages=True)
63
+ chain = ConversationalRetrievalChain.from_llm(
64
+ llm=llm,
65
+ retriever=retriever,
66
+ memory=memory,
67
+ verbose=False,
68
+ chain_type="map_reduce"
69
+ )
70
+ return chain
71
+
72
+ def process_pdf(pdf_file):
73
+ if pdf_file is not None:
74
+ file_path = os.path.join(UPLOAD_FOLDER, pdf_file.name)
75
+ pdf_file.save(file_path)
76
+
77
+ # Load PDF, prepare vectorstore
78
+ data = load_pdf(file_path)
79
+ vectorstore = prepare_vectorstore(data)
80
+ chain = create_chain(vectorstore)
81
+
82
+ return chain, f"تم تحميل الملف '{pdf_file.name}' بنجاح!"
83
+ return None, "الرجاء تحميل ملف PDF ."
84
+
85
+ def chat_with_bot(user_input, chain):
86
+ if chain is None:
87
+ return "يرجى تحميل ملف PDF أولاً."
88
+
89
+ prompt=f"""
90
+ 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.
91
+
92
+ When responding, ensure the following:
93
+
94
+ - Your answer directly reflects the content of the document.
95
+ - If the requested information is not available in the document, clearly state that.
96
+ - Keep your response concise yet comprehensive, addressing the question fully.
97
+ - Always respond in formal Arabic, without using English.\n
98
+
99
+ Question: {user_input}\n
100
+ Helpful Answer:"""
101
+
102
+ response = chain({"question": prompt})
103
+ assistant_response = response["answer"]
104
+
105
+ # Generate and save audio response
106
+ audio_id = str(uuid.uuid4())
107
+ audio_file = f"{audio_id}.mp3"
108
+ tts = gTTS(text=assistant_response, lang='ar')
109
+ tts.save(os.path.join(AUDIO_FOLDER, audio_file))
110
+
111
+ return assistant_response, f"{AUDIO_FOLDER}/{audio_file}"
112
+
113
+ # Gradio app interface
114
+ def chatbot_interface(pdf_file, user_input):
115
+ chain, message = process_pdf(pdf_file)
116
+
117
+ if user_input and chain:
118
+ response_text, audio_path = chat_with_bot(user_input, chain)
119
+ return response_text, audio_path
120
+ else:
121
+ return "يرجى إدخال السؤال.", None
122
+
123
+ with gr.Blocks() as demo:
124
+ gr.Markdown("<h1 style='text-align:center;'>ديمو بوت للقاء مركز حضرموت</h1>")
125
+
126
+ with gr.Row():
127
+ pdf_input = gr.File(label="اختر ملف 📑 PDF للدردشة", type="file")
128
+
129
+ with gr.Row():
130
+ user_input = gr.Textbox(label="سؤالك")
131
+
132
+ with gr.Row():
133
+ submit_button = gr.Button("رفع وبدء الدردشة")
134
+
135
+ with gr.Row():
136
+ output_text = gr.Textbox(label="الرد")
137
+ audio_output = gr.Audio(label="الرد الصوتي")
138
+
139
+ submit_button.click(chatbot_interface, inputs=[pdf_input, user_input], outputs=[output_text, audio_output])
140
+
141
+ demo.launch()