VectorV17 commited on
Commit
ab0d0f7
·
verified ·
1 Parent(s): 4283c78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +172 -143
app.py CHANGED
@@ -2,10 +2,10 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  from datetime import datetime
4
 
5
- # Powered by an active provider endpoint on HF serverless framework
6
  client = InferenceClient("Qwen/Qwen2.5-7B-Instruct")
7
 
8
- # Static grounding data asset for the Agentic Architecture
9
  FAQ_CONTEXT = """
10
  An AI FAQ Agent can be adapted for almost any website where users have questions, need guidance, or require support. If people visit your site and need to find information, an FAQ agent can help. Here is a breakdown of the most common types of websites that benefit from an FAQ Agent, categorized by industry:
11
 
@@ -39,58 +39,39 @@ Travelers and event attendees are always looking for specific logistical details
39
  - Examples: Hotels, Airbnb listings, music festivals, business conferences.
40
  """
41
 
42
- # Custom thematic CSS to styling cards, status panels, and the toggle buttons
43
- CUSTOM_CSS = """
44
- .agent-header {
45
- background: linear-gradient(135deg, #2b5876 0%, #4e4376 100%);
46
- color: white;
47
- padding: 24px;
48
- border-radius: 12px;
49
- margin-bottom: 15px;
50
- box-shadow: 0 4px 15px rgba(0,0,0,0.15);
51
- }
52
- .dark .agent-header {
53
- background: linear-gradient(135deg, #141e30 0%, #243b55 100%);
54
- }
55
- .meta-panel {
56
- background: rgba(255, 255, 255, 0.1);
57
- padding: 8px 15px;
58
- border-radius: 8px;
59
- font-size: 0.95em;
60
- font-family: monospace;
61
- display: inline-block;
62
- }
63
- .toggle-btn {
64
- max-width: 150px !important;
65
- }
66
- """
67
 
68
  def extract_text(content):
69
- """Safely extracts plain string data from Gradio Chat history structures."""
70
  if isinstance(content, str):
71
  return content
72
  if isinstance(content, dict):
73
- return content.get("text", content.get("content", str(content)))
 
74
  if hasattr(content, "text") and isinstance(content.text, str):
75
  return content.text
76
  if hasattr(content, "content") and isinstance(content.content, str):
77
  return content.content
78
  return str(content)
79
 
80
- def update_timestamp():
81
- """Generates a dynamic real-time clock panel string."""
82
- now = datetime.now()
83
- return f"🕒 {now.strftime('%A, %B %d, %Y | %I:%M:%S %p')}"
84
-
85
- def respond(
86
- message,
87
- chat_history,
88
- system_message,
89
- max_tokens,
90
- temperature,
91
- top_p,
92
- ):
93
- # Anchor system parameters and context data inside root payload
94
  full_system_prompt = (
95
  f"{system_message}\n\n"
96
  f"Use the following FAQ reference context to answer questions accurately:\n"
@@ -99,130 +80,178 @@ def respond(
99
 
100
  messages = [{"role": "system", "content": full_system_prompt}]
101
 
102
- # Cleanse and build conversation history state sequentially
103
- if chat_history:
104
- for turn in chat_history:
105
- # Handles dictionary structure standard in modern Gradio
106
- if isinstance(turn, dict):
107
- role = turn.get("role")
108
- content = extract_text(turn.get("content") or turn.get("text"))
109
- if role and content:
110
- messages.append({"role": role, "content": content})
111
- # Handles tuple/list structures from historical versions
112
- elif isinstance(turn, (list, tuple)) and len(turn) >= 2:
113
- if turn[0]:
114
- messages.append({"role": "user", "content": extract_text(turn[0])})
115
- if turn[1]:
116
- messages.append({"role": "assistant", "content": extract_text(turn[1])})
117
-
118
- # Append current message text
119
- user_content_str = extract_text(message)
120
- messages.append({"role": "user", "content": user_content_str})
121
-
122
- # Initialize message list updates for modern stateful block tracking
123
- updated_history = chat_history + [
124
- {"role": "user", "content": user_content_str},
125
- {"role": "assistant", "content": ""}
126
- ]
127
 
128
  try:
129
- # Request stream from backend serverless cluster
130
  for message_chunk in client.chat_completion(
131
  messages,
132
- max_tokens=max_tokens,
133
  stream=True,
134
- temperature=temperature,
135
- top_p=top_p,
136
  ):
137
  token = message_chunk.choices[0].delta.content
138
  if token is not None:
139
- updated_history[-1]["content"] += token
140
- yield "", updated_history
141
  except Exception as e:
142
- updated_history[-1]["content"] = f"An error occurred while connecting to the model API: {str(e)}"
143
- yield "", updated_history
144
 
145
- # Initialize customizable custom-themed layout frame
146
- with gr.Blocks(css=CUSTOM_CSS, theme=gr.themes.Soft()) as demo:
147
-
148
- # Top Control and Info Strip
149
- with gr.Row():
150
- with gr.Column(scale=8):
151
- # Dynamic Live Date & Time Tracker Panel updated automatically
152
- timestamp_box = gr.Markdown(value=update_timestamp(), elem_classes=["meta-panel"])
153
- with gr.Column(scale=2, min_width=150):
154
- # Day/Night Mode Switch Trigger via Gradio JavaScript Engine
155
- theme_toggle = gr.Button("🌙 Dark Mode", elem_classes=["toggle-btn"])
156
-
157
- # Automatically refresh time indicator string every 1 second
158
- demo.load(fn=update_timestamp, outputs=timestamp_box, every=1.0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
 
160
- # JavaScript logic binding for the Theme Toggle Switch
161
- theme_toggle.click(
162
- fn=None,
163
- js="""
164
- () => {
165
- document.body.classList.toggle('dark');
166
- const isDark = document.body.classList.contains('dark');
167
- return isDark ? "☀️ Light Mode" : "🌙 Dark Mode";
168
- }
169
- """,
170
- outputs=theme_toggle
171
- )
 
 
172
 
173
- # Master Headers and System Details Box
174
- with gr.Div(elem_classes=["agent-header"]):
175
- gr.Markdown("# 🧠 System Context-Based AI FAQ Agent")
176
- gr.Markdown(
177
- "### *Agentic Architecture Grounding Demonstration*\n"
178
- "This workspace operates using a **System Context-Based (Agentic) Approach**. "
179
- "Unlike raw LLM queries, this agent is explicitly grounded via immutable system context instructions. "
180
- "It maps incoming user data safely against isolated domain intelligence vectors in real time, preventing context drift."
181
- )
182
-
183
- # Core Application Workspace Split
184
  with gr.Row():
185
- with gr.Column(scale=7):
186
- # Interactive Stream Chat Window Interface
187
- chatbot = gr.Chatbot(type="messages", height=500, show_label=False)
 
 
 
 
188
 
189
  with gr.Row():
190
- user_input = gr.Textbox(
191
- show_label=False,
192
- placeholder="Ask questions about tailoring automated customer guidance bots...",
193
- container=False,
194
- scale=8
195
  )
196
- submit_btn = gr.Button("Send", variant="primary", scale=2)
197
 
198
- clear_btn = gr.Button("🗑️ Clear Conversation History")
 
 
 
199
 
200
- # Configurations Panel Sidebar
201
- with gr.Column(scale=3):
202
- with gr.Accordion("System Settings & Grounding Metrics", open=True):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
  system_msg = gr.Textbox(
204
  value="You are an expert AI FAQ Agent assistant. Help users understand how websites use FAQ agents across various industries.",
205
- label="Active Persona Rule",
206
- lines=3
207
  )
208
- max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max Generated Tokens")
209
- temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Sampling Temperature")
210
- top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p Sampling Window")
211
-
212
- # Binding input pipelines safely across the context state configurations
213
- submit_event = submit_btn.click(
214
- fn=respond,
215
- inputs=[user_input, chatbot, system_msg, max_tokens, temperature, top_p],
216
- outputs=[user_input, chatbot]
217
  )
218
- user_input.submit(
219
- fn=respond,
220
- inputs=[user_input, chatbot, system_msg, max_tokens, temperature, top_p],
221
- outputs=[user_input, chatbot]
 
 
 
 
 
 
 
 
222
  )
223
 
224
- # Session memory wiping logic tracking
225
- clear_btn.click(fn=lambda: [], outputs=chatbot)
 
 
 
226
 
227
  if __name__ == "__main__":
228
  demo.launch()
 
2
  from huggingface_hub import InferenceClient
3
  from datetime import datetime
4
 
5
+ # Initialize the Hugging Face Serverless client
6
  client = InferenceClient("Qwen/Qwen2.5-7B-Instruct")
7
 
8
+ # AI FAQ Agent Knowledge Base Context
9
  FAQ_CONTEXT = """
10
  An AI FAQ Agent can be adapted for almost any website where users have questions, need guidance, or require support. If people visit your site and need to find information, an FAQ agent can help. Here is a breakdown of the most common types of websites that benefit from an FAQ Agent, categorized by industry:
11
 
 
39
  - Examples: Hotels, Airbnb listings, music festivals, business conferences.
40
  """
41
 
42
+ # Helper function to generate structural ordinal date format matching the UI (e.g., "08th June 2026")
43
+ def get_thematic_datetime():
44
+ now = datetime.now()
45
+ day = now.strftime("%d")
46
+ if day.endswith("1") and day != "11": suffix = "st"
47
+ elif day.endswith("2") and day != "12": suffix = "nd"
48
+ elif day.endswith("3") and day != "13": suffix = "rd"
49
+ else: suffix = "th"
50
+ return f"{day}{suffix} {now.strftime('%B %Y')} | time: {now.strftime('%I:%M%p').lower()}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  def extract_text(content):
53
+ """Safely extracts plain string text from any Gradio message structure."""
54
  if isinstance(content, str):
55
  return content
56
  if isinstance(content, dict):
57
+ if "text" in content: return content["text"]
58
+ if "content" in content: return content["content"]
59
  if hasattr(content, "text") and isinstance(content.text, str):
60
  return content.text
61
  if hasattr(content, "content") and isinstance(content.content, str):
62
  return content.content
63
  return str(content)
64
 
65
+ # Handle system prompts and streaming chat loops
66
+ def user_side(user_message, history):
67
+ if not user_message.strip():
68
+ return "", history
69
+ return "", history + [{"role": "user", "content": user_message}]
70
+
71
+ def bot_side(history, system_message, max_tokens, temperature, top_p):
72
+ if not history:
73
+ return history
74
+
 
 
 
 
75
  full_system_prompt = (
76
  f"{system_message}\n\n"
77
  f"Use the following FAQ reference context to answer questions accurately:\n"
 
80
 
81
  messages = [{"role": "system", "content": full_system_prompt}]
82
 
83
+ for msg in history:
84
+ role = msg.get("role")
85
+ content_str = extract_text(msg.get("content"))
86
+ if role and content_str:
87
+ messages.append({"role": role, "content": content_str})
88
+
89
+ history.append({"role": "assistant", "content": ""})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
  try:
 
92
  for message_chunk in client.chat_completion(
93
  messages,
94
+ max_tokens=int(max_tokens),
95
  stream=True,
96
+ temperature=float(temperature),
97
+ top_p=float(top_p),
98
  ):
99
  token = message_chunk.choices[0].delta.content
100
  if token is not None:
101
+ history[-1]["content"] += token
102
+ yield history
103
  except Exception as e:
104
+ history[-1]["content"] = f"An error occurred while connecting to the model API: {str(e)}"
105
+ yield history
106
 
107
+ # Custom UI Theme Stylesheet
108
+ custom_css = """
109
+ #header-container {
110
+ background-color: #eef2f5 !important;
111
+ border-radius: 12px !important;
112
+ padding: 18px !important;
113
+ margin-bottom: 20px !important;
114
+ border: 1px solid #dcdfe6 !important;
115
+ }
116
+ body.dark #header-container {
117
+ background-color: #1f2937 !important;
118
+ border-color: #374151 !important;
119
+ }
120
+ #header-container * {
121
+ color: #1a1a1a !important;
122
+ }
123
+ body.dark #header-container * {
124
+ color: #f9fafb !important;
125
+ }
126
+ #right-meta-card {
127
+ background-color: #005f53 !important;
128
+ border-radius: 12px !important;
129
+ padding: 24px !important;
130
+ color: white !important;
131
+ text-align: center;
132
+ }
133
+ #right-meta-card * {
134
+ color: white !important;
135
+ }
136
+ #send-btn {
137
+ background-color: #ff6600 !important;
138
+ color: white !important;
139
+ border: none !important;
140
+ font-weight: bold !important;
141
+ }
142
+ #toggle-mode-btn {
143
+ background-color: #4b5563 !important;
144
+ color: white !important;
145
+ border-radius: 20px !important;
146
+ cursor: pointer;
147
+ font-size: 13px !important;
148
+ }
149
+ .meta-status-text {
150
+ text-align: left;
151
+ font-size: 13px;
152
+ line-height: 1.8;
153
+ margin-top: 20px;
154
+ border-top: 1px solid rgba(255,255,255,0.2);
155
+ padding-top: 15px;
156
+ }
157
+ """
158
+
159
+ with gr.Blocks(css=custom_css, theme=gr.themes.Default()) as demo:
160
 
161
+ # 1. Header Section
162
+ with gr.Row(elem_id="header-container"):
163
+ with gr.Column(scale=3):
164
+ gr.Markdown(
165
+ """
166
+ ### About Us
167
+ At FAQ Agentic Hub, we are passionate about the power of structured, context-driven automation and its ability to transform customer workflows. We are a leading knowledge processing framework dedicated to helping businesses unlock the full potential of specialized, conversational AI support layers.
168
+ """
169
+ )
170
+ with gr.Column(scale=1):
171
+ gr.HTML(f"<div style='text-align: right; font-weight: 500; font-size: 14px;'>{get_thematic_datetime()}</div>")
172
+ toggle_btn = gr.Button("☀️/🌙 ➔ Change Mode (Day / Night)", elem_id="toggle-mode-btn")
173
+ # JavaScript layer injected out-of-the-box to shift between native light/dark frameworks
174
+ toggle_btn.click(None, None, None, js="() => document.body.classList.toggle('dark')")
175
 
176
+ # 2. Workspace Layout
 
 
 
 
 
 
 
 
 
 
177
  with gr.Row():
178
+ # Left Panel (Chat interface & Prompt recommendations)
179
+ with gr.Column(scale=2):
180
+ chatbot = gr.Chatbot(
181
+ label="Lane AI Support Stream",
182
+ type="messages",
183
+ value=[{"role": "assistant", "content": "Hello! I'm Lane, your FAQ Support Agent. How can I assist you with your platform infrastructure today?"}]
184
+ )
185
 
186
  with gr.Row():
187
+ msg_input = gr.Textbox(
188
+ placeholder="Ask Lane anything regarding automated FAQ policies...",
189
+ show_label=False,
190
+ scale=4
 
191
  )
192
+ send_btn = gr.Button("Send", variant="primary", scale=1, elem_id="send-btn")
193
 
194
+ # Interactive Suggestion Pill Buttons
195
+ with gr.Row():
196
+ suggest_btn1 = gr.Button("What is the course refund policy?")
197
+ suggest_btn2 = gr.Button("How do I unlock my completion certificate?")
198
 
199
+ # Right Panel (Thematic Avatar Identity Card)
200
+ with gr.Column(scale=1):
201
+ with gr.Card(elem_id="right-meta-card"):
202
+ gr.HTML(
203
+ """
204
+ <div style='display: flex; justify-content: center; margin-bottom: 12px;'>
205
+ <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);'>
206
+ <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%);'>
207
+ </div>
208
+ </div>
209
+ <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;'>AI</span></h2>
210
+ <p style='margin: 0; opacity: 0.85; font-size: 14px;'>Your Agentic FAQ Assistant</p>
211
+
212
+ <div class='meta-status-text'>
213
+ <p>🟢 Secure Session Activated</p>
214
+ <p><strong>Operational Node:</strong> FAQ-Chroma-01</p>
215
+ <p><strong>Processing Hardware:</strong> Base CPU Node</p>
216
+ </div>
217
+ """
218
+ )
219
+
220
+ # Collapse System Variables to Keep Layout Tidy
221
+ with gr.Accordion("Advanced Agent Parameter Configurations", open=False):
222
  system_msg = gr.Textbox(
223
  value="You are an expert AI FAQ Agent assistant. Help users understand how websites use FAQ agents across various industries.",
224
+ label="System Prompt Context Injection"
 
225
  )
226
+ max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
227
+ temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
228
+ top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (Nucleus Sampling)")
229
+
230
+ # 3. Component Interaction Wirings
231
+ submit_event = send_btn.click(
232
+ user_side, inputs=[msg_input, chatbot], outputs=[msg_input, chatbot], queue=False
233
+ ).then(
234
+ bot_side, inputs=[chatbot, system_msg, max_tokens, temperature, top_p], outputs=[chatbot]
235
  )
236
+
237
+ msg_input.submit(
238
+ user_side, inputs=[msg_input, chatbot], outputs=[msg_input, chatbot], queue=False
239
+ ).then(
240
+ bot_side, inputs=[chatbot, system_msg, max_tokens, temperature, top_p], outputs=[chatbot]
241
+ )
242
+
243
+ # Make suggestion buttons immediately clear text input and trigger model streaming responses
244
+ suggest_btn1.click(
245
+ user_side, inputs=[suggest_btn1, chatbot], outputs=[msg_input, chatbot], queue=False
246
+ ).then(
247
+ bot_side, inputs=[chatbot, system_msg, max_tokens, temperature, top_p], outputs=[chatbot]
248
  )
249
 
250
+ suggest_btn2.click(
251
+ user_side, inputs=[suggest_btn2, chatbot], outputs=[msg_input, chatbot], queue=False
252
+ ).then(
253
+ bot_side, inputs=[chatbot, system_msg, max_tokens, temperature, top_p], outputs=[chatbot]
254
+ )
255
 
256
  if __name__ == "__main__":
257
  demo.launch()