Update app.py
Browse files
app.py
CHANGED
@@ -7,17 +7,18 @@ import json
|
|
7 |
from sentence_transformers import SentenceTransformer
|
8 |
from datetime import datetime
|
9 |
|
10 |
-
# β
Load context
|
11 |
with open("texts.json", "r", encoding="utf-8") as f:
|
12 |
texts = json.load(f)
|
13 |
|
14 |
index = faiss.read_index("faiss_index.bin")
|
15 |
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
|
16 |
|
|
|
17 |
API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
18 |
MODEL = "qwen/qwen-2.5-coder-32b-instruct:free"
|
19 |
|
20 |
-
# β
Get context
|
21 |
def get_context(query, top_k=5, threshold=0.45):
|
22 |
query_vec = embed_model.encode([query])
|
23 |
D, I = index.search(np.array(query_vec), top_k)
|
@@ -29,7 +30,7 @@ def get_context(query, top_k=5, threshold=0.45):
|
|
29 |
def chat_fn(message, history):
|
30 |
context = get_context(message)
|
31 |
if context is None:
|
32 |
-
return history + [(message, "β Sorry! I cannot answer that.")], gr.update(visible=True)
|
33 |
|
34 |
headers = {
|
35 |
"Authorization": f"Bearer {API_KEY}",
|
@@ -43,7 +44,7 @@ def chat_fn(message, history):
|
|
43 |
}
|
44 |
]
|
45 |
|
46 |
-
for user, assistant in history:
|
47 |
messages.append({"role": "user", "content": user})
|
48 |
messages.append({"role": "assistant", "content": assistant})
|
49 |
|
@@ -61,9 +62,9 @@ def chat_fn(message, history):
|
|
61 |
except Exception as e:
|
62 |
reply = f"β Error: {e}"
|
63 |
|
64 |
-
return history + [(message, reply)], gr.update(visible=True)
|
65 |
|
66 |
-
# β
Export
|
67 |
def export_logs(history):
|
68 |
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
69 |
filename = f"chat_log_{timestamp}.txt"
|
@@ -72,18 +73,20 @@ def export_logs(history):
|
|
72 |
f.write(log_text)
|
73 |
return filename
|
74 |
|
75 |
-
# β
|
76 |
-
with gr.Blocks(css="
|
77 |
-
|
|
|
|
|
|
|
78 |
state = gr.State([])
|
|
|
79 |
with gr.Row():
|
80 |
msg = gr.Textbox(placeholder="Type your message and press enter...", scale=8)
|
81 |
export_btn = gr.Button("Export Chat", scale=1, visible=False)
|
82 |
|
83 |
-
|
84 |
-
|
85 |
def user_send(message, history):
|
86 |
-
chatbot.update(value=history + [(message, "β³ ...")]) # Typing animation
|
87 |
return "", history + [(message, "β³ ...")], gr.update(visible=False)
|
88 |
|
89 |
def complete_chat(message, history):
|
@@ -95,6 +98,4 @@ with gr.Blocks(css=".footer {display: none !important;}") as demo:
|
|
95 |
|
96 |
export_btn.click(fn=export_logs, inputs=[state], outputs=gr.File(label="Download Chat Log"))
|
97 |
|
98 |
-
chatbot.style(height=500)
|
99 |
-
|
100 |
demo.launch()
|
|
|
7 |
from sentence_transformers import SentenceTransformer
|
8 |
from datetime import datetime
|
9 |
|
10 |
+
# β
Load RAG context
|
11 |
with open("texts.json", "r", encoding="utf-8") as f:
|
12 |
texts = json.load(f)
|
13 |
|
14 |
index = faiss.read_index("faiss_index.bin")
|
15 |
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
|
16 |
|
17 |
+
# β
API setup
|
18 |
API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
19 |
MODEL = "qwen/qwen-2.5-coder-32b-instruct:free"
|
20 |
|
21 |
+
# β
Get relevant context
|
22 |
def get_context(query, top_k=5, threshold=0.45):
|
23 |
query_vec = embed_model.encode([query])
|
24 |
D, I = index.search(np.array(query_vec), top_k)
|
|
|
30 |
def chat_fn(message, history):
|
31 |
context = get_context(message)
|
32 |
if context is None:
|
33 |
+
return history[:-1] + [(message, "β Sorry! I cannot answer that.")], gr.update(visible=True)
|
34 |
|
35 |
headers = {
|
36 |
"Authorization": f"Bearer {API_KEY}",
|
|
|
44 |
}
|
45 |
]
|
46 |
|
47 |
+
for user, assistant in history[:-1]:
|
48 |
messages.append({"role": "user", "content": user})
|
49 |
messages.append({"role": "assistant", "content": assistant})
|
50 |
|
|
|
62 |
except Exception as e:
|
63 |
reply = f"β Error: {e}"
|
64 |
|
65 |
+
return history[:-1] + [(message, reply)], gr.update(visible=True)
|
66 |
|
67 |
+
# β
Export logs to file
|
68 |
def export_logs(history):
|
69 |
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
70 |
filename = f"chat_log_{timestamp}.txt"
|
|
|
73 |
f.write(log_text)
|
74 |
return filename
|
75 |
|
76 |
+
# β
UI
|
77 |
+
with gr.Blocks(css="""
|
78 |
+
.footer {display: none !important;}
|
79 |
+
#chat-window {height: 500px !important; overflow-y: auto;}
|
80 |
+
""") as demo:
|
81 |
+
chatbot = gr.Chatbot(elem_id="chat-window")
|
82 |
state = gr.State([])
|
83 |
+
|
84 |
with gr.Row():
|
85 |
msg = gr.Textbox(placeholder="Type your message and press enter...", scale=8)
|
86 |
export_btn = gr.Button("Export Chat", scale=1, visible=False)
|
87 |
|
88 |
+
# Typing simulation
|
|
|
89 |
def user_send(message, history):
|
|
|
90 |
return "", history + [(message, "β³ ...")], gr.update(visible=False)
|
91 |
|
92 |
def complete_chat(message, history):
|
|
|
98 |
|
99 |
export_btn.click(fn=export_logs, inputs=[state], outputs=gr.File(label="Download Chat Log"))
|
100 |
|
|
|
|
|
101 |
demo.launch()
|