mirxakamran893 commited on
Commit
efa5f27
Β·
verified Β·
1 Parent(s): d1406e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -15
app.py CHANGED
@@ -6,41 +6,48 @@ import faiss
6
  import numpy as np
7
  from sentence_transformers import SentenceTransformer
8
 
9
- # βœ… Load vector store and data
10
  index = faiss.read_index("faiss_index.bin")
11
  with open("texts.json", "r") as f:
12
  texts = json.load(f)
13
 
14
- # βœ… Load embedding model
15
  model = SentenceTransformer("all-MiniLM-L6-v2")
16
 
17
- # βœ… OpenRouter setup
18
  API_KEY = os.environ.get("OPENROUTER_API_KEY")
19
  MODEL = "deepseek/deepseek-chat-v3-0324:free"
20
 
21
- # βœ… Helper: Find top-k relevant context
22
  def get_relevant_context(query, k=5):
23
  query_vector = model.encode([query])
24
  scores, indices = index.search(np.array(query_vector), k)
25
  return [texts[i] for i in indices[0] if i < len(texts)]
26
 
27
- # βœ… Chat logic
28
  def chat_with_data(message, history):
29
- greetings = ["hi", "hello", "hey", "salam", "assalamualaikum", "good morning", "good evening"]
30
  message_lower = message.lower().strip()
31
 
32
  if any(greet in message_lower for greet in greetings):
33
  return "πŸ‘‹ Hello! How can I assist you regarding LogiqCurve today?"
34
 
35
  context = get_relevant_context(message)
36
- if not context:
37
- return "❌ Sorry, I can only answer questions related to LogiqCurve and its services."
38
 
39
  context_text = "\n".join(context)
40
- prompt = f"You are a helpful assistant for LogiqCurve. Use only the following context:\n\n{context_text}\n\nUser: {message}"
 
 
 
 
 
 
 
41
 
42
  messages = [
43
- {"role": "system", "content": "You are a helpful assistant that answers only using provided context."},
44
  {"role": "user", "content": prompt}
45
  ]
46
 
@@ -57,14 +64,16 @@ def chat_with_data(message, history):
57
  try:
58
  res = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
59
  res.raise_for_status()
60
- return res.json()["choices"][0]["message"]["content"]
61
  except Exception as e:
62
- return f"❌ Error: {e}"
 
 
63
 
64
- # βœ… Gradio UI
65
  gr.ChatInterface(
66
  fn=chat_with_data,
67
- title="MK Assistant",
68
- description="Ask questions related to LogiqCurve. Chat is limited to website-related content only.",
69
  theme="soft"
70
  ).launch()
 
6
  import numpy as np
7
  from sentence_transformers import SentenceTransformer
8
 
9
+ # Load files
10
  index = faiss.read_index("faiss_index.bin")
11
  with open("texts.json", "r") as f:
12
  texts = json.load(f)
13
 
14
+ # Embedding model
15
  model = SentenceTransformer("all-MiniLM-L6-v2")
16
 
17
+ # API key
18
  API_KEY = os.environ.get("OPENROUTER_API_KEY")
19
  MODEL = "deepseek/deepseek-chat-v3-0324:free"
20
 
21
+ # Function: Get relevant chunks
22
  def get_relevant_context(query, k=5):
23
  query_vector = model.encode([query])
24
  scores, indices = index.search(np.array(query_vector), k)
25
  return [texts[i] for i in indices[0] if i < len(texts)]
26
 
27
+ # Chatbot logic
28
  def chat_with_data(message, history):
29
+ greetings = ["hi", "hello", "hey", "salam", "assalamualaikum"]
30
  message_lower = message.lower().strip()
31
 
32
  if any(greet in message_lower for greet in greetings):
33
  return "πŸ‘‹ Hello! How can I assist you regarding LogiqCurve today?"
34
 
35
  context = get_relevant_context(message)
36
+ if not context or all(len(c.strip()) < 10 for c in context):
37
+ return "❌ Sorry, I can only answer questions based on content from LogiqCurve.com."
38
 
39
  context_text = "\n".join(context)
40
+
41
+ prompt = (
42
+ f"You are a helpful assistant for LogiqCurve.\n"
43
+ f"ONLY use the context below to answer the user. Do not use any outside knowledge.\n\n"
44
+ f"Context:\n{context_text}\n\n"
45
+ f"User question: {message}\n\n"
46
+ f"Answer strictly using the context above."
47
+ )
48
 
49
  messages = [
50
+ {"role": "system", "content": "You are a strict assistant who only answers using provided context."},
51
  {"role": "user", "content": prompt}
52
  ]
53
 
 
64
  try:
65
  res = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
66
  res.raise_for_status()
67
+ reply = res.json()["choices"][0]["message"]["content"]
68
  except Exception as e:
69
+ reply = f"❌ Error: {e}"
70
+
71
+ return reply
72
 
73
+ # UI
74
  gr.ChatInterface(
75
  fn=chat_with_data,
76
+ title="MK Private Assistant",
77
+ description="Ask me about LogiqCurve services. I respond only using website data.",
78
  theme="soft"
79
  ).launch()