Spaces:
Paused
Paused
Create app.py
Browse files
app.py
CHANGED
|
@@ -1,328 +1,150 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
"q": "What are your shipping times and policies?",
|
| 25 |
-
"a": "Shipping times vary by location. Typically, local retail deliveries take 2-3 business days, while international dropshipping options take 7-14 business days. All tracking codes are sent via email."
|
| 26 |
-
},
|
| 27 |
-
{
|
| 28 |
-
"q": "What is the return policy for products?",
|
| 29 |
-
"a": "We offer a flexible 30-day return policy. Items must be unworn, in their original packaging layout, and accompanied by the checkout receipt."
|
| 30 |
-
},
|
| 31 |
-
{
|
| 32 |
-
"q": "What are your service project timelines and quotes?",
|
| 33 |
-
"a": "Standard design and consulting project timelines range between 2 to 6 weeks. You can request a personalized pricing quote directly via our booking panel."
|
| 34 |
-
},
|
| 35 |
-
{
|
| 36 |
-
"q": "What are the differences between your software subscription tiers?",
|
| 37 |
-
"a": "We offer Free, Pro, and Enterprise tiers. Pro unlocks comprehensive API documentation access, and Enterprise unlocks custom third-party platform integrations."
|
| 38 |
-
},
|
| 39 |
-
{
|
| 40 |
-
"q": "How do I unlock my course completion certificate?",
|
| 41 |
-
"a": "Completion certificates unlock automatically on your student profile dashboard as soon as all video modules and the final evaluation quiz are marked 100% complete."
|
| 42 |
-
},
|
| 43 |
-
{
|
| 44 |
-
"q": "What insurance providers do you accept at the clinic?",
|
| 45 |
-
"a": "We accept major national medical insurance providers. Please contact our front office coordinator to verify your specific policy layout before scheduling appointments."
|
| 46 |
-
},
|
| 47 |
-
{
|
| 48 |
-
"q": "What are your standard check-in and check-out times?",
|
| 49 |
-
"a": "Standard hospitality check-in opens at 3:00 PM, and checkout must be finalized by 11:00 AM. Early check-in requests can be accommodated based on live room availability."
|
| 50 |
-
}
|
| 51 |
-
]
|
| 52 |
-
|
| 53 |
-
def perform_nlp_cosine_matching(user_query):
|
| 54 |
-
"""
|
| 55 |
-
Preprocesses text using tokenization and applies Cosine Similarity
|
| 56 |
-
matching over TF-IDF vectors to find the best matching FAQ.
|
| 57 |
-
"""
|
| 58 |
-
try:
|
| 59 |
-
tokens = nltk.word_tokenize(user_query.lower())
|
| 60 |
-
cleaned_query = " ".join([w for w in tokens if re.match(r'^\w+$', w)])
|
| 61 |
-
except Exception:
|
| 62 |
-
cleaned_query = " ".join(re.findall(r'\w+', user_query.lower()))
|
| 63 |
-
|
| 64 |
-
if not cleaned_query.strip():
|
| 65 |
-
return None, 0.0
|
| 66 |
-
|
| 67 |
-
questions_corpus = [faq["q"] for faq in FAQ_DATA]
|
| 68 |
-
|
| 69 |
-
vectorizer = TfidfVectorizer(stop_words='english')
|
| 70 |
-
tfidf_matrix = vectorizer.fit_transform(questions_corpus)
|
| 71 |
-
|
| 72 |
-
query_vector = vectorizer.transform([cleaned_query])
|
| 73 |
-
similarity_scores = cosine_similarity(query_vector, tfidf_matrix)[0]
|
| 74 |
-
|
| 75 |
-
best_match_idx = similarity_scores.argmax()
|
| 76 |
-
highest_score = similarity_scores[best_match_idx]
|
| 77 |
-
|
| 78 |
-
return FAQ_DATA[best_match_idx], float(highest_score)
|
| 79 |
-
|
| 80 |
-
def get_thematic_datetime():
|
| 81 |
-
"""Generates precise, real-time updated date and running seconds strings."""
|
| 82 |
-
now = datetime.now()
|
| 83 |
-
day_int = now.day
|
| 84 |
-
if 11 <= day_int <= 13:
|
| 85 |
-
suffix = "th"
|
| 86 |
-
else:
|
| 87 |
-
suffix = {1: "st", 2: "nd", 3: "rd"}.get(day_int % 10, "th")
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
return str(content)
|
| 108 |
-
|
| 109 |
-
def user_side(user_message, history):
|
| 110 |
-
"""Handles incoming user messages and sanitizes state history format."""
|
| 111 |
if not user_message.strip():
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
clean_history = []
|
| 115 |
-
for msg in history:
|
| 116 |
-
if isinstance(msg, dict):
|
| 117 |
-
clean_history.append({"role": msg["role"], "content": extract_text(msg["content"])})
|
| 118 |
-
elif hasattr(msg, "role") and hasattr(msg, "content"):
|
| 119 |
-
clean_history.append({"role": msg.role, "content": extract_text(msg.content)})
|
| 120 |
-
|
| 121 |
-
clean_history.append({"role": "user", "content": user_message})
|
| 122 |
-
return "", clean_history
|
| 123 |
-
|
| 124 |
-
def bot_side(history, system_message, max_tokens, temperature, top_p):
|
| 125 |
-
"""Streams responses out, embedding the NLP similarity match directly into context."""
|
| 126 |
-
if not history:
|
| 127 |
-
yield history
|
| 128 |
return
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
faq_context_injection = ""
|
| 134 |
-
|
| 135 |
-
# RECTIFIED LOGIC: The metadata line only outlines telemetry metrics. It doesn't dump the duplicate answer text block.
|
| 136 |
-
if matched_faq and match_score > 0.20:
|
| 137 |
-
nlp_metadata_prefix = f"🔍 *NLP Intent Match: \"{matched_faq['q']}\" (Confidence: {match_score*100:.1f}%)*\n\n"
|
| 138 |
-
faq_context_injection = (
|
| 139 |
-
f"The user's query closely matches this curated FAQ entry:\n"
|
| 140 |
-
f"Question: {matched_faq['q']}\n"
|
| 141 |
-
f"Answer: {matched_faq['a']}\n"
|
| 142 |
-
f"Please output this answer clearly and conversationally. Do not wrap it in quotes."
|
| 143 |
-
)
|
| 144 |
-
else:
|
| 145 |
-
nlp_metadata_prefix = "🔍 *NLP Search Notice: No close database FAQ match found. Processing via general knowledge base...*\n\n"
|
| 146 |
-
faq_context_injection = "No matching database entry found. Answer the question directly using standard general system knowledge."
|
| 147 |
|
| 148 |
-
|
| 149 |
-
f"{
|
| 150 |
-
f"{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
)
|
| 152 |
-
|
| 153 |
-
messages = [{"role": "system", "content": full_system_prompt}]
|
| 154 |
-
clean_history = []
|
| 155 |
-
|
| 156 |
-
for msg in history[:-1]:
|
| 157 |
-
if isinstance(msg, dict):
|
| 158 |
-
role, content = msg.get("role"), extract_text(msg.get("content"))
|
| 159 |
-
else:
|
| 160 |
-
role, content = getattr(msg, "role", None), extract_text(getattr(msg, "content", ""))
|
| 161 |
-
if role and content:
|
| 162 |
-
messages.append({"role": role, "content": content})
|
| 163 |
-
clean_history.append({"role": role, "content": content})
|
| 164 |
|
| 165 |
-
messages
|
| 166 |
-
clean_history.append({"role": "user", "content": user_query})
|
| 167 |
|
| 168 |
-
#
|
| 169 |
-
|
|
|
|
|
|
|
| 170 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
try:
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
token = message_chunk.choices[0].delta.content
|
| 180 |
-
if token is not None:
|
| 181 |
-
clean_history[-1]["content"] += token
|
| 182 |
-
yield clean_history
|
| 183 |
except Exception as e:
|
| 184 |
-
|
| 185 |
-
yield
|
| 186 |
|
| 187 |
-
#
|
| 188 |
custom_css = """
|
| 189 |
-
#header
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
margin-bottom: 20px !important;
|
| 194 |
-
border: 1px solid #dcdfe6 !important;
|
| 195 |
-
}
|
| 196 |
-
body.dark #header-container {
|
| 197 |
-
background-color: #1f2937 !important;
|
| 198 |
-
border-color: #374151 !important;
|
| 199 |
-
}
|
| 200 |
-
#header-container * { color: #1a1a1a !important; }
|
| 201 |
-
body.dark #header-container * { color: #f9fafb !important; }
|
| 202 |
-
#right-meta-card {
|
| 203 |
-
background-color: #005f53 !important;
|
| 204 |
-
border-radius: 12px !important;
|
| 205 |
-
padding: 24px !important;
|
| 206 |
-
color: white !important;
|
| 207 |
-
text-align: center;
|
| 208 |
-
}
|
| 209 |
-
#right-meta-card * { color: white !important; }
|
| 210 |
-
#send-btn {
|
| 211 |
-
background-color: #ff6600 !important;
|
| 212 |
-
color: white !important;
|
| 213 |
-
border: none !important;
|
| 214 |
-
font-weight: bold !important;
|
| 215 |
-
}
|
| 216 |
-
#toggle-mode-btn {
|
| 217 |
-
background-color: #4b5563 !important;
|
| 218 |
-
color: white !important;
|
| 219 |
-
border-radius: 20px !important;
|
| 220 |
-
cursor: pointer;
|
| 221 |
-
font-size: 13px !important;
|
| 222 |
-
}
|
| 223 |
-
.meta-status-text {
|
| 224 |
-
text-align: left;
|
| 225 |
-
font-size: 13px;
|
| 226 |
-
line-height: 1.8;
|
| 227 |
-
margin-top: 20px;
|
| 228 |
-
border-top: 1px solid rgba(255,255,255,0.2);
|
| 229 |
-
padding-top: 15px;
|
| 230 |
-
}
|
| 231 |
"""
|
| 232 |
|
| 233 |
-
with gr.Blocks(css=custom_css) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
|
| 235 |
-
|
| 236 |
-
with gr.Row(elem_id="header-container"):
|
| 237 |
-
with gr.Column(scale=3):
|
| 238 |
-
gr.Markdown(
|
| 239 |
-
"""
|
| 240 |
-
### About Us
|
| 241 |
-
At FAQ Agentic Hub, we are passionate about the power of structured, NLP-driven automation and its ability to transform customer workflows. This engine utilizes real-time Tokenization and Cosine Similarity computations.
|
| 242 |
-
"""
|
| 243 |
-
)
|
| 244 |
-
with gr.Column(scale=1):
|
| 245 |
-
time_display = gr.HTML(value=get_thematic_datetime())
|
| 246 |
-
toggle_btn = gr.Button("☀️/🌙 ➔ Change Mode (Day / Night)", elem_id="toggle-mode-btn")
|
| 247 |
-
toggle_btn.click(None, None, None, js="() => document.body.classList.toggle('dark')")
|
| 248 |
|
| 249 |
-
# 2. Workspace Layout
|
| 250 |
with gr.Row():
|
| 251 |
-
with gr.Column(scale=
|
| 252 |
-
chatbot = gr.Chatbot(
|
| 253 |
-
|
| 254 |
-
|
|
|
|
| 255 |
)
|
| 256 |
|
| 257 |
with gr.Row():
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
show_label=False,
|
| 261 |
-
scale=4
|
| 262 |
-
)
|
| 263 |
-
send_btn = gr.Button("Send", variant="primary", scale=1, elem_id="send-btn")
|
| 264 |
-
|
| 265 |
-
with gr.Row():
|
| 266 |
-
suggest_btn1 = gr.Button("What are your shipping times and policies?")
|
| 267 |
-
suggest_btn2 = gr.Button("How do I unlock my course completion certificate?")
|
| 268 |
-
|
| 269 |
-
with gr.Column(scale=1):
|
| 270 |
-
with gr.Group(elem_id="right-meta-card"):
|
| 271 |
-
gr.HTML(
|
| 272 |
-
"""
|
| 273 |
-
<div style='display: flex; justify-content: center; margin-bottom: 12px;'>
|
| 274 |
-
<div style='background: white; border-radius: 50%; padding: 12px; width: 110px; height: 110px; display: flex; align-items: center; justify-content: center; box-shadow: 0 4px 6px rgba(0,0,0,0.1);'>
|
| 275 |
-
<img src='https://fonts.gstatic.com/s/i/short-term/release/googlesymbols/robot/default/48px.svg' style='width: 70px; filter: invert(27%) sepia(91%) saturate(2853%) hue-rotate(152deg) brightness(93%) contrast(101%);'>
|
| 276 |
-
</div>
|
| 277 |
-
</div>
|
| 278 |
-
<h2 style='margin: 5px 0 2px 0; font-size: 24px;'>Lane <span style='font-size: 11px; background: rgba(255,255,255,0.25); padding: 2px 7px; border-radius: 4px; vertical-align: middle;'>NLP AI</span></h2>
|
| 279 |
-
<p style='margin: 0; opacity: 0.85; font-size: 14px;'>Similarity-Augmented Assistant</p>
|
| 280 |
-
|
| 281 |
-
<div class='meta-status-text'>
|
| 282 |
-
<p>🟢 NLP Match Engine Online</p>
|
| 283 |
-
<p><strong>Vectorization Model:</strong> Text TF-IDF Vectorizer</p>
|
| 284 |
-
<p><strong>Processing Hardware:</strong> Base CPU Node</p>
|
| 285 |
-
</div>
|
| 286 |
-
"""
|
| 287 |
-
)
|
| 288 |
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
|
| 298 |
-
#
|
| 299 |
send_btn.click(
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
)
|
| 304 |
-
|
| 305 |
-
msg_input.submit(
|
| 306 |
-
user_side, inputs=[msg_input, chatbot], outputs=[msg_input, chatbot], queue=False
|
| 307 |
-
).then(
|
| 308 |
-
bot_side, inputs=[chatbot, system_msg, max_tokens, temperature, top_p], outputs=[chatbot]
|
| 309 |
-
)
|
| 310 |
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
)
|
| 316 |
-
|
| 317 |
-
suggest_btn2.click(
|
| 318 |
-
user_side, inputs=[suggest_btn2, chatbot], outputs=[msg_input, chatbot], queue=False
|
| 319 |
-
).then(
|
| 320 |
-
bot_side, inputs=[chatbot, system_msg, max_tokens, temperature, top_p], outputs=[chatbot]
|
| 321 |
-
)
|
| 322 |
|
| 323 |
-
|
| 324 |
-
clock_timer = gr.Timer(1.0)
|
| 325 |
-
clock_timer.tick(fn=get_thematic_datetime, outputs=time_display, queue=False)
|
| 326 |
|
| 327 |
if __name__ == "__main__":
|
| 328 |
-
demo.launch(
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
+
from database import db
|
| 5 |
+
|
| 6 |
+
# Initialize HF Serverless Inference Client (Uses HF API, 0 CPU overhead)
|
| 7 |
+
HF_TOKEN = os.getenv("HF_TOKEN", None)
|
| 8 |
+
client = InferenceClient("Qwen/Qwen2.5-7B-Instruct", token=HF_TOKEN)
|
| 9 |
+
|
| 10 |
+
def format_graph_context(records):
|
| 11 |
+
"""Formats retrieved graph records into readable LLM context."""
|
| 12 |
+
if not records:
|
| 13 |
+
return "No matching nodes found in Knowledge Graph.", ""
|
| 14 |
+
|
| 15 |
+
context_str = "RELEVANT KNOWLEDGE GRAPH NEIGHBORHOOD:\n"
|
| 16 |
+
formatted_html = "<div style='font-size: 13px; line-height: 1.6;'>"
|
| 17 |
+
|
| 18 |
+
for r in records:
|
| 19 |
+
context_str += f"- Article: {r['title']}\n"
|
| 20 |
+
context_str += f" Content: {r['content']}\n"
|
| 21 |
+
if r.get('category'):
|
| 22 |
+
context_str += f" Category: {r['category']}\n"
|
| 23 |
+
if r.get('errorCode'):
|
| 24 |
+
context_str += f" Resolves Error: {r['errorCode']} ({r.get('issueDetail', '')})\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Multi-hop prerequisites
|
| 27 |
+
prereqs = [p['title'] for p in r.get('prerequisites', []) if p.get('title')]
|
| 28 |
+
if prereqs:
|
| 29 |
+
context_str += f" Prerequisite Action Required: {', '.join(prereqs)}\n"
|
| 30 |
+
context_str += "\n"
|
| 31 |
+
|
| 32 |
+
# UI Visual Debugger Content
|
| 33 |
+
formatted_html += f"<b>📄 Article:</b> {r['title']}<br/>"
|
| 34 |
+
formatted_html += f"<b>📂 Category:</b> {r.get('category', 'N/A')}<br/>"
|
| 35 |
+
if prereqs:
|
| 36 |
+
formatted_html += f"<b>🔗 2-Hop Prerequisite:</b> <span style='color: #d97706;'>{', '.join(prereqs)}</span><br/>"
|
| 37 |
+
formatted_html += "<hr style='margin: 8px 0; border-color: #eee;'/>"
|
| 38 |
+
|
| 39 |
+
formatted_html += "</div>"
|
| 40 |
+
return context_str, formatted_html
|
| 41 |
+
|
| 42 |
+
def bot_respond(user_message, history):
|
| 43 |
+
"""Processes user input, queries CognoDB, and streams LLM response."""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
if not user_message.strip():
|
| 45 |
+
yield history, "<i>No query entered.</i>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
return
|
| 47 |
|
| 48 |
+
# Step 1: Multi-Hop Cypher Traversal on CognoDB
|
| 49 |
+
records, db_error = db.query_graph_neighborhood(user_message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
if db_error:
|
| 52 |
+
graph_context = f"Database Alert: {db_error}"
|
| 53 |
+
ui_debug = f"<span style='color:red;'>⚠️ {db_error}</span>"
|
| 54 |
+
else:
|
| 55 |
+
graph_context, ui_debug = format_graph_context(records)
|
| 56 |
+
|
| 57 |
+
# Step 2: Construct Prompt with Graph Context Injection
|
| 58 |
+
system_prompt = (
|
| 59 |
+
"You are the Wexa Knowledge Graph Assistant. "
|
| 60 |
+
"Answer the user's question accurately and concisely using ONLY the retrieved Graph Context below. "
|
| 61 |
+
"If prerequisites are mentioned in the context, explicitly state them to the user.\n\n"
|
| 62 |
+
f"{graph_context}"
|
| 63 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
messages = [{"role": "system", "content": system_prompt}]
|
|
|
|
| 66 |
|
| 67 |
+
# Reconstruct conversation history
|
| 68 |
+
for msg in history:
|
| 69 |
+
messages.append(msg)
|
| 70 |
+
messages.append({"role": "user", "content": user_message})
|
| 71 |
|
| 72 |
+
# Update chatbot UI state
|
| 73 |
+
history.append({"role": "user", "content": user_message})
|
| 74 |
+
history.append({"role": "assistant", "content": ""})
|
| 75 |
+
yield history, ui_debug
|
| 76 |
+
|
| 77 |
+
# Step 3: Stream Response from Hugging Face Inference API
|
| 78 |
try:
|
| 79 |
+
accumulated_text = ""
|
| 80 |
+
for chunk in client.chat_completion(messages, max_tokens=512, stream=True, temperature=0.3):
|
| 81 |
+
token = chunk.choices[0].delta.content
|
| 82 |
+
if token:
|
| 83 |
+
accumulated_text += token
|
| 84 |
+
history[-1]["content"] = accumulated_text
|
| 85 |
+
yield history, ui_debug
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
except Exception as e:
|
| 87 |
+
history[-1]["content"] = f"An error occurred while generating the response: {str(e)}"
|
| 88 |
+
yield history, ui_debug
|
| 89 |
|
| 90 |
+
# --- Gradio UI Layout ---
|
| 91 |
custom_css = """
|
| 92 |
+
#header { text-align: center; margin-bottom: 10px; }
|
| 93 |
+
#status-badge { padding: 8px 12px; border-radius: 6px; font-weight: bold; margin-bottom: 10px; }
|
| 94 |
+
.online { background-color: #dcfce7; color: #15803d; border: 1px solid #86efac; }
|
| 95 |
+
.offline { background-color: #fee2e2; color: #b91c1c; border: 1px solid #fca5a5; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
"""
|
| 97 |
|
| 98 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
|
| 99 |
+
gr.Markdown("# 🕸️ GraphRAG FAQ AI Agent", elem_id="header")
|
| 100 |
+
gr.Markdown("An intelligent support assistant powered by **CognoDB Cloud (Graph Database)** and **Qwen2.5-7B**.")
|
| 101 |
+
|
| 102 |
+
# Live Database Status Indicator
|
| 103 |
+
is_online = db.is_connected()
|
| 104 |
+
status_class = "online" if is_online else "offline"
|
| 105 |
+
status_text = "🟢 CognoDB Cloud: Connected & Ready" if is_online else "🔴 CognoDB Cloud: Unreachable (Fallback Mode)"
|
| 106 |
|
| 107 |
+
gr.HTML(f"<div id='status-badge' class='{status_class}'>{status_text}</div>")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
|
|
|
| 109 |
with gr.Row():
|
| 110 |
+
with gr.Column(scale=3):
|
| 111 |
+
chatbot = gr.Chatbot(type="messages", height=420)
|
| 112 |
+
msg_input = gr.Textbox(
|
| 113 |
+
placeholder="Ask a question (e.g., 'How do I fix 401 unauthorized errors?')...",
|
| 114 |
+
show_label=False
|
| 115 |
)
|
| 116 |
|
| 117 |
with gr.Row():
|
| 118 |
+
send_btn = gr.Button("Submit Query", variant="primary")
|
| 119 |
+
clear_btn = gr.Button("Clear Chat")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
+
gr.Examples(
|
| 122 |
+
examples=[
|
| 123 |
+
"How do I fix 401 unauthorized errors?",
|
| 124 |
+
"How can I rotate my API keys?",
|
| 125 |
+
"How does graph context optimize token economics?"
|
| 126 |
+
],
|
| 127 |
+
inputs=msg_input
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
with gr.Column(scale=2):
|
| 131 |
+
gr.Markdown("### 🔍 Live Graph Retrieval Inspector")
|
| 132 |
+
graph_inspector = gr.HTML("<i>Submit a query to inspect live Cypher 2-hop traversal context...</i>")
|
| 133 |
|
| 134 |
+
# Event Wiring
|
| 135 |
send_btn.click(
|
| 136 |
+
bot_respond,
|
| 137 |
+
inputs=[msg_input, chatbot],
|
| 138 |
+
outputs=[chatbot, graph_inspector]
|
| 139 |
+
).then(lambda: "", None, msg_input)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
msg_input.submit(
|
| 142 |
+
bot_respond,
|
| 143 |
+
inputs=[msg_input, chatbot],
|
| 144 |
+
outputs=[chatbot, graph_inspector]
|
| 145 |
+
).then(lambda: "", None, msg_input)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
+
clear_btn.click(lambda: ([], "<i>Chat cleared.</i>"), None, [chatbot, graph_inspector])
|
|
|
|
|
|
|
| 148 |
|
| 149 |
if __name__ == "__main__":
|
| 150 |
+
demo.launch()
|