Update app.py
Browse files
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
|
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 |
-
# β
|
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
|
45 |
-
|
46 |
-
|
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 |
-
|
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
|
76 |
custom_css = """
|
77 |
footer { display: none !important; }
|
78 |
button[data-testid="settings-button"] { display: none !important; }
|
79 |
"""
|
80 |
|
81 |
-
# β
Gradio
|
82 |
with gr.Blocks(css=custom_css) as demo:
|
83 |
chatbot = gr.Chatbot()
|
84 |
-
|
|
|
|
|
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()
|