Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
import json
|
3 |
from sentence_transformers import SentenceTransformer
|
4 |
-
from transformers import pipeline
|
5 |
from sklearn.metrics.pairwise import cosine_similarity
|
6 |
import numpy as np
|
7 |
import os
|
8 |
-
from huggingface_hub import upload_file, hf_hub_download
|
9 |
|
10 |
PUP_Themed_css = """
|
11 |
html, body, .gradio-container, .gr-app {
|
@@ -19,7 +18,11 @@ html, body, .gradio-container, .gr-app {
|
|
19 |
"""
|
20 |
|
21 |
embedding_model = SentenceTransformer('paraphrase-mpnet-base-v2')
|
22 |
-
|
|
|
|
|
|
|
|
|
23 |
|
24 |
with open("dataset.json", "r") as f:
|
25 |
dataset = json.load(f)
|
@@ -31,8 +34,8 @@ question_embeddings = embedding_model.encode(questions, convert_to_tensor=True)
|
|
31 |
chat_history = []
|
32 |
feedback_data = []
|
33 |
feedback_questions = []
|
34 |
-
feedback_answers = []
|
35 |
feedback_embeddings = None
|
|
|
36 |
|
37 |
feedback_path = "outputs/feedback.json"
|
38 |
os.makedirs("outputs", exist_ok=True)
|
@@ -48,7 +51,6 @@ try:
|
|
48 |
with open(downloaded_path, "r") as f:
|
49 |
feedback_data = json.load(f)
|
50 |
feedback_questions = [item["question"] for item in feedback_data]
|
51 |
-
feedback_answers = [item["response"] for item in feedback_data]
|
52 |
if feedback_questions:
|
53 |
feedback_embeddings = embedding_model.encode(feedback_questions, convert_to_tensor=True)
|
54 |
|
@@ -63,7 +65,7 @@ def upload_feedback_to_hf():
|
|
63 |
hf_token = os.getenv("PUP_AI_Chatbot_Token")
|
64 |
if not hf_token:
|
65 |
raise ValueError("Hugging Face token not found in environment variables!")
|
66 |
-
|
67 |
try:
|
68 |
upload_file(
|
69 |
path_or_fileobj=feedback_path,
|
@@ -104,40 +106,33 @@ def chatbot_response(query, chat_history):
|
|
104 |
|
105 |
if best_score < 0.4:
|
106 |
response = "Sorry, but the PUP handbook does not contain such information."
|
107 |
-
chat_history.append((query, response))
|
108 |
-
return "", chat_history, gr.update(visible=True)
|
109 |
-
|
110 |
-
prompt = (
|
111 |
-
f"\"{matched_a}\"\n\n"
|
112 |
-
f"Please explain this to a student in a short, natural, and easy-to-understand way. "
|
113 |
-
f"Use simple words, and do not add new information."
|
114 |
-
)
|
115 |
-
|
116 |
-
llm_response = llm(prompt, max_length=200, do_sample=True, temperature=0.7, top_p=0.9)[0]["generated_text"].strip()
|
117 |
-
if not llm_response:
|
118 |
-
llm_response = "I'm sorry, I couldn't simplify that at the moment."
|
119 |
-
|
120 |
-
a_embedding = embedding_model.encode([matched_a], convert_to_tensor=True)
|
121 |
-
llm_embedding = embedding_model.encode([llm_response], convert_to_tensor=True)
|
122 |
-
explanation_similarity = cosine_similarity(a_embedding.cpu().numpy(), llm_embedding.cpu().numpy())[0][0]
|
123 |
-
|
124 |
-
if explanation_similarity >= 0.95:
|
125 |
-
final_response = f"According to the university handbook, {matched_a}"
|
126 |
else:
|
127 |
-
|
128 |
-
|
129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
return "", chat_history, gr.update(visible=True)
|
131 |
|
132 |
def record_feedback(feedback, chat_history):
|
133 |
-
global feedback_embeddings
|
134 |
if chat_history:
|
135 |
last_query, last_response = chat_history[-1]
|
136 |
matched = False
|
|
|
137 |
|
138 |
for item in feedback_data:
|
139 |
existing_embedding = embedding_model.encode([item["question"]], convert_to_tensor=True)
|
140 |
-
new_embedding = embedding_model.encode([last_query], convert_to_tensor=True)
|
141 |
similarity = cosine_similarity(existing_embedding.cpu().numpy(), new_embedding.cpu().numpy())[0][0]
|
142 |
if similarity >= 0.8 and item["response"] == last_response:
|
143 |
matched = True
|
@@ -168,23 +163,45 @@ def record_feedback(feedback, chat_history):
|
|
168 |
|
169 |
with gr.Blocks(css=PUP_Themed_css, title="University Handbook AI Chatbot") as demo:
|
170 |
gr.Markdown(
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
|
|
|
|
|
|
184 |
|
185 |
state = gr.State(chat_history)
|
186 |
chatbot_ui = gr.Chatbot(label="Chat", show_label=False)
|
187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
with gr.Row():
|
189 |
query_input = gr.Textbox(placeholder="Type your question here...", show_label=False)
|
190 |
submit_btn = gr.Button("Submit")
|
|
|
1 |
import gradio as gr
|
2 |
import json
|
3 |
from sentence_transformers import SentenceTransformer
|
|
|
4 |
from sklearn.metrics.pairwise import cosine_similarity
|
5 |
import numpy as np
|
6 |
import os
|
7 |
+
from huggingface_hub import upload_file, hf_hub_download, InferenceClient
|
8 |
|
9 |
PUP_Themed_css = """
|
10 |
html, body, .gradio-container, .gr-app {
|
|
|
18 |
"""
|
19 |
|
20 |
embedding_model = SentenceTransformer('paraphrase-mpnet-base-v2')
|
21 |
+
inference_token = os.getenv("HF_TOKEN") or os.getenv("PUP_AI_Chatbot_Token")
|
22 |
+
inference_client = InferenceClient(
|
23 |
+
model="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
24 |
+
token=inference_token
|
25 |
+
)
|
26 |
|
27 |
with open("dataset.json", "r") as f:
|
28 |
dataset = json.load(f)
|
|
|
34 |
chat_history = []
|
35 |
feedback_data = []
|
36 |
feedback_questions = []
|
|
|
37 |
feedback_embeddings = None
|
38 |
+
dev_mode = {"enabled": False} # DevMode flag
|
39 |
|
40 |
feedback_path = "outputs/feedback.json"
|
41 |
os.makedirs("outputs", exist_ok=True)
|
|
|
51 |
with open(downloaded_path, "r") as f:
|
52 |
feedback_data = json.load(f)
|
53 |
feedback_questions = [item["question"] for item in feedback_data]
|
|
|
54 |
if feedback_questions:
|
55 |
feedback_embeddings = embedding_model.encode(feedback_questions, convert_to_tensor=True)
|
56 |
|
|
|
65 |
hf_token = os.getenv("PUP_AI_Chatbot_Token")
|
66 |
if not hf_token:
|
67 |
raise ValueError("Hugging Face token not found in environment variables!")
|
68 |
+
|
69 |
try:
|
70 |
upload_file(
|
71 |
path_or_fileobj=feedback_path,
|
|
|
106 |
|
107 |
if best_score < 0.4:
|
108 |
response = "Sorry, but the PUP handbook does not contain such information."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
else:
|
110 |
+
if dev_mode["enabled"]:
|
111 |
+
prompt = (
|
112 |
+
f"A student asked:\n\"{query}\"\n\n"
|
113 |
+
f"Relevant handbook info:\n\"{matched_a}\"\n\n"
|
114 |
+
f"Please answer based only on this handbook content."
|
115 |
+
)
|
116 |
+
try:
|
117 |
+
response = inference_client.text_generation(prompt, max_new_tokens=200, temperature=0.7)
|
118 |
+
except Exception as e:
|
119 |
+
print(f"[ERROR] HF inference failed: {e}")
|
120 |
+
response = f"(Fallback) {matched_a}"
|
121 |
+
else:
|
122 |
+
response = f"According to the university handbook, {matched_a}"
|
123 |
+
|
124 |
+
chat_history.append((query, response.strip()))
|
125 |
return "", chat_history, gr.update(visible=True)
|
126 |
|
127 |
def record_feedback(feedback, chat_history):
|
128 |
+
global feedback_embeddings, feedback_questions
|
129 |
if chat_history:
|
130 |
last_query, last_response = chat_history[-1]
|
131 |
matched = False
|
132 |
+
new_embedding = embedding_model.encode([last_query], convert_to_tensor=True)
|
133 |
|
134 |
for item in feedback_data:
|
135 |
existing_embedding = embedding_model.encode([item["question"]], convert_to_tensor=True)
|
|
|
136 |
similarity = cosine_similarity(existing_embedding.cpu().numpy(), new_embedding.cpu().numpy())[0][0]
|
137 |
if similarity >= 0.8 and item["response"] == last_response:
|
138 |
matched = True
|
|
|
163 |
|
164 |
with gr.Blocks(css=PUP_Themed_css, title="University Handbook AI Chatbot") as demo:
|
165 |
gr.Markdown(
|
166 |
+
"""
|
167 |
+
<div style='
|
168 |
+
background-color: var(--block-background-fill);
|
169 |
+
border-radius: 16px;
|
170 |
+
padding: 24px 16px;
|
171 |
+
margin-bottom: 24px;
|
172 |
+
box-shadow: 0 6px 16px rgba(0, 0, 0, 0.15);
|
173 |
+
max-width: 700px;
|
174 |
+
margin-left: auto;
|
175 |
+
margin-right: auto;
|
176 |
+
text-align: center;
|
177 |
+
color: var(--text-color);'>
|
178 |
+
<h1 style='font-size: 2.2rem; margin: 0;'>University Inquiries AI Chatbot</h1>
|
179 |
+
</div>
|
180 |
+
"""
|
181 |
+
)
|
182 |
|
183 |
state = gr.State(chat_history)
|
184 |
chatbot_ui = gr.Chatbot(label="Chat", show_label=False)
|
185 |
|
186 |
+
with gr.Row():
|
187 |
+
dev_btn = gr.Button("DevMode 🔐")
|
188 |
+
password_box = gr.Textbox(placeholder="Enter Dev password", type="password", visible=False, show_label=False)
|
189 |
+
confirm_btn = gr.Button("Confirm", visible=False)
|
190 |
+
|
191 |
+
dev_pass = os.getenv("DEV_MODE_PASSWORD", "letmein")
|
192 |
+
|
193 |
+
def show_password_input():
|
194 |
+
return gr.update(visible=True), gr.update(visible=True)
|
195 |
+
|
196 |
+
def enable_devmode(password_input):
|
197 |
+
if password_input == dev_pass:
|
198 |
+
dev_mode["enabled"] = True
|
199 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(value="DevMode ✅", interactive=False)
|
200 |
+
return gr.update(visible=True), gr.update(visible=True), gr.update(value="Wrong password. Try again.")
|
201 |
+
|
202 |
+
dev_btn.click(show_password_input, outputs=[password_box, confirm_btn])
|
203 |
+
confirm_btn.click(enable_devmode, inputs=[password_box], outputs=[password_box, confirm_btn, dev_btn])
|
204 |
+
|
205 |
with gr.Row():
|
206 |
query_input = gr.Textbox(placeholder="Type your question here...", show_label=False)
|
207 |
submit_btn = gr.Button("Submit")
|