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 |
-
# Load context
|
10 |
with open("texts.json", "r", encoding="utf-8") as f:
|
11 |
texts = json.load(f)
|
12 |
|
@@ -16,11 +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 |
def get_context(query, top_k=5):
|
20 |
query_vec = embed_model.encode([query])
|
21 |
D, I = index.search(np.array(query_vec), top_k)
|
22 |
return "\n".join([texts[i] for i in I[0]])
|
23 |
|
|
|
24 |
def chat_fn(message, history):
|
25 |
headers = {
|
26 |
"Authorization": f"Bearer {API_KEY}",
|
@@ -52,6 +54,18 @@ Avoid robotic language. Respond using the following information:
|
|
52 |
|
53 |
return reply
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
custom_css = """
|
56 |
* {
|
57 |
font-family: 'Segoe UI', sans-serif;
|
@@ -71,7 +85,6 @@ custom_css = """
|
|
71 |
max-width: 75%;
|
72 |
word-wrap: break-word;
|
73 |
box-shadow: 0 4px 14px rgba(0,0,0,0.08);
|
74 |
-
transition: 0.3s;
|
75 |
}
|
76 |
.message.user {
|
77 |
background-color: #4F46E5;
|
@@ -110,7 +123,7 @@ button {
|
|
110 |
}
|
111 |
"""
|
112 |
|
113 |
-
with gr.Blocks(css=custom_css) as
|
114 |
chatbot_state = gr.State([])
|
115 |
|
116 |
with gr.Column():
|
@@ -138,4 +151,7 @@ with gr.Blocks(css=custom_css) as demo:
|
|
138 |
send.click(respond, [msg, chatbot_state], [chatbox, chatbot_state, msg])
|
139 |
msg.submit(respond, [msg, chatbot_state], [chatbox, chatbot_state, msg])
|
140 |
|
141 |
-
|
|
|
|
|
|
|
|
6 |
import json
|
7 |
from sentence_transformers import SentenceTransformer
|
8 |
|
9 |
+
# Load RAG context
|
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 |
+
# π Get relevant context from vector index
|
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 |
+
# π¬ Generate reply with context + chat history
|
26 |
def chat_fn(message, history):
|
27 |
headers = {
|
28 |
"Authorization": f"Bearer {API_KEY}",
|
|
|
54 |
|
55 |
return reply
|
56 |
|
57 |
+
# β
API endpoint: plain input/output
|
58 |
+
def api_respond(message):
|
59 |
+
return chat_fn(message, [])
|
60 |
+
|
61 |
+
api_interface = gr.Interface(
|
62 |
+
fn=api_respond,
|
63 |
+
inputs=gr.Textbox(lines=1, placeholder="Ask me anything..."),
|
64 |
+
outputs="text",
|
65 |
+
live=False
|
66 |
+
)
|
67 |
+
|
68 |
+
# π¨ Custom CSS and chat UI
|
69 |
custom_css = """
|
70 |
* {
|
71 |
font-family: 'Segoe UI', sans-serif;
|
|
|
85 |
max-width: 75%;
|
86 |
word-wrap: break-word;
|
87 |
box-shadow: 0 4px 14px rgba(0,0,0,0.08);
|
|
|
88 |
}
|
89 |
.message.user {
|
90 |
background-color: #4F46E5;
|
|
|
123 |
}
|
124 |
"""
|
125 |
|
126 |
+
with gr.Blocks(css=custom_css) as chatbot_ui:
|
127 |
chatbot_state = gr.State([])
|
128 |
|
129 |
with gr.Column():
|
|
|
151 |
send.click(respond, [msg, chatbot_state], [chatbox, chatbot_state, msg])
|
152 |
msg.submit(respond, [msg, chatbot_state], [chatbox, chatbot_state, msg])
|
153 |
|
154 |
+
# π Launch both UI and API
|
155 |
+
if __name__ == "__main__":
|
156 |
+
chatbot_ui.queue().launch(share=True, inline=False)
|
157 |
+
api_interface.launch(inline=False)
|