VectorV17 commited on
Commit
919d5d6
ยท
verified ยท
1 Parent(s): 767aa35

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -260
app.py CHANGED
@@ -1,56 +1,53 @@
1
- """
2
- AI News Debate & Summary Agent Suite
3
- ----------------------------------
4
- An enterprise-grade, lightweight RAG & Few-Shot NLP application designed for
5
- Hugging Face Spaces CPU Basic instances.
6
- """
7
-
8
  import logging
9
- import re
10
  from typing import Optional
11
-
12
- import torch
13
  import gradio as gr
14
- from transformers import pipeline
 
 
15
  from langchain_community.vectorstores import FAISS
16
  from langchain_huggingface import HuggingFaceEmbeddings
17
  from langchain_core.documents import Document
18
 
19
- # ==============================================================================
20
- # 1. LOGGING & SYSTEM CONFIGURATION
21
- # ==============================================================================
22
- logging.basicConfig(
23
- level=logging.INFO,
24
- format="%(asctime)s [%(levelname)s] %(name)s - %(message)s"
25
- )
26
- logger = logging.getLogger("AIAgentSuite")
27
-
28
-
29
- # ==============================================================================
30
- # 2. SINGLETON RESOURCE & ENGINE REGISTRY
31
- # ==============================================================================
32
- class EngineRegistry:
33
- """
34
- Singleton registry managing model loading, pipeline state,
35
- and FAISS vector stores to maximize memory efficiency on CPU Basic hardware.
36
- """
37
- _instance: Optional["EngineRegistry"] = None
38
-
39
- def __init__(self) -> None:
40
- logger.info("Initializing Lightweight Sentence-Transformer Embedding Engine...")
 
41
  self.embedding_model = HuggingFaceEmbeddings(
42
  model_name="sentence-transformers/all-MiniLM-L6-v2"
43
  )
44
 
45
- logger.info("Initializing High-Performance Qwen2.5-1.5B Core on CPU...")
46
- self.chat_pipeline = pipeline(
47
- "text-generation",
48
- model="Qwen/Qwen2.5-1.5B-Instruct",
49
- device="cpu",
50
- torch_dtype=torch.float32
 
 
51
  )
52
 
53
- logger.info("Indexing Fact-Checking RAG Context Database into FAISS...")
54
  mock_news_db = [
55
  "AI and Automation: Global studies confirm that while AI will replace 85 million routine operational jobs by late 2026, it simultaneously offsets this by engineering 97 million technical roles.",
56
  "EV Industrial Lifecycle: Electric Vehicles emit zero greenhouse operational gases, but lithium-ion battery extraction increases heavy localized ecosystem toxicity by 42%.",
@@ -58,249 +55,108 @@ class EngineRegistry:
58
  ]
59
  documents = [Document(page_content=text) for text in mock_news_db]
60
  self.vector_store = FAISS.from_documents(documents, self.embedding_model)
61
- logger.info("Engine Registry successfully loaded into system memory.")
62
 
63
  @classmethod
64
- def get_instance(cls) -> "EngineRegistry":
65
  if cls._instance is None:
66
  cls._instance = cls()
67
  return cls._instance
68
 
69
-
70
- # ==============================================================================
71
- # 3. CORE EXECUTION & PROMPT COMPILER
72
- # ==============================================================================
73
- def execution_engine(
74
- workspace_type: str,
75
- user_input: str,
76
- max_tokens: int,
77
- temp: float,
78
- top_p: float
79
- ) -> str:
80
- """
81
- Processes workspace queries through vector retrieval, ChatML prompt compilation,
82
- and output post-processing.
83
- """
84
  if not user_input or not user_input.strip():
85
- return "โš ๏ธ **System Input Error:** Workspace field cannot be processed empty."
86
 
87
  try:
88
- registry = EngineRegistry.get_instance()
89
-
90
- # Workspace Routing & Prompt Engineering
 
 
 
91
  if workspace_type == "debate":
92
- retriever = registry.vector_store.as_retriever(search_kwargs={"k": 1})
93
- matched_docs = retriever.invoke(user_input)
94
- context = matched_docs[0].page_content if matched_docs else "No specific context available."
95
-
96
- system_instruction = (
97
- "You are a sharp, polite adversarial debate assistant. Disagree with the user's stance. "
98
- "Provide exactly one paragraph of logical counter-argument, then provide a score out of 10.\n\n"
99
- f"VERIFIED CONTEXT TO USE: {context}\n\n"
100
- "### GOOD EXAMPLE RESPONSE FORMAT:\n"
101
- "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"
102
- "**Argument Logic Score: 6/10**"
103
  )
104
- else: # Generalize mode
105
- system_instruction = (
106
- "You are an expert news editor. Translate complex, jargon-heavy statements into plain, simple English. "
107
- "Output your answer as clear, high-level summary bullet points under 50 words total.\n\n"
108
- "### GOOD EXAMPLE RESPONSE FORMAT:\n"
109
- "* Global markets fell sharply due to sudden central bank interest rate changes.\n"
110
- "* Local manufacturing activity slowed down to an all-time low."
111
  )
112
 
113
- # Qwen Official ChatML Formatting
114
- formatted_prompt = (
115
- f"<|im_start|>system\n{system_instruction}\n"
116
- "CRITICAL: Keep your response concise. Do not ramble. Complete your final sentence perfectly.<|im_end|>\n"
117
- f"<|im_start|>user\n{user_input}<|im_end|>\n"
118
- "<|im_start|>assistant\n"
119
- )
 
 
 
 
 
 
 
 
 
 
120
 
121
- # CPU Pipeline Execution
122
- raw_output = registry.chat_pipeline(
123
- formatted_prompt,
124
- max_new_tokens=int(max_tokens),
125
- do_sample=True,
126
  temperature=float(temp),
127
- top_p=float(top_p),
128
- top_k=40,
129
- repetition_penalty=1.2
130
  )
131
 
132
- processed_text = raw_output[0]["generated_text"].split("<|im_start|>assistant\n")[-1].strip()
133
-
134
- # Programmatic Clean Up: Ensure no trailing cut-off sentences are displayed
135
- if processed_text and processed_text[-1] not in [".", "!", "?", '"', "*"]:
136
- last_punctuation = max(processed_text.rfind("."), processed_text.rfind("!"), processed_text.rfind("?"))
137
- if last_punctuation != -1:
138
- processed_text = processed_text[:last_punctuation + 1]
139
-
140
- return processed_text
141
 
142
- except Exception as error:
143
- logger.error(f"Pipeline Fault: {str(error)}", exc_info=True)
144
- return f"โŒ **Pipeline Fault:** Execution error on CPU node. Details: {str(error)}"
145
-
146
-
147
- # ==============================================================================
148
- # 4. INDUSTRIAL GRADIO UI INTERFACE
149
- # ==============================================================================
150
- CUSTOM_CSS = """
151
- /* Enterprise Layout & Styling */
152
- body, .gradio-container {
153
- font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
154
- }
155
- .header-box {
156
- padding: 20px;
157
- background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
158
- border: 1px solid #334155;
159
- border-radius: 10px;
160
- margin-bottom: 15px;
161
- color: #f8fafc;
162
- }
163
- .status-card {
164
- background: #1e293b;
165
- border: 1px solid #334155;
166
- border-radius: 8px;
167
- padding: 12px 16px;
168
- }
169
- .metric-badge {
170
- display: inline-block;
171
- background: #0284c7;
172
- color: white;
173
- padding: 2px 8px;
174
- border-radius: 4px;
175
- font-size: 0.75rem;
176
- font-weight: 600;
177
- }
178
- """
179
-
180
- with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", neutral_hue="slate"), css=CUSTOM_CSS, title="AI Agent Suite") as demo:
181
-
182
- # Header & Status Section
183
- with gr.Row(elem_classes=["header-box"]):
184
- with gr.Column(scale=3):
185
- gr.Markdown("# ๐Ÿค– AI News Debate & Summary Agent Suite")
186
- gr.Markdown(
187
- "**About This Agent:** This framework utilizes localized vector embeddings alongside "
188
- "few-shot prompting to anchor small model reasoning. It delivers deterministic, "
189
- "concise outputs optimized for zero-error execution on base CPU clusters. "
190
- "This framework processes user statements and news artifacts to provide dual NLP functions: "
191
- "*Adversarial Debate Environment* using real-time vectorized context verification to engage with users "
192
- "and *Automated Generalization Engine* for complex-news summarization and analysis."
193
  )
 
 
 
194
 
195
- with gr.Column(scale=1, elem_classes=["status-card"]):
196
- gr.HighlightedText(
197
- value=[("ONLINE", "๐ŸŸข NLP Match Engine Status")],
198
- show_label=False,
199
- combine_adjacent=True
200
- )
201
- gr.Markdown(
202
- "๐Ÿชช **Agent Profile:** Few-Shot Context Assistant \n"
203
- "๐Ÿ“ฆ **Model Matrix:** Qwen2.5-1.5B-Instruct \n"
204
- "๐Ÿงฌ **Vectorization Engine:** Text FAISS Engine \n"
205
- "๐Ÿ’ป **Processing Hardware:** Base CPU Tier"
206
- )
207
 
208
- gr.HTML("<hr style='border: 0.5px solid #374151; margin: 10px 0;'/>")
 
 
 
 
 
209
 
210
- # Main Workspace Layout
211
  with gr.Row():
212
-
213
- # Left Panel: Control & Input Workspaces
214
- with gr.Column(scale=1):
215
- gr.Markdown("### ๐Ÿ› ๏ธ Agent Workspaces")
216
-
217
- with gr.Tabs() as active_workspace_tabs:
218
-
219
- # Workspace 1: Debate Agent
220
- with gr.Tab(label="๐ŸŽ™๏ธ News Debate Agent", id=0):
221
- gr.Markdown("*State a topic and your stance. The agent will pull factual context, debate you, and score your logic.*")
222
-
223
- debate_example_picker = gr.Dropdown(
224
- label="๐Ÿ’ก Suggestions for Preset Debate Topic",
225
- choices=[
226
- "LLMs are indispensable for application development.",
227
- "Electric vehicles are the only way to save the environment.",
228
- "Universal Basic Income will just make people lazy."
229
- ]
230
- )
231
-
232
- debate_text_input = gr.Textbox(
233
- label="Your Stance",
234
- placeholder="Select or paste your claim here...",
235
- lines=3
236
- )
237
-
238
- debate_example_picker.change(
239
- fn=lambda x: x,
240
- inputs=debate_example_picker,
241
- outputs=debate_text_input
242
- )
243
-
244
- run_debate_action = gr.Button("Deploy Counter-Argument & Score", variant="primary")
245
-
246
- # Workspace 2: Text Simplification Agent
247
- with gr.Tab(label="๐Ÿ“ฐ Snippet Generalizer", id=1):
248
- gr.Markdown("*Paste complex, jargon-heavy news columns here to output plain-English, executive summaries.*")
249
-
250
- generalizer_example_picker = gr.Dropdown(
251
- label="๐Ÿ’ก Examples for Preset Complex News",
252
- choices=[
253
- "Despite aggressive macroeconomic headwinds and structural quantitative tightening by central banking systems, retail index trends demonstrated non-linear elasticity during fiscal evaluations.",
254
- "The ecosystem validation vectors indicate substantial degradation in sub-surface lithospheric layers owing to chemical manufacturing runoffs, sparking compliance friction."
255
- ],
256
- value="Despite aggressive macroeconomic headwinds and structural quantitative tightening by central banking systems, retail index trends demonstrated non-linear elasticity during fiscal evaluations."
257
- )
258
-
259
- simplify_text_input = gr.Textbox(
260
- label="Complex News Snippet",
261
- lines=4,
262
- value="Despite aggressive macroeconomic headwinds and structural quantitative tightening by central banking systems, retail index trends demonstrated non-linear elasticity during fiscal evaluations.",
263
- placeholder="Paste text block or official reports here..."
264
- )
265
-
266
- generalizer_example_picker.change(
267
- fn=lambda x: x,
268
- inputs=generalizer_example_picker,
269
- outputs=simplify_text_input
270
- )
271
-
272
- run_simplifier_action = gr.Button("Deploy for generalization system", variant="primary")
273
-
274
- gr.HTML("<div style='margin-bottom: 15px;'></div>")
275
-
276
- # Runtime Parameter Tuning
277
- with gr.Accordion("โš™๏ธ Runtime Parameter Tuning", open=False):
278
- max_tokens_val = gr.Slider(minimum=64, maximum=256, value=140, step=16, label="Max tokens")
279
- temp_val = gr.Slider(minimum=0.1, maximum=1.0, value=0.25, step=0.05, label="Temperature (Low = Deterministic)")
280
- top_p_val = gr.Slider(minimum=0.50, maximum=1.00, value=0.85, step=0.05, label="Top-p")
281
-
282
- # Right Panel: Output Execution Stream
283
- with gr.Column(scale=1):
284
- gr.Markdown("### ๐Ÿ–ฅ๏ธ Active Output Stream")
285
- active_output_panel = gr.Markdown(
286
- value="*System ready. Select a workspace on the left panel and click deploy to review parsed generation stream.*"
287
- )
288
-
289
- # Event Bindings
290
- run_debate_action.click(
291
- fn=lambda user_in, tokens, t, p: execution_engine("debate", user_in, tokens, t, p),
292
- inputs=[debate_text_input, max_tokens_val, temp_val, top_p_val],
293
- outputs=active_output_panel
294
- )
295
-
296
- run_simplifier_action.click(
297
- fn=lambda user_in, tokens, t, p: execution_engine("generalize", user_in, tokens, t, p),
298
- inputs=[simplify_text_input, max_tokens_val, temp_val, top_p_val],
299
- outputs=active_output_panel
300
- )
301
 
302
- # Pre-warm singleton engine during container launch to prevent UI cold starts
303
- EngineRegistry.get_instance()
304
 
305
  if __name__ == "__main__":
306
- demo.launch(server_name="0.0.0.0", server_port=7860, theme=gr.themes.Soft())
 
1
+ import json
 
 
 
 
 
 
2
  import logging
 
3
  from typing import Optional
4
+ from pydantic import BaseModel, Field
 
5
  import gradio as gr
6
+
7
+ # Optimized CPU Engine & Embeddings
8
+ from llama_cpp import Llama
9
  from langchain_community.vectorstores import FAISS
10
  from langchain_huggingface import HuggingFaceEmbeddings
11
  from langchain_core.documents import Document
12
 
13
+ logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
14
+ logger = logging.getLogger("CSENewsEngine")
15
+
16
+ # ==========================================
17
+ # 1. STRUCTURED OUTPUT SCHEMAS (Pydantic)
18
+ # ==========================================
19
+ class DebateResponseSchema(BaseModel):
20
+ counter_argument: str = Field(description="One paragraph counter-argument grounded in context.")
21
+ logic_score: int = Field(description="Numerical argument logic score from 1 to 10.")
22
+ context_applied: bool = Field(description="True if verified context was successfully utilized.")
23
+
24
+ class SummaryResponseSchema(BaseModel):
25
+ summary_bullet_points: list[str] = Field(description="List of plain English executive summary bullets.")
26
+ word_count: int = Field(description="Total word count of the output summary.")
27
+
28
+ # ==========================================
29
+ # 2. OPTIMIZED ENGINE REGISTRY (GGUF + KV CACHE)
30
+ # ==========================================
31
+ class OptimizedEngineRegistry:
32
+ _instance: Optional["OptimizedEngineRegistry"] = None
33
+
34
+ def __init__(self):
35
+ logger.info("Initializing Embeddings Engine...")
36
  self.embedding_model = HuggingFaceEmbeddings(
37
  model_name="sentence-transformers/all-MiniLM-L6-v2"
38
  )
39
 
40
+ logger.info("Loading Quantized GGUF Model via llama.cpp (4-Bit SIMD Optimized)...")
41
+ # Download and run quantized Qwen2.5-1.5B GGUF for low RAM/high CPU speed
42
+ self.llm = Llama.from_pretrained(
43
+ repo_id="Qwen/Qwen2.5-1.5B-Instruct-GGUF",
44
+ filename="*q4_k_m.gguf",
45
+ n_ctx=2048, # Controlled Context Window Size
46
+ n_threads=4, # Optimize for HF CPU core allocation
47
+ verbose=False
48
  )
49
 
50
+ logger.info("Indexing Fact Database into FAISS Vector Store...")
51
  mock_news_db = [
52
  "AI and Automation: Global studies confirm that while AI will replace 85 million routine operational jobs by late 2026, it simultaneously offsets this by engineering 97 million technical roles.",
53
  "EV Industrial Lifecycle: Electric Vehicles emit zero greenhouse operational gases, but lithium-ion battery extraction increases heavy localized ecosystem toxicity by 42%.",
 
55
  ]
56
  documents = [Document(page_content=text) for text in mock_news_db]
57
  self.vector_store = FAISS.from_documents(documents, self.embedding_model)
 
58
 
59
  @classmethod
60
+ def get_instance(cls) -> "OptimizedEngineRegistry":
61
  if cls._instance is None:
62
  cls._instance = cls()
63
  return cls._instance
64
 
65
+ # ==========================================
66
+ # 3. CONTEXT-BUDGETED EXECUTION PIPELINE
67
+ # ==========================================
68
+ def execution_engine(workspace_type: str, user_input: str, max_tokens: int, temp: float) -> str:
 
 
 
 
 
 
 
 
 
 
 
69
  if not user_input or not user_input.strip():
70
+ return "โš ๏ธ๏ธ **Error:** User input field cannot be empty."
71
 
72
  try:
73
+ engine = OptimizedEngineRegistry.get_instance()
74
+
75
+ # --- A. Context Retrieval & Distance Thresholding ---
76
+ context_str = "No verified context matched."
77
+ context_used = False
78
+
79
  if workspace_type == "debate":
80
+ # Similarity Search with Score Thresholding (L2 Distance)
81
+ results = engine.vector_store.similarity_search_with_score(user_input, k=1)
82
+ if results and results[0][1] < 1.2: # Threshold for valid vector match
83
+ context_str = results[0][0].page_content
84
+ context_used = True
85
+
86
+ schema_json = json.dumps(DebateResponseSchema.model_json_schema(), indent=2)
87
+ system_prompt = (
88
+ "You are an adversarial debate engine. Respond EXCLUSIVELY in valid JSON matching this schema:\n"
89
+ f"{schema_json}\n\n"
90
+ f"VERIFIED CONTEXT: {context_str}"
91
  )
92
+ else:
93
+ schema_json = json.dumps(SummaryResponseSchema.model_json_schema(), indent=2)
94
+ system_prompt = (
95
+ "You are a news editor. Summarize the text into plain English. Respond EXCLUSIVELY in valid JSON:\n"
96
+ f"{schema_json}"
 
 
97
  )
98
 
99
+ # --- B. Explicit Context Window Token Budgeting ---
100
+ total_ctx_limit = 2048
101
+ system_tokens = len(engine.llm.tokenize(system_prompt.encode("utf-8")))
102
+ user_tokens = len(engine.llm.tokenize(user_input.encode("utf-8")))
103
+ allocated_budget = total_ctx_limit - (system_tokens + max_tokens + 50)
104
+
105
+ # Dynamic Truncation if User Input exceeds token budget
106
+ if user_tokens > allocated_budget:
107
+ tokens = engine.llm.tokenize(user_input.encode("utf-8"))[:allocated_budget]
108
+ user_input = engine.llm.detokenize(tokens).decode("utf-8")
109
+ logger.warning("User prompt truncated to satisfy token context budget.")
110
+
111
+ # --- C. Structured Schema-Constrained Generation ---
112
+ messages = [
113
+ {"role": "system", "content": system_prompt},
114
+ {"role": "user", "content": user_input}
115
+ ]
116
 
117
+ response = engine.llm.create_chat_completion(
118
+ messages=messages,
119
+ max_tokens=int(max_tokens),
 
 
120
  temperature=float(temp),
121
+ response_format={"type": "json_object"} # Native JSON Schema Enforcement
 
 
122
  )
123
 
124
+ raw_json_str = response["choices"][0]["message"]["content"]
125
+ parsed_json = json.loads(raw_json_str)
 
 
 
 
 
 
 
126
 
127
+ # Render Clean Output
128
+ if workspace_type == "debate":
129
+ return (
130
+ f"### ๐ŸŽ™๏ธ Adversarial Counter-Argument\n{parsed_json.get('counter_argument')}\n\n"
131
+ f"**Argument Logic Score:** {parsed_json.get('logic_score')}/10 \n"
132
+ f"**Context Grounding Applied:** `{context_used}`"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  )
134
+ else:
135
+ bullets = "\n".join([f"* {b}" for b in parsed_json.get("summary_bullet_points", [])])
136
+ return f"### ๐Ÿ“ฐ Executive Summary\n{bullets}\n\n**Total Word Count:** {parsed_json.get('word_count')} words"
137
 
138
+ except Exception as e:
139
+ logger.error(f"Execution Error: {str(e)}", exc_info=True)
140
+ return f"โŒ **System Execution Fault:** {str(e)}"
 
 
 
 
 
 
 
 
 
141
 
142
+ # ==========================================
143
+ # 4. GRADIO INTERFACE
144
+ # ==========================================
145
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
146
+ gr.Markdown("# ๐ŸŽ“ CS Industrial-Grade NLP & Debate Engine")
147
+ gr.Markdown("*Featuring GGUF SIMD Quantization, Token Budgeting, and Pydantic JSON Schema Validation.*")
148
 
 
149
  with gr.Row():
150
+ with gr.Column():
151
+ workspace = gr.Radio(choices=["debate", "generalize"], value="debate", label="Workspace Engine Mode")
152
+ user_in = gr.Textbox(lines=4, label="Input Prompt / Claim")
153
+ max_tok = gr.Slider(64, 256, value=140, label="Max Response Tokens")
154
+ temp = gr.Slider(0.1, 1.0, value=0.2, label="Temperature")
155
+ btn = gr.Button("Execute System Engine", variant="primary")
156
+ with gr.Column():
157
+ out = gr.Markdown("### ๐Ÿ–ฅ๏ธ Structured Engine Output\n*Awaiting execution...*")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
 
159
+ btn.click(execution_engine, inputs=[workspace, user_in, max_tok, temp], outputs=out)
 
160
 
161
  if __name__ == "__main__":
162
+ demo.launch(server_name="0.0.0.0", server_port=7860)