VectorV17 commited on
Commit
55db09f
Β·
verified Β·
1 Parent(s): 10c545f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -217
app.py CHANGED
@@ -30,289 +30,127 @@ vector_store = FAISS.from_documents(documents, embedding_model)
30
 
31
 
32
  # ==========================================
33
- # 2. FEW-SHOT PROMPT COMPILER & EXECUTION
34
  # ==========================================
35
  def execution_engine(workspace_type, user_input, max_tokens, temp, top_p):
36
  if not user_input.strip():
37
- return "⚠️ **System Input Error:** Workspace field cannot be processed empty."
38
 
39
- # Structural Instructions + Few-Shot Examples
40
  if workspace_type == "debate":
41
  retriever = vector_store.as_retriever(search_kwargs={"k": 1})
42
  matched_docs = retriever.invoke(user_input)
43
- context = matched_docs[0].page_content if matched_docs else "No specific context available."
44
 
 
45
  system_instruction = (
46
- "You are a sharp, polite adversarial debate assistant. Disagree with the user's stance. "
47
- "Provide exactly one paragraph of logical counter-argument, then provide a score out of 10.\n\n"
48
- f"VERIFIED CONTEXT TO USE: {context}\n\n"
49
- "### GOOD EXAMPLE RESPONSE FORMAT:\n"
50
- "While your point highlights consumer convenience, verified data shows that global emissions from battery manufacturing offset initial green gains by up to 40%. Therefore, an immediate full transition remains structurally flawed.\n\n"
51
- "**Argument Logic Score: 6/10**"
52
  )
53
  else: # Generalize mode
54
  system_instruction = (
55
- "You are an expert news editor. Translate complex, jargon-heavy statements into plain, simple English. "
56
- "Output your answer as clear, high-level summary bullet points under 50 words total.\n\n"
57
- "### GOOD EXAMPLE RESPONSE FORMAT:\n"
58
- "* Global markets fell sharply due to sudden central bank interest rate changes.\n"
59
- "* Local manufacturing activity slowed down to an all-time low."
60
  )
61
 
62
- # Construct Prompt using Official Qwen ChatML Template format
63
  formatted_prompt = (
64
- f"<|im_start|>system\n{system_instruction}\n"
65
- "CRITICAL: Keep your response concise. Do not ramble. Complete your final sentence perfectly.<|im_end|>\n"
66
  f"<|im_start|>user\n{user_input}<|im_end|>\n"
67
  "<|im_start|>assistant\n"
68
  )
69
 
70
- # CPU Execution Pipeline
71
  try:
 
72
  raw_output = chat_pipeline(
73
  formatted_prompt,
74
  max_new_tokens=int(max_tokens),
75
- do_sample=True,
76
- temperature=float(temp),
77
- top_p=float(top_p),
78
- top_k=40,
79
- repetition_penalty=1.2
80
  )
81
 
82
  processed_text = raw_output[0]["generated_text"].split("<|im_start|>assistant\n")[-1].strip()
83
 
84
- # Programmatic Clean Up: Ensure no trailing cut-off sentences are displayed
85
- if processed_text and processed_text[-1] not in [".", "!", "?", '"', "*"]:
86
- last_punctuation = max(processed_text.rfind("."), processed_text.rfind("!"), processed_text.rfind("?"))
87
- if last_punctuation != -1:
88
- processed_text = processed_text[:last_punctuation + 1]
 
 
 
 
 
 
89
 
90
  return processed_text
91
 
92
  except Exception as error:
93
- return f"❌ **Pipeline Fault:** Execution error on CPU node. Details: {str(error)}"
94
 
95
 
96
  # ==========================================
97
- # 3. CUSTOM ENTERPRISE CSS INSPIRATION
98
  # ==========================================
99
- custom_css = """
100
- @import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap');
101
-
102
- body, .gradio-container {
103
- font-family: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, sans-serif !important;
104
- background-color: #0B0F17 !important;
105
- color: #F3F4F6 !important;
106
- }
107
-
108
- /* Header Branding */
109
- .breadcrumb-text {
110
- font-size: 12px;
111
- font-weight: 700;
112
- text-transform: uppercase;
113
- letter-spacing: 0.08em;
114
- color: #6366F1;
115
- margin-bottom: 4px;
116
- }
117
-
118
- .main-title {
119
- font-size: 32px;
120
- font-weight: 800;
121
- letter-spacing: -0.02em;
122
- color: #FFFFFF;
123
- margin-bottom: 6px;
124
- }
125
-
126
- .sub-title {
127
- font-size: 15px;
128
- color: #9CA3AF;
129
- line-height: 1.5;
130
- }
131
-
132
- /* Metric KPI Dashboard Styling */
133
- .kpi-container {
134
- display: grid;
135
- grid-template-columns: repeat(4, 1fr);
136
- gap: 12px;
137
- margin: 20px 0;
138
- }
139
-
140
- .kpi-card {
141
- background: #111827;
142
- border: 1px solid #1F2937;
143
- border-radius: 12px;
144
- padding: 16px 20px;
145
- box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.3);
146
- }
147
-
148
- .kpi-value {
149
- font-size: 26px;
150
- font-weight: 800;
151
- color: #4F46E5;
152
- letter-spacing: -0.02em;
153
- }
154
-
155
- .kpi-label {
156
- font-size: 11px;
157
- font-weight: 700;
158
- text-transform: uppercase;
159
- color: #9CA3AF;
160
- letter-spacing: 0.05em;
161
- margin-top: 4px;
162
- }
163
-
164
- /* Custom Primary Button Styling */
165
- button.primary-action-btn {
166
- background: linear-gradient(135deg, #4F46E5 0%, #6366F1 100%) !important;
167
- border: none !important;
168
- color: #FFFFFF !important;
169
- font-weight: 700 !important;
170
- border-radius: 8px !important;
171
- padding: 12px 24px !important;
172
- box-shadow: 0 4px 14px 0 rgba(79, 70, 229, 0.4) !important;
173
- transition: all 0.2s ease !important;
174
- }
175
-
176
- button.primary-action-btn:hover {
177
- transform: translateY(-1px) !important;
178
- box-shadow: 0 6px 20px 0 rgba(79, 70, 229, 0.6) !important;
179
- }
180
-
181
- /* Form Container and Tab Cards */
182
- .workspace-card {
183
- background: #111827 !important;
184
- border: 1px solid #1F2937 !important;
185
- border-radius: 16px !important;
186
- padding: 20px !important;
187
- }
188
-
189
- /* Output Stream Terminal Panel */
190
- .output-monitor {
191
- background: #0F172A !important;
192
- border: 1px solid #1E293B !important;
193
- border-radius: 12px !important;
194
- padding: 18px !important;
195
- min-height: 240px;
196
- }
197
- """
198
-
199
-
200
- # ==========================================
201
- # 4. MODULAR UI DASHBOARD (GRADIO 6.0)
202
- # ==========================================
203
- with gr.Blocks(css=custom_css, title="Enterprise AI News Suite") as demo:
204
 
205
- # 1. Executive Top Bar
206
  with gr.Row():
207
  with gr.Column(scale=3):
208
- gr.Markdown(
209
- "<div class='breadcrumb-text'>AI AGENT LIBRARY &gt; NEWS INTELLIGENCE & DEBATE</div>"
210
- "<div class='main-title'>Enterprise News Debate &amp; Summary Suite</div>"
211
- "<div class='sub-title'>Localized FAISS vector embeddings paired with few-shot prompt anchoring for deterministic, zero-hallucination analysis on CPU clusters.</div>"
212
- )
213
  with gr.Column(scale=1):
214
- gr.HighlightedText(
215
- value=[("SYSTEM ONLINE", "🟒 CPU Cluster")],
216
- show_label=False,
217
- combine_adjacent=True
218
- )
219
 
220
- # 2. Executive KPI Metrics Grid (Inspired by Webtures template)
221
- gr.HTML("""
222
- <div class="kpi-container">
223
- <div class="kpi-card">
224
- <div class="kpi-value">1.5B</div>
225
- <div class="kpi-label">Qwen Core Parameters</div>
226
- </div>
227
- <div class="kpi-card">
228
- <div class="kpi-value">FAISS</div>
229
- <div class="kpi-label">Vector RAG Engine</div>
230
- </div>
231
- <div class="kpi-card">
232
- <div class="kpi-value">32-Bit</div>
233
- <div class="kpi-label">Precision Runtime</div>
234
- </div>
235
- <div class="kpi-card">
236
- <div class="kpi-value">100%</div>
237
- <div class="kpi-label">CPU Localized Execution</div>
238
- </div>
239
- </div>
240
- """)
241
 
242
- # 3. Main Operational Workspaces
243
  with gr.Row():
244
-
245
- # Left Panel: Input & Workspaces
246
- with gr.Column(scale=12, elem_classes=["workspace-card"]):
247
- gr.Markdown("### πŸ› οΈ Agent Execution Workspaces")
248
 
249
- with gr.Tabs() as active_workspace_tabs:
250
-
251
- # Tab 1: Debate Module
252
- with gr.Tab(label="πŸŽ™οΈοΈ News Debate Agent", id=0):
253
- gr.Markdown("*State a topic or stance. The agent will pull verified context, debate your argument, and issue a score.*")
254
-
255
  debate_example_picker = gr.Dropdown(
256
- label="πŸ’‘ Preset Topics & Stances",
257
  choices=[
258
  "LLMs are indispensable for application development.",
259
  "Electric vehicles are the only way to save the environment.",
260
  "Universal Basic Income will just make people lazy."
261
  ]
262
  )
263
-
264
- debate_text_input = gr.Textbox(
265
- label="Your Stance *",
266
- placeholder="Select or paste your claim here...",
267
- lines=3
268
- )
269
-
270
  debate_example_picker.change(fn=lambda x: x, inputs=debate_example_picker, outputs=debate_text_input)
271
-
272
- run_debate_action = gr.Button("Deploy Counter-Argument Engine", variant="primary", elem_classes=["primary-action-btn"])
273
 
274
- # Tab 2: Text Simplification Module
275
- with gr.Tab(label="πŸ“° Snippet Generalizer", id=1):
276
- gr.Markdown("*Paste complex, jargon-heavy news reports to convert them into concise executive summaries.*")
277
-
278
  generalizer_example_picker = gr.Dropdown(
279
- label="πŸ’‘ Preset News Snippets",
280
  choices=[
281
  "Despite aggressive macroeconomic headwinds and structural quantitative tightening by central banking systems, retail index trends demonstrated non-linear elasticity during fiscal evaluations.",
282
  "The ecosystem validation vectors indicate substantial degradation in sub-surface lithospheric layers owing to chemical manufacturing runoffs, sparking compliance friction."
283
  ],
284
  value="Despite aggressive macroeconomic headwinds and structural quantitative tightening by central banking systems, retail index trends demonstrated non-linear elasticity during fiscal evaluations."
285
  )
286
-
287
  simplify_text_input = gr.Textbox(
288
- label="Complex News Snippet *",
289
  lines=4,
290
- value="Despite aggressive macroeconomic headwinds and structural quantitative tightening by central banking systems, retail index trends demonstrated non-linear elasticity during fiscal evaluations.",
291
- placeholder="Paste text block or official reports here..."
292
  )
293
-
294
  generalizer_example_picker.change(fn=lambda x: x, inputs=generalizer_example_picker, outputs=simplify_text_input)
295
-
296
- run_simplifier_action = gr.Button("Deploy Generalization Engine", variant="primary", elem_classes=["primary-action-btn"])
297
 
298
- # Runtime Tuning Accordion
299
- with gr.Accordion("βš™οΈ Engine Tuning & Runtime Parameters", open=False):
300
- max_tokens_val = gr.Slider(minimum=64, maximum=256, value=140, step=16, label="Max Tokens")
301
- temp_val = gr.Slider(minimum=0.1, maximum=1.0, value=0.25, step=0.05, label="Temperature (Low = Deterministic)")
302
- top_p_val = gr.Slider(minimum=0.50, maximum=1.00, value=0.85, step=0.05, label="Top-p Sampling")
303
 
304
- # Right Panel: Output Monitor
305
- with gr.Column(scale=10, elem_classes=["workspace-card"]):
306
- gr.Markdown("### πŸ–₯️ Real-Time Output Stream")
307
-
308
- with gr.Column(elem_classes=["output-monitor"]):
309
- active_output_panel = gr.Markdown(
310
- value="*System initialized and ready. Select an execution workspace and click deploy to parse stream output.*"
311
- )
312
 
313
- # ==========================================
314
- # 5. EVENT CONTROLLERS
315
- # ==========================================
316
  run_debate_action.click(
317
  fn=lambda user_in, tokens, t, p: execution_engine("debate", user_in, tokens, t, p),
318
  inputs=[debate_text_input, max_tokens_val, temp_val, top_p_val],
 
30
 
31
 
32
  # ==========================================
33
+ # 2. OPTIMIZED EXECUTION ENGINE
34
  # ==========================================
35
  def execution_engine(workspace_type, user_input, max_tokens, temp, top_p):
36
  if not user_input.strip():
37
+ return "⚠️ **System Input Error:** Input field cannot be empty."
38
 
 
39
  if workspace_type == "debate":
40
  retriever = vector_store.as_retriever(search_kwargs={"k": 1})
41
  matched_docs = retriever.invoke(user_input)
42
+ context = matched_docs[0].page_content if matched_docs else "No context available."
43
 
44
+ # Concise prompt structure ensuring score presence
45
  system_instruction = (
46
+ "You are a sharp, polite adversarial debate assistant. Disagree with the user's stance.\n"
47
+ f"VERIFIED CONTEXT: {context}\n\n"
48
+ "INSTRUCTIONS:\n"
49
+ "1. Write exactly 2 concise counter-argument sentences.\n"
50
+ "2. Always end your response with: '**Argument Logic Score: X/10**' (replace X with 1-10)."
 
51
  )
52
  else: # Generalize mode
53
  system_instruction = (
54
+ "You are an expert news editor. Translate complex statements into simple English.\n"
55
+ "Output exactly 2 concise bullet points summarizing key points under 40 words total."
 
 
 
56
  )
57
 
58
+ # ChatML Template Formatting
59
  formatted_prompt = (
60
+ f"<|im_start|>system\n{system_instruction}<|im_end|>\n"
 
61
  f"<|im_start|>user\n{user_input}<|im_end|>\n"
62
  "<|im_start|>assistant\n"
63
  )
64
 
 
65
  try:
66
+ # Greedy decoding (do_sample=False) accelerates CPU generation significantly
67
  raw_output = chat_pipeline(
68
  formatted_prompt,
69
  max_new_tokens=int(max_tokens),
70
+ do_sample=False, # Faster deterministic execution on CPU
71
+ pad_token_id=chat_pipeline.tokenizer.eos_token_id
 
 
 
72
  )
73
 
74
  processed_text = raw_output[0]["generated_text"].split("<|im_start|>assistant\n")[-1].strip()
75
 
76
+ # Safe Truncation Cleanup: Retain score characters (*, ), digits) without stripping score line
77
+ valid_endings = [".", "!", "?", '"', "*", ")", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
78
+ if processed_text and processed_text[-1] not in valid_endings:
79
+ last_punc = max(
80
+ processed_text.rfind("."),
81
+ processed_text.rfind("!"),
82
+ processed_text.rfind("?"),
83
+ processed_text.rfind("*")
84
+ )
85
+ if last_punc != -1:
86
+ processed_text = processed_text[:last_punc + 1]
87
 
88
  return processed_text
89
 
90
  except Exception as error:
91
+ return f"❌ **Pipeline Fault:** Details: {str(error)}"
92
 
93
 
94
  # ==========================================
95
+ # 3. GRADIO INTERFACE
96
  # ==========================================
97
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
 
99
  with gr.Row():
100
  with gr.Column(scale=3):
101
+ gr.Markdown("# πŸ€– AI News Debate & Summary Agent Suite")
102
+ gr.Markdown("Localized vector embeddings + few-shot prompting optimized for CPU execution.")
103
+
 
 
104
  with gr.Column(scale=1):
105
+ gr.HighlightedText(value=[("ONLINE", "🟒 CPU Mode")], show_label=False)
 
 
 
 
106
 
107
+ gr.HTML("<hr style='border: 0.5px solid #374151; margin: 15px 0;'/>")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
 
109
  with gr.Row():
110
+ with gr.Column(scale=1):
111
+ gr.Markdown("### πŸ› οΈ Agent Workspaces")
 
 
112
 
113
+ with gr.Tabs():
114
+ with gr.Tab(label="πŸŽ™οΈ News Debate Agent"):
 
 
 
 
115
  debate_example_picker = gr.Dropdown(
116
+ label="πŸ’‘ Preset Topics",
117
  choices=[
118
  "LLMs are indispensable for application development.",
119
  "Electric vehicles are the only way to save the environment.",
120
  "Universal Basic Income will just make people lazy."
121
  ]
122
  )
123
+ debate_text_input = gr.Textbox(label="Your Stance", placeholder="Enter your claim...")
 
 
 
 
 
 
124
  debate_example_picker.change(fn=lambda x: x, inputs=debate_example_picker, outputs=debate_text_input)
125
+ run_debate_action = gr.Button("Deploy Counter-Argument & Score", variant="primary")
 
126
 
127
+ with gr.Tab(label="πŸ“° Snippet Generalizer"):
 
 
 
128
  generalizer_example_picker = gr.Dropdown(
129
+ label="πŸ’‘ Preset Complex News",
130
  choices=[
131
  "Despite aggressive macroeconomic headwinds and structural quantitative tightening by central banking systems, retail index trends demonstrated non-linear elasticity during fiscal evaluations.",
132
  "The ecosystem validation vectors indicate substantial degradation in sub-surface lithospheric layers owing to chemical manufacturing runoffs, sparking compliance friction."
133
  ],
134
  value="Despite aggressive macroeconomic headwinds and structural quantitative tightening by central banking systems, retail index trends demonstrated non-linear elasticity during fiscal evaluations."
135
  )
 
136
  simplify_text_input = gr.Textbox(
137
+ label="Complex News Snippet",
138
  lines=4,
139
+ value="Despite aggressive macroeconomic headwinds and structural quantitative tightening by central banking systems, retail index trends demonstrated non-linear elasticity during fiscal evaluations."
 
140
  )
 
141
  generalizer_example_picker.change(fn=lambda x: x, inputs=generalizer_example_picker, outputs=simplify_text_input)
142
+ run_simplifier_action = gr.Button("Deploy Generalization Engine", variant="primary")
 
143
 
144
+ with gr.Accordion("βš™οΈ Parameter Tuning", open=False):
145
+ max_tokens_val = gr.Slider(minimum=100, maximum=300, value=200, step=20, label="Max tokens")
146
+ temp_val = gr.Slider(minimum=0.1, maximum=1.0, value=0.25, step=0.05, label="Temperature")
147
+ top_p_val = gr.Slider(minimum=0.50, maximum=1.00, value=0.85, step=0.05, label="Top-p")
 
148
 
149
+ with gr.Column(scale=1):
150
+ gr.Markdown("### πŸ–₯️ Active Output Stream")
151
+ active_output_panel = gr.Markdown(value="*System ready.*")
 
 
 
 
 
152
 
153
+ # Event Bindings
 
 
154
  run_debate_action.click(
155
  fn=lambda user_in, tokens, t, p: execution_engine("debate", user_in, tokens, t, p),
156
  inputs=[debate_text_input, max_tokens_val, temp_val, top_p_val],