ohalkhateeb commited on
Commit
42da822
·
verified ·
1 Parent(s): 0c3bebf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -57
app.py CHANGED
@@ -1,73 +1,57 @@
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
-
10
- HF_TOKEN = os.getenv("HF_TOKEN")
11
- if not HF_TOKEN:
12
- raise ValueError("HF_TOKEN environment variable not set. Please configure it in Space settings.")
13
-
14
-
15
- # Load precomputed chunks and FAISS index
16
- with open("chunks.pkl", "rb") as f:
17
- chunks = pickle.load(f)
18
- index = faiss.read_index("index.faiss")
19
-
20
- # Load embedding model (same as used in preprocessing)
21
- embedding_model = SentenceTransformer("sentence-transformers/paraphrase-multilingual-mpnet-base-v2")
22
-
23
-
24
- # Load Jais model and tokenizer
25
- model_name = "aubmindlab/aragpt2-base"
26
- tokenizer = AutoTokenizer.from_pretrained(model_name, token=HF_TOKEN, trust_remote_code=True)
27
- model = AutoModelForCausalLM.from_pretrained(model_name, token=HF_TOKEN, trust_remote_code=True)
28
-
29
-
30
- # RAG function to retrieve and generate a response
31
- def get_response(query, k=3):
32
- query_embedding = embedding_model.encode([query])
33
- distances, indices = index.search(np.array(query_embedding), k)
34
- retrieved_chunks = [chunks[i] for i in indices[0]]
35
- context = " ".join(retrieved_chunks)
36
- prompt = f"Based on the following documents: {context}, answer the question: {query}"
37
- inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
38
- outputs = model.generate(
39
- **inputs,
40
- max_new_tokens=200,
41
- do_sample=True,
42
- temperature=0.7,
43
- top_p=0.9,
44
- return_full_text=False
45
- )
46
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
47
- return response.split(query)[-1].strip()
48
-
49
- # Gradio interface
50
- import gradio as gr
51
 
52
  with gr.Blocks(title="المتحدث الآلي للتشريعات المحلية لإمارة دبي") as demo:
53
- gr.Markdown("# Dubai Legislation Chatbot\nاسأل أي سؤال حول تشريعات دبي- نسخة تجريبة (تصميم وتنفيذ م.أسامة الخطيب")
54
- chatbot = gr.Chatbot(elem_id="chatbot") # Assign an ID for CSS targeting
55
- msg = gr.Textbox(placeholder="اكتب سؤالك هنا...", rtl=True)
56
- clear = gr.Button("مسح")
 
57
 
58
  def user(user_message, history):
59
- return "", history + [[user_message, None]]
 
60
 
61
  def bot(history):
62
  user_message = history[-1][0]
63
- bot_message = get_response(user_message) # Your response function
64
  history[-1][1] = bot_message
65
  return history
66
 
67
  msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
68
  bot, chatbot, chatbot
69
  )
70
- clear.click(lambda: None, None, chatbot, queue=False)
71
 
72
- # Launch with custom CSS
73
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
 
 
 
 
 
2
 
3
+ # Placeholder response function
4
+ def get_response(user_message):
5
+ return f"رد تلقائي: {user_message}" # Replace this with your AI model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  with gr.Blocks(title="المتحدث الآلي للتشريعات المحلية لإمارة دبي") as demo:
8
+ gr.Markdown("# Dubai Legislation Chatbot\nاسأل أي سؤال حول تشريعات دبي - نسخة تجريبية (تصميم وتنفيذ م. أسامة الخطيب)", elem_id="title")
9
+
10
+ chatbot = gr.Chatbot(elem_id="chatbot")
11
+ msg = gr.Textbox(placeholder="اكتب سؤالك هنا...", rtl=True, elem_id="input-box")
12
+ clear = gr.Button("مسح", elem_id="clear-btn")
13
 
14
  def user(user_message, history):
15
+ history.append([user_message, None])
16
+ return "", history
17
 
18
  def bot(history):
19
  user_message = history[-1][0]
20
+ bot_message = get_response(user_message)
21
  history[-1][1] = bot_message
22
  return history
23
 
24
  msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
25
  bot, chatbot, chatbot
26
  )
 
27
 
28
+ clear.click(lambda: [], None, chatbot, queue=False)
29
+
30
+ # Launch with custom RTL-friendly CSS
31
+ demo.launch(css="""
32
+ #title {
33
+ text-align: center;
34
+ font-size: 24px;
35
+ color: darkblue;
36
+ direction: rtl;
37
+ }
38
+ #chatbot {
39
+ background-color: #f9f9f9;
40
+ border-radius: 10px;
41
+ padding: 10px;
42
+ direction: rtl;
43
+ text-align: right;
44
+ font-family: 'Tajawal', sans-serif;
45
+ }
46
+ #input-box {
47
+ border: 2px solid blue;
48
+ padding: 10px;
49
+ direction: rtl;
50
+ text-align: right;
51
+ }
52
+ #clear-btn {
53
+ background-color: red;
54
+ color: white;
55
+ font-weight: bold;
56
+ }
57
+ """)