Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,7 @@ import numpy as np
|
|
6 |
import json
|
7 |
from sentence_transformers import SentenceTransformer
|
8 |
|
9 |
-
#
|
10 |
with open("texts.json", "r", encoding="utf-8") as f:
|
11 |
texts = json.load(f)
|
12 |
|
@@ -16,13 +16,13 @@ embed_model = SentenceTransformer("all-MiniLM-L6-v2")
|
|
16 |
API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
17 |
MODEL = "qwen/qwen-2.5-coder-32b-instruct:free"
|
18 |
|
19 |
-
#
|
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 |
-
#
|
26 |
def chat_fn(message, history):
|
27 |
headers = {
|
28 |
"Authorization": f"Bearer {API_KEY}",
|
@@ -31,9 +31,8 @@ def chat_fn(message, history):
|
|
31 |
|
32 |
context = get_context(message)
|
33 |
|
34 |
-
system_prompt = f"""You are Codex Assistant by LogIQ Curve β a friendly
|
35 |
-
|
36 |
-
Do NOT mention the word 'context'. Use the following to guide your answer:
|
37 |
|
38 |
{context}
|
39 |
"""
|
@@ -56,29 +55,82 @@ Do NOT mention the word 'context'. Use the following to guide your answer:
|
|
56 |
response.raise_for_status()
|
57 |
reply = response.json()["choices"][0]["message"]["content"]
|
58 |
except Exception as e:
|
59 |
-
reply = f"β
|
60 |
|
61 |
return reply
|
62 |
|
63 |
-
#
|
64 |
custom_css = """
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
"""
|
68 |
|
69 |
-
# β
Launch Gradio Chat UI
|
70 |
with gr.Blocks(css=custom_css) as demo:
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
demo.launch()
|
|
|
6 |
import json
|
7 |
from sentence_transformers import SentenceTransformer
|
8 |
|
9 |
+
# Load RAG content
|
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 |
+
# Search relevant context
|
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 function
|
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 helpful, friendly AI assistant.
|
35 |
+
Talk like a smart human. Use the context below to answer:
|
|
|
36 |
|
37 |
{context}
|
38 |
"""
|
|
|
55 |
response.raise_for_status()
|
56 |
reply = response.json()["choices"][0]["message"]["content"]
|
57 |
except Exception as e:
|
58 |
+
reply = f"β Error: {e}"
|
59 |
|
60 |
return reply
|
61 |
|
62 |
+
# Custom CSS for full screen & styling
|
63 |
custom_css = """
|
64 |
+
* {
|
65 |
+
font-family: 'Segoe UI', sans-serif;
|
66 |
+
}
|
67 |
+
footer, button[data-testid="settings-button"] {
|
68 |
+
display: none !important;
|
69 |
+
}
|
70 |
+
#chat-container {
|
71 |
+
display: flex;
|
72 |
+
flex-direction: column;
|
73 |
+
height: 100vh;
|
74 |
+
}
|
75 |
+
#chat-window {
|
76 |
+
flex: 1;
|
77 |
+
overflow-y: auto;
|
78 |
+
padding: 16px;
|
79 |
+
}
|
80 |
+
#input-row {
|
81 |
+
display: flex;
|
82 |
+
padding: 10px;
|
83 |
+
border-top: 1px solid #ddd;
|
84 |
+
}
|
85 |
+
textarea {
|
86 |
+
flex: 1;
|
87 |
+
resize: none;
|
88 |
+
padding: 10px;
|
89 |
+
font-size: 1rem;
|
90 |
+
}
|
91 |
+
button {
|
92 |
+
margin-left: 8px;
|
93 |
+
}
|
94 |
+
.message.user {
|
95 |
+
background-color: #daf0ff;
|
96 |
+
padding: 10px 15px;
|
97 |
+
border-radius: 10px;
|
98 |
+
align-self: flex-end;
|
99 |
+
margin: 5px 0;
|
100 |
+
max-width: 80%;
|
101 |
+
}
|
102 |
+
.message.ai {
|
103 |
+
background-color: #f0f0f0;
|
104 |
+
padding: 10px 15px;
|
105 |
+
border-radius: 10px;
|
106 |
+
align-self: flex-start;
|
107 |
+
margin: 5px 0;
|
108 |
+
max-width: 80%;
|
109 |
+
}
|
110 |
"""
|
111 |
|
|
|
112 |
with gr.Blocks(css=custom_css) as demo:
|
113 |
+
chatbot_state = gr.State([])
|
114 |
+
|
115 |
+
with gr.Column(elem_id="chat-container"):
|
116 |
+
chatbox = gr.HTML('<div id="chat-window"></div>', elem_id="chat-window")
|
117 |
+
with gr.Row(elem_id="input-row"):
|
118 |
+
msg = gr.Textbox(placeholder="Type your message here...", lines=1, scale=8, show_label=False)
|
119 |
+
send = gr.Button("Send", scale=1)
|
120 |
+
|
121 |
+
def update_chat(message, state):
|
122 |
+
state.append(("user", message))
|
123 |
+
response = chat_fn(message, [(u, a) for i, (u, a) in enumerate(zip(state[::2], state[1::2]))])
|
124 |
+
state.append(("ai", response))
|
125 |
+
|
126 |
+
html = ""
|
127 |
+
for role, content in state:
|
128 |
+
bubble_class = "user" if role == "user" else "ai"
|
129 |
+
html += f'<div class="message {bubble_class}">{content}</div>'
|
130 |
+
|
131 |
+
return html, state, ""
|
132 |
+
|
133 |
+
send.click(update_chat, [msg, chatbot_state], [chatbox, chatbot_state, msg])
|
134 |
+
msg.submit(update_chat, [msg, chatbot_state], [chatbox, chatbot_state, msg])
|
135 |
|
136 |
demo.launch()
|