mirxakamran893 commited on
Commit
aff8a04
Β·
verified Β·
1 Parent(s): d7a7ea4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -22
app.py CHANGED
@@ -4,10 +4,9 @@ import os
4
  import faiss
5
  import numpy as np
6
  import json
7
- import random
8
  from sentence_transformers import SentenceTransformer
9
 
10
- # βœ… Load files
11
  with open("texts.json", "r", encoding="utf-8") as f:
12
  texts = json.load(f)
13
 
@@ -17,22 +16,13 @@ embed_model = SentenceTransformer("all-MiniLM-L6-v2")
17
  API_KEY = os.environ.get("OPENROUTER_API_KEY")
18
  MODEL = "qwen/qwen-2.5-coder-32b-instruct:free"
19
 
20
- # βœ… Friendly openers
21
- friendly_openers = [
22
- "Hey! Gotcha covered πŸš€",
23
- "Sure thing! Here’s what I found 🧠",
24
- "Alright, let me break it down πŸ‘‡",
25
- "No worries β€” I’ve got you πŸ˜„",
26
- "Yup, here's how it works βš™οΈ",
27
- "Let’s dive into it πŸ’‘",
28
- "Boom! Here's the scoop πŸ’₯"
29
- ]
30
-
31
  def get_context(query, top_k=5):
32
  query_vec = embed_model.encode([query])
33
  D, I = index.search(np.array(query_vec), top_k)
34
  return "\n".join([texts[i] for i in I[0]])
35
 
 
36
  def chat_fn(message, history):
37
  headers = {
38
  "Authorization": f"Bearer {API_KEY}",
@@ -41,9 +31,9 @@ def chat_fn(message, history):
41
 
42
  context = get_context(message)
43
 
44
- system_prompt = f"""You are Codex Assistant by LogIQ Curve β€” a helpful, friendly AI assistant.
45
- Talk like you're chatting with a teammate: casual, confident, and clear. Use emojis to make things fun and engaging 😎.
46
- Avoid robotic replies. NEVER mention the word "context" to the user. Just use this internally:
47
 
48
  {context}
49
  """
@@ -64,30 +54,31 @@ Avoid robotic replies. NEVER mention the word "context" to the user. Just use th
64
  try:
65
  response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
66
  response.raise_for_status()
67
- ai_reply = response.json()["choices"][0]["message"]["content"]
68
- opener = random.choice(friendly_openers)
69
- reply = f"{opener}\n\n{ai_reply}"
70
  except Exception as e:
71
  reply = f"❌ Oops! Something went wrong: {e}"
72
 
73
  return reply
74
 
75
- # βœ… Custom CSS to hide footer + settings
76
  custom_css = """
77
  footer { display: none !important; }
78
  button[data-testid="settings-button"] { display: none !important; }
79
  """
80
 
81
- # βœ… Gradio interface
82
  with gr.Blocks(css=custom_css) as demo:
83
  chatbot = gr.Chatbot()
84
- msg = gr.Textbox(placeholder="Type your message...", show_label=False)
 
 
85
 
86
  def respond(message, history):
87
  response = chat_fn(message, history)
88
  history.append((message, response))
89
  return history, ""
90
 
 
91
  msg.submit(respond, [msg, chatbot], [chatbot, msg])
92
 
93
  demo.launch()
 
4
  import faiss
5
  import numpy as np
6
  import json
 
7
  from sentence_transformers import SentenceTransformer
8
 
9
+ # βœ… Load data
10
  with open("texts.json", "r", encoding="utf-8") as f:
11
  texts = json.load(f)
12
 
 
16
  API_KEY = os.environ.get("OPENROUTER_API_KEY")
17
  MODEL = "qwen/qwen-2.5-coder-32b-instruct:free"
18
 
19
+ # βœ… Context Search
 
 
 
 
 
 
 
 
 
 
20
  def get_context(query, top_k=5):
21
  query_vec = embed_model.encode([query])
22
  D, I = index.search(np.array(query_vec), top_k)
23
  return "\n".join([texts[i] for i in I[0]])
24
 
25
+ # βœ… Chat handler
26
  def chat_fn(message, history):
27
  headers = {
28
  "Authorization": f"Bearer {API_KEY}",
 
31
 
32
  context = get_context(message)
33
 
34
+ system_prompt = f"""You are Codex Assistant by LogIQ Curve β€” a friendly and intelligent assistant.
35
+ Speak casually and confidently, like you're helping a teammate. Add emojis where it makes sense, but don’t sound robotic.
36
+ Do NOT mention the word 'context'. Use the following to guide your answer:
37
 
38
  {context}
39
  """
 
54
  try:
55
  response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
56
  response.raise_for_status()
57
+ reply = response.json()["choices"][0]["message"]["content"]
 
 
58
  except Exception as e:
59
  reply = f"❌ Oops! Something went wrong: {e}"
60
 
61
  return reply
62
 
63
+ # βœ… Custom CSS to remove footer/settings only
64
  custom_css = """
65
  footer { display: none !important; }
66
  button[data-testid="settings-button"] { display: none !important; }
67
  """
68
 
69
+ # βœ… Launch Gradio Chat UI
70
  with gr.Blocks(css=custom_css) as demo:
71
  chatbot = gr.Chatbot()
72
+ with gr.Row():
73
+ msg = gr.Textbox(placeholder="Type your message here...", show_label=False, scale=8)
74
+ send_btn = gr.Button("Send", scale=1)
75
 
76
  def respond(message, history):
77
  response = chat_fn(message, history)
78
  history.append((message, response))
79
  return history, ""
80
 
81
+ send_btn.click(respond, [msg, chatbot], [chatbot, msg])
82
  msg.submit(respond, [msg, chatbot], [chatbot, msg])
83
 
84
  demo.launch()