VectorV17 commited on
Commit
b094b22
ยท
verified ยท
1 Parent(s): a5c094b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +293 -121
app.py CHANGED
@@ -1,157 +1,329 @@
1
- import json
2
- import logging
3
- import numpy as np
4
- import torch
5
  import gradio as gr
 
6
  from transformers import pipeline
7
- from sentence_transformers import SentenceTransformer
 
 
8
 
9
  # ==========================================
10
- # 1. LOGGING & SYSTEM INITIALIZATION
11
  # ==========================================
12
- logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
13
- logger = logging.getLogger("SimpleAIAgent")
14
 
15
- logger.info("Loading Embedding Engine & Vector Database...")
16
- embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
 
 
 
 
 
17
 
18
- # Real ground-truth context database
19
- MOCK_NEWS_DB = [
20
  "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.",
21
  "EV Industrial Lifecycle: Electric Vehicles emit zero greenhouse operational gases, but lithium-ion battery extraction increases heavy localized ecosystem toxicity by 42%.",
22
  "Universal Basic Income: Comprehensive economic trials indicate UBI successfully lowers base poverty indexes but sparks minor macro-inflationary pressures across basic retail commodities."
23
  ]
 
 
24
 
25
- # Pre-compute document embeddings normalized for Cosine Similarity
26
- doc_embeddings = embedder.encode(MOCK_NEWS_DB, normalize_embeddings=True)
27
-
28
- def retrieve_context(query: str, similarity_threshold: float = 0.35):
29
- """Pure NumPy Cosine Similarity Vector Retrieval (No LangChain needed)."""
30
- query_emb = embedder.encode([query], normalize_embeddings=True)
31
- scores = np.dot(doc_embeddings, query_emb.T).flatten() # Dot product of normalized vectors = Cosine Similarity
32
- best_idx = int(np.argmax(scores))
33
-
34
- if scores[best_idx] >= similarity_threshold:
35
- return MOCK_NEWS_DB[best_idx], float(scores[best_idx])
36
- return "No verified context matched.", float(scores[best_idx])
37
-
38
-
39
- logger.info("Loading Qwen2.5-1.5B Instruct Pipeline...")
40
- chat_pipeline = pipeline(
41
- "text-generation",
42
- model="Qwen/Qwen2.5-1.5B-Instruct",
43
- device="cpu",
44
- torch_dtype=torch.float32
45
- )
46
 
47
  # ==========================================
48
- # 2. CORE EXECUTION ENGINE
49
  # ==========================================
50
- def execution_engine(workspace_type: str, user_input: str, max_tokens: int, temp: float):
51
- if not user_input or not user_input.strip():
52
- return "โš ๏ธ **System Error:** Input field cannot be empty."
53
 
54
- try:
55
- # A. Context Retrieval & JSON Prompt Setup
56
- context_text = ""
57
- similarity_score = 0.0
 
58
 
59
- if workspace_type == "debate":
60
- context_text, similarity_score = retrieve_context(user_input)
61
- system_instruction = (
62
- "You are an adversarial debate engine. Respond EXCLUSIVELY in valid JSON format.\n"
63
- "JSON Format:\n"
64
- '{\n "counter_argument": "One concise paragraph disagreeing.",\n "logic_score": 7\n}\n\n'
65
- f"VERIFIED CONTEXT: {context_text}"
66
- )
67
- else:
68
- system_instruction = (
69
- "You are a news editor. Translate complex text into simple English. Respond EXCLUSIVELY in valid JSON format.\n"
70
- "JSON Format:\n"
71
- '{\n "summary_bullets": ["bullet point 1", "bullet point 2"]\n}'
72
- )
 
 
73
 
74
- # B. Context Window Token Budgeting (Standard HF Tokenizer)
75
- tokenizer = chat_pipeline.tokenizer
76
- sys_tokens = len(tokenizer.encode(system_instruction))
77
- user_tokens = len(tokenizer.encode(user_input))
78
- max_context_limit = 2048
79
-
80
- # Dynamic truncation if user input exceeds available token budget
81
- if (sys_tokens + user_tokens + int(max_tokens)) > max_context_limit:
82
- allowed_user_tokens = max_context_limit - sys_tokens - int(max_tokens) - 30
83
- truncated_ids = tokenizer.encode(user_input)[:allowed_user_tokens]
84
- user_input = tokenizer.decode(truncated_ids)
85
- logger.warning("User input truncated to satisfy context window token budget.")
86
-
87
- # C. Construct Official Model Prompt
88
- messages = [
89
- {"role": "system", "content": system_instruction},
90
- {"role": "user", "content": user_input}
91
- ]
92
- prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
93
-
94
- # D. Run Model Inference
95
  raw_output = chat_pipeline(
96
- prompt,
97
  max_new_tokens=int(max_tokens),
98
  do_sample=True,
99
  temperature=float(temp),
100
- top_p=0.85,
101
- repetition_penalty=1.1
 
102
  )
 
 
 
 
 
 
 
 
 
 
103
 
104
- generated_text = raw_output[0]["generated_text"][len(prompt):].strip()
105
-
106
- # E. Extract and Parse Structured JSON Output
107
- clean_json_str = generated_text
108
- if "```json" in clean_json_str:
109
- clean_json_str = clean_json_str.split("```json")[1].split("```")[0].strip()
110
- elif "```" in clean_json_str:
111
- clean_json_str = clean_json_str.split("```")[1].split("```")[0].strip()
112
-
113
- data = json.loads(clean_json_str)
114
-
115
- # F. Format Final Response
116
- if workspace_type == "debate":
117
- counter = data.get("counter_argument", generated_text)
118
- score = data.get("logic_score", "N/A")
119
- return (
120
- f"### ๐ŸŽ™๏ธ Adversarial Counter-Argument\n{counter}\n\n"
121
- f"**Argument Logic Score:** {score}/10 \n"
122
- f"**Vector Similarity Score:** `{similarity_score:.2f}`"
123
- )
124
- else:
125
- bullets = data.get("summary_bullets", [])
126
- bullet_fmt = "\n".join([f"* {b}" for b in bullets]) if isinstance(bullets, list) else str(bullets)
127
- return f"### ๐Ÿ“ฐ Executive Summary\n{bullet_fmt}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
 
129
- except json.JSONDecodeError:
130
- # Fallback handling if JSON formatting had minor syntax issues
131
- return f"### ๐Ÿ–ฅ๏ธ Model Generation Output\n{generated_text}"
132
- except Exception as e:
133
- return f"โŒ **Execution Fault:** {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
 
135
 
136
  # ==========================================
137
- # 3. GRADIO USER INTERFACE
138
  # ==========================================
139
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
140
- gr.Markdown("# ๐Ÿค– Simplified AI News & Debate Agent")
141
- gr.Markdown("Lightweight RAG Engine featuring explicit Token Budgeting, NumPy Vector Search, and JSON Structured Output.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
 
 
143
  with gr.Row():
144
- with gr.Column():
145
- mode = gr.Radio(["debate", "generalize"], value="debate", label="Workspace Engine Mode")
146
- inp = gr.Textbox(lines=4, label="Input Prompt / Claim", placeholder="Enter your stance or article snippet...")
147
- max_tok = gr.Slider(64, 256, value=120, step=16, label="Max Response Tokens")
148
- temp = gr.Slider(0.1, 1.0, value=0.2, step=0.05, label="Temperature")
149
- submit_btn = gr.Button("Deploy Request", variant="primary")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
 
151
- with gr.Column():
152
- out = gr.Markdown("### ๐Ÿ–ฅ๏ธ Response Output\n*Awaiting execution...*")
 
 
 
 
 
 
153
 
154
- submit_btn.click(execution_engine, inputs=[mode, inp, max_tok, temp], outputs=out)
 
 
 
 
155
 
156
  if __name__ == "__main__":
157
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
 
1
  import gradio as gr
2
+ import torch
3
  from transformers import pipeline
4
+ from langchain_community.vectorstores import FAISS
5
+ from langchain_huggingface import HuggingFaceEmbeddings
6
+ from langchain_core.documents import Document
7
 
8
  # ==========================================
9
+ # 1. INITIALIZE ENGINE & VECTOR DATA
10
  # ==========================================
11
+ print("Loading Lightweight Embedding Engine...")
12
+ embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
13
 
14
+ print("Initializing High-Performance Qwen Core on CPU...")
15
+ chat_pipeline = pipeline(
16
+ "text-generation",
17
+ model="Qwen/Qwen2.5-1.5B-Instruct",
18
+ device="cpu",
19
+ dtype=torch.float32
20
+ )
21
 
22
+ # Fact-checking RAG context database
23
+ mock_news_db = [
24
  "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.",
25
  "EV Industrial Lifecycle: Electric Vehicles emit zero greenhouse operational gases, but lithium-ion battery extraction increases heavy localized ecosystem toxicity by 42%.",
26
  "Universal Basic Income: Comprehensive economic trials indicate UBI successfully lowers base poverty indexes but sparks minor macro-inflationary pressures across basic retail commodities."
27
  ]
28
+ documents = [Document(page_content=text) for text in mock_news_db]
29
+ 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],
319
+ outputs=active_output_panel
320
+ )
321
 
322
+ run_simplifier_action.click(
323
+ fn=lambda user_in, tokens, t, p: execution_engine("generalize", user_in, tokens, t, p),
324
+ inputs=[simplify_text_input, max_tokens_val, temp_val, top_p_val],
325
+ outputs=active_output_panel
326
+ )
327
 
328
  if __name__ == "__main__":
329
  demo.launch(server_name="0.0.0.0", server_port=7860)