BasitAliii commited on
Commit
d3cb654
Β·
verified Β·
1 Parent(s): 5583e1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -77
app.py CHANGED
@@ -1,5 +1,5 @@
1
  # =========================================
2
- # 1️⃣ Install imports
3
  # =========================================
4
  import os
5
  import pickle
@@ -10,29 +10,45 @@ from sentence_transformers import SentenceTransformer
10
  from groq import Groq
11
 
12
  # =========================================
13
- # 2️⃣ Load Groq API from HF Secrets
14
  # =========================================
15
  client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
16
  GROQ_MODEL = "llama-3.3-70b-versatile"
17
 
18
  # =========================================
19
- # 3️⃣ Load FAISS + Chunks
20
  # =========================================
21
  index = faiss.read_index("faiss_index.bin")
22
 
23
  with open("chunks.pkl", "rb") as f:
24
  all_chunks = pickle.load(f)
25
 
26
- # Load embedding model (only for query embedding)
27
  embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
28
 
29
  # =========================================
30
- # 4️⃣ Groq Query Function
31
  # =========================================
32
  def groq_query(prompt):
33
  completion = client.chat.completions.create(
34
  messages=[
35
- {"role": "system", "content": "You are a strict university assistant. Answer ONLY using provided context."},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  {"role": "user", "content": prompt}
37
  ],
38
  model=GROQ_MODEL,
@@ -41,28 +57,26 @@ def groq_query(prompt):
41
  return completion.choices[0].message.content
42
 
43
  # =========================================
44
- # 5️⃣ RAG Function
45
  # =========================================
46
  def rag_answer(query, k=3):
47
 
48
  if not query.strip():
49
  return "Please enter a valid question."
50
 
 
51
  query_embedding = embedding_model.encode([query])
52
  query_embedding = query_embedding / np.linalg.norm(query_embedding, axis=1, keepdims=True)
53
  query_embedding = query_embedding.astype("float32")
54
 
 
55
  distances, indices = index.search(query_embedding, k)
56
  retrieved_texts = [all_chunks[i] for i in indices[0]]
57
 
58
  context = "\n\n".join(retrieved_texts)
59
 
60
  prompt = f"""
61
- Use ONLY information below to answer.
62
- If no answer is found, respond:
63
-
64
- "The information is not available in the provided documents.
65
- Please check the official University of Baltistan website for more information: https://uobs.edu.pk/"
66
 
67
  Context:
68
  {context}
@@ -70,101 +84,84 @@ Context:
70
  Question:
71
  {query}
72
  """
 
73
  return groq_query(prompt)
74
 
75
  # =========================================
76
- # Gradio UI
77
  # =========================================
78
-
79
- # Sample mock questions
80
- mock_questions = [
81
- "What is the focus of the Botany Department?",
82
- "Who is the Head of Chemistry Department?",
83
- "What programs does the Computer Science Department offer?",
84
- "What environmental challenges does the Earth & Environmental Science Dept address?",
85
- "Who is the Head of Mathematics Department?",
86
- "What is the goal of Educational Development Department?",
87
- "Who leads the Languages and Cultural Studies Department?"
88
- ]
89
-
90
- # Function to set mock question into input
91
- def set_question(q_text):
92
- return q_text
93
-
94
- # Function to respond via RAG
95
  def respond(message, history):
 
96
  if not history:
97
  history = []
98
 
99
- user_entry = {"role": "user", "content": message}
100
- answer = rag_answer(message) # your RAG function
101
- assistant_entry = {"role": "assistant", "content": answer}
102
 
103
- history.append(user_entry)
104
- history.append(assistant_entry)
105
 
106
- return history, "" # update chat and clear input
107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  with gr.Blocks() as demo:
109
- # App title
110
  gr.Markdown(
111
- "<h1 style='text-align:center; color:#2c3e50;'>πŸŽ“ UoBs HoDs Insight AI Assistant</h1>"
112
  )
113
 
114
- # Description
115
- gr.Markdown(
116
- """
117
- <div style="text-align:center; color:#34495e; font-size:16px; max-width:900px; margin:auto;">
118
- Welcome to the <b>UoBs HoDs Insight AI Assistant</b> – your trusted AI-powered guide to the University of Baltistan.
119
- This intelligent chatbot delivers accurate and verified information directly from the official messages of all department heads.
120
- Ask about academic programs, faculty, research initiatives, or departmental objectives, and receive <b>instant, reliable answers</b> strictly sourced from official university communications.
121
- For additional details or to explore further, all information is anchored to the <a href="https://uobs.edu.pk/" target="_blank">official University of Baltistan website</a>.
122
  </div>
123
- """
124
- )
125
 
126
- # Main layout: Left sidebar + Right chatbot
127
  with gr.Row():
128
- # Left column: mock questions
 
129
  with gr.Column(scale=1):
130
  gr.Markdown("### πŸ’‘ Sample Questions")
131
- # create buttons for each question
132
- mock_buttons = []
133
  for q in mock_questions:
134
- btn = gr.Button(q, elem_classes="mock-btn")
135
- mock_buttons.append(btn)
 
 
 
 
 
 
 
136
 
137
- # Right column: chatbot + input
138
  with gr.Column(scale=3):
139
- chatbot = gr.Chatbot()
140
-
141
- # Input row: 80% textbox + 20% send button
142
  with gr.Row():
143
  user_input = gr.Textbox(
144
- placeholder="Type your question here...",
145
- lines=1,
146
- scale=8 # 80% width
147
- )
148
- send_btn = gr.Button(
149
- "Send",
150
- variant="primary",
151
- elem_id="send-btn",
152
- scale=2 # 20% width
153
  )
154
 
155
- # Bind send button to respond function
 
156
  send_btn.click(
157
  respond,
158
  inputs=[user_input, chatbot],
159
  outputs=[chatbot, user_input]
160
  )
161
 
162
- # Bind each mock question button to input field
163
- for btn, q in zip(mock_buttons, mock_questions):
164
- btn.click(
165
- lambda q=q: q,
166
- outputs=user_input
167
- )
168
-
169
- # Launch the app
170
- demo.launch()
 
1
  # =========================================
2
+ # 1️⃣ Imports
3
  # =========================================
4
  import os
5
  import pickle
 
10
  from groq import Groq
11
 
12
  # =========================================
13
+ # 2️⃣ Groq API
14
  # =========================================
15
  client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
16
  GROQ_MODEL = "llama-3.3-70b-versatile"
17
 
18
  # =========================================
19
+ # 3️⃣ Load FAISS + chunks
20
  # =========================================
21
  index = faiss.read_index("faiss_index.bin")
22
 
23
  with open("chunks.pkl", "rb") as f:
24
  all_chunks = pickle.load(f)
25
 
26
+ # Embedding model
27
  embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
28
 
29
  # =========================================
30
+ # 4️⃣ LLM Call Function (MULTILINGUAL)
31
  # =========================================
32
  def groq_query(prompt):
33
  completion = client.chat.completions.create(
34
  messages=[
35
+ {
36
+ "role": "system",
37
+ "content": """
38
+ You are a strict university assistant.
39
+
40
+ RULES:
41
+ - Use ONLY the provided context
42
+ - If answer is not in context, say:
43
+ "The information is not available in the provided documents. Please check https://uobs.edu.pk/"
44
+
45
+ LANGUAGE RULES:
46
+ - If user asks in English β†’ reply in English
47
+ - If user asks in Urdu (اردو) β†’ reply in Urdu
48
+ - If user writes in Roman Urdu (e.g. "department ka head kon hai") β†’ reply in Roman Urdu
49
+ - Keep answer clear, correct and simple
50
+ """
51
+ },
52
  {"role": "user", "content": prompt}
53
  ],
54
  model=GROQ_MODEL,
 
57
  return completion.choices[0].message.content
58
 
59
  # =========================================
60
+ # 5️⃣ RAG FUNCTION
61
  # =========================================
62
  def rag_answer(query, k=3):
63
 
64
  if not query.strip():
65
  return "Please enter a valid question."
66
 
67
+ # Embed query
68
  query_embedding = embedding_model.encode([query])
69
  query_embedding = query_embedding / np.linalg.norm(query_embedding, axis=1, keepdims=True)
70
  query_embedding = query_embedding.astype("float32")
71
 
72
+ # Search FAISS
73
  distances, indices = index.search(query_embedding, k)
74
  retrieved_texts = [all_chunks[i] for i in indices[0]]
75
 
76
  context = "\n\n".join(retrieved_texts)
77
 
78
  prompt = f"""
79
+ Use ONLY the context below to answer the question.
 
 
 
 
80
 
81
  Context:
82
  {context}
 
84
  Question:
85
  {query}
86
  """
87
+
88
  return groq_query(prompt)
89
 
90
  # =========================================
91
+ # 6️⃣ Chat function
92
  # =========================================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  def respond(message, history):
94
+
95
  if not history:
96
  history = []
97
 
98
+ answer = rag_answer(message)
 
 
99
 
100
+ history.append({"role": "user", "content": message})
101
+ history.append({"role": "assistant", "content": answer})
102
 
103
+ return history, ""
104
 
105
+ # =========================================
106
+ # 7️⃣ Sample Questions
107
+ # =========================================
108
+ mock_questions = [
109
+ "What is the focus of the Botany Department?",
110
+ "Who is the Head of Chemistry Department?",
111
+ "Computer Science department ka head kon hai?",
112
+ "اردو اور Ψ§Ω†Ϊ―Ω„Ψ΄ Ω…ΫŒΪΊ جواب دیں: Education department ka goal kya hai?"
113
+ ]
114
+
115
+ # =========================================
116
+ # 8️⃣ Gradio UI
117
+ # =========================================
118
  with gr.Blocks() as demo:
119
+
120
  gr.Markdown(
121
+ "<h1 style='text-align:center;'>πŸŽ“ UoBs Multilingual AI Assistant</h1>"
122
  )
123
 
124
+ gr.Markdown("""
125
+ <div style="text-align:center;">
126
+ Ask questions in <b>English</b>, <b>Urdu</b>, or <b>Roman Urdu</b>.<br>
127
+ System will automatically respond in the same language.
 
 
 
 
128
  </div>
129
+ """)
 
130
 
 
131
  with gr.Row():
132
+
133
+ # Left panel
134
  with gr.Column(scale=1):
135
  gr.Markdown("### πŸ’‘ Sample Questions")
136
+
 
137
  for q in mock_questions:
138
+ btn = gr.Button(q)
139
+
140
+ btn.click(
141
+ fn=lambda q=q: q,
142
+ outputs=None
143
+ ).then(
144
+ lambda q=q: q,
145
+ outputs=None
146
+ )
147
 
148
+ # Right panel
149
  with gr.Column(scale=3):
150
+
151
+ chatbot = gr.Chatbot(type="messages")
152
+
153
  with gr.Row():
154
  user_input = gr.Textbox(
155
+ placeholder="Ask in English, Urdu, or Roman Urdu...",
156
+ scale=8
 
 
 
 
 
 
 
157
  )
158
 
159
+ send_btn = gr.Button("Send", scale=2)
160
+
161
  send_btn.click(
162
  respond,
163
  inputs=[user_input, chatbot],
164
  outputs=[chatbot, user_input]
165
  )
166
 
167
+ demo.launch()