Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import faiss
|
4 |
+
import numpy as np
|
5 |
+
import pickle
|
6 |
+
from sentence_transformers import SentenceTransformer
|
7 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
8 |
+
|
9 |
+
# Load precomputed chunks and FAISS index
|
10 |
+
print("Loading precomputed data...")
|
11 |
+
with open("chunks.pkl", "rb") as f:
|
12 |
+
chunks = pickle.load(f)
|
13 |
+
index = faiss.read_index("index.faiss")
|
14 |
+
|
15 |
+
# Load embedding model (for queries only)
|
16 |
+
embedding_model = SentenceTransformer("sentence-transformers/paraphrase-multilingual-mpnet-base-v2")
|
17 |
+
|
18 |
+
# Load Jais model and tokenizer
|
19 |
+
model_name = "inceptionai/jais-13b"
|
20 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
21 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
22 |
+
|
23 |
+
# RAG function
|
24 |
+
def get_response(query, k=3):
|
25 |
+
query_embedding = embedding_model.encode([query])
|
26 |
+
distances, indices = index.search(np.array(query_embedding), k)
|
27 |
+
retrieved_chunks = [chunks[i] for i in indices[0]]
|
28 |
+
context = " ".join(retrieved_chunks)
|
29 |
+
prompt = f"استنادًا إلى الوثائق التالية: {context}، أجب على السؤال: {query}"
|
30 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
|
31 |
+
outputs = model.generate(
|
32 |
+
**inputs,
|
33 |
+
max_new_tokens=200,
|
34 |
+
do_sample=True,
|
35 |
+
temperature=0.7,
|
36 |
+
top_p=0.9
|
37 |
+
)
|
38 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
39 |
+
return response.split(query)[-1].strip()
|
40 |
+
|
41 |
+
# Gradio interface
|
42 |
+
with gr.Blocks(title="Dubai Legislation Chatbot") as demo:
|
43 |
+
gr.Markdown("# Dubai Legislation Chatbot\nاسأل أي سؤال حول تشريعات دبي")
|
44 |
+
chatbot = gr.Chatbot()
|
45 |
+
msg = gr.Textbox(placeholder="اكتب سؤالك هنا...", rtl=True)
|
46 |
+
clear = gr.Button("مسح")
|
47 |
+
|
48 |
+
def user(user_message, history):
|
49 |
+
return "", history + [[user_message, None]]
|
50 |
+
|
51 |
+
def bot(history):
|
52 |
+
user_message = history[-1][0]
|
53 |
+
bot_message = get_response(user_message)
|
54 |
+
history[-1][1] = bot_message
|
55 |
+
return history
|
56 |
+
|
57 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
58 |
+
bot, chatbot, chatbot
|
59 |
+
)
|
60 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
61 |
+
|
62 |
+
demo.launch()
|