Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,55 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 4 |
-
from
|
|
|
|
| 5 |
from langchain.text_splitter import CharacterTextSplitter
|
| 6 |
-
from langchain.document_loaders import TextLoader
|
| 7 |
from langchain.chains import RetrievalQA
|
| 8 |
from langchain_huggingface import HuggingFaceEndpoint
|
| 9 |
|
| 10 |
-
#
|
| 11 |
loader = TextLoader("knowledge.txt", encoding="utf-8")
|
| 12 |
docs = loader.load()
|
| 13 |
|
| 14 |
-
#
|
| 15 |
text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 16 |
documents = text_splitter.split_documents(docs)
|
| 17 |
|
| 18 |
-
#
|
| 19 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
|
| 20 |
|
| 21 |
-
#
|
| 22 |
vectorstore = FAISS.from_documents(documents, embeddings)
|
| 23 |
|
| 24 |
-
#
|
| 25 |
token = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
| 26 |
|
|
|
|
| 27 |
llm = HuggingFaceEndpoint(
|
| 28 |
repo_id="tiiuae/falcon-7b-instruct",
|
| 29 |
huggingfacehub_api_token=token,
|
| 30 |
-
|
|
|
|
| 31 |
)
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
qa = RetrievalQA.from_chain_type(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
#
|
| 37 |
def answer_question_arabic(question):
|
| 38 |
return qa.run(question)
|
| 39 |
|
| 40 |
-
#
|
| 41 |
iface = gr.Interface(
|
| 42 |
fn=answer_question_arabic,
|
| 43 |
inputs=gr.Textbox(lines=2, placeholder="اكتب سؤالك هنا", label="سؤال"),
|
| 44 |
outputs=gr.Textbox(label="الرد"),
|
| 45 |
title="المساعد الذكي للقطاع الوزاري",
|
| 46 |
-
description="اكتب أي سؤال متعلق بالخدمات أو الإجراءات داخل القطاع وسن
|
| 47 |
)
|
| 48 |
|
| 49 |
-
# === Launch the app ===
|
| 50 |
iface.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 4 |
+
from langchain_community.vectorstores import FAISS
|
| 5 |
+
from langchain_community.document_loaders import TextLoader
|
| 6 |
from langchain.text_splitter import CharacterTextSplitter
|
|
|
|
| 7 |
from langchain.chains import RetrievalQA
|
| 8 |
from langchain_huggingface import HuggingFaceEndpoint
|
| 9 |
|
| 10 |
+
# Load knowledge from Arabic text file
|
| 11 |
loader = TextLoader("knowledge.txt", encoding="utf-8")
|
| 12 |
docs = loader.load()
|
| 13 |
|
| 14 |
+
# Split documents into chunks
|
| 15 |
text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 16 |
documents = text_splitter.split_documents(docs)
|
| 17 |
|
| 18 |
+
# Arabic-capable multilingual sentence embeddings
|
| 19 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
|
| 20 |
|
| 21 |
+
# Create FAISS vector store
|
| 22 |
vectorstore = FAISS.from_documents(documents, embeddings)
|
| 23 |
|
| 24 |
+
# Get token from secret
|
| 25 |
token = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
| 26 |
|
| 27 |
+
# Correct way: pass temperature and max_new_tokens explicitly
|
| 28 |
llm = HuggingFaceEndpoint(
|
| 29 |
repo_id="tiiuae/falcon-7b-instruct",
|
| 30 |
huggingfacehub_api_token=token,
|
| 31 |
+
temperature=0.3,
|
| 32 |
+
max_new_tokens=256
|
| 33 |
)
|
| 34 |
|
| 35 |
+
# Create the RetrievalQA chain
|
| 36 |
+
qa = RetrievalQA.from_chain_type(
|
| 37 |
+
llm=llm,
|
| 38 |
+
chain_type="stuff",
|
| 39 |
+
retriever=vectorstore.as_retriever()
|
| 40 |
+
)
|
| 41 |
|
| 42 |
+
# Arabic chatbot function
|
| 43 |
def answer_question_arabic(question):
|
| 44 |
return qa.run(question)
|
| 45 |
|
| 46 |
+
# Gradio interface
|
| 47 |
iface = gr.Interface(
|
| 48 |
fn=answer_question_arabic,
|
| 49 |
inputs=gr.Textbox(lines=2, placeholder="اكتب سؤالك هنا", label="سؤال"),
|
| 50 |
outputs=gr.Textbox(label="الرد"),
|
| 51 |
title="المساعد الذكي للقطاع الوزاري",
|
| 52 |
+
description="اكتب أي سؤال متعلق بالخدمات أو الإجراءات داخل القطاع، وسنقدم لك الرد بناءً على قاعدة المعرفة."
|
| 53 |
)
|
| 54 |
|
|
|
|
| 55 |
iface.launch()
|