VectorV17 commited on
Commit
8c82254
Β·
verified Β·
1 Parent(s): 3b75f3b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -93
app.py CHANGED
@@ -1,6 +1,5 @@
1
- import time
2
- import torch
3
  import gradio as gr
 
4
  from transformers import pipeline
5
  from langchain_community.vectorstores import FAISS
6
  from langchain_huggingface import HuggingFaceEmbeddings
@@ -13,7 +12,6 @@ print("Loading Lightweight Embedding Engine...")
13
  embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
14
 
15
  print("Initializing High-Performance Qwen Core on CPU...")
16
- # PyTorch CPU Inference Engine Execution
17
  chat_pipeline = pipeline(
18
  "text-generation",
19
  model="Qwen/Qwen2.5-1.5B-Instruct",
@@ -32,54 +30,44 @@ vector_store = FAISS.from_documents(documents, embedding_model)
32
 
33
 
34
  # ==========================================
35
- # 2. INFERENCE ENGINE WITH METADATA & INSIGHTS
36
  # ==========================================
37
  def execution_engine(workspace_type, user_input, max_tokens, temp, top_p):
38
  if not user_input.strip():
39
  return "⚠️ **System Input Error:** Workspace field cannot be processed empty."
40
 
41
- start_time = time.time()
42
-
43
- # 1. System Prompt Engineering & Schema Enforcement
44
  if workspace_type == "debate":
45
  retriever = vector_store.as_retriever(search_kwargs={"k": 1})
46
  matched_docs = retriever.invoke(user_input)
47
  context = matched_docs[0].page_content if matched_docs else "No specific context available."
48
 
49
  system_instruction = (
50
- "You are an enterprise adversarial debate intelligence assistant. Disagree politely with the user's stance.\n"
51
- f"VERIFIED BACKGROUND CONTEXT: {context}\n\n"
52
- "Format your answer EXACTLY into these sections:\n"
53
- "### βš–οΈ Counter-Argument\n"
54
- "[Direct, concise rebuttal under 3 sentences]\n\n"
55
- "### πŸ“Š Logic Evaluation\n"
56
- "**Argument Logic Score:** [X/10]\n\n"
57
- "### 🏷️ Categorized Insights\n"
58
- "- **Factual Alignment:** [Brief note on data backing]\n"
59
- "- **Fallacy/Risk Alert:** [Key vulnerability in the stance]"
60
  )
61
- else: # Generalize / Summary mode
62
  system_instruction = (
63
- "You are an executive news summarization agent. Translate complex statements into plain English.\n\n"
64
- "Format your answer EXACTLY into these sections:\n"
65
- "### πŸ“ Executive Summary\n"
66
- "* [Key point 1]\n"
67
- "* [Key point 2]\n\n"
68
- "### 🏷️ Categorized Insights\n"
69
- "- **Core Takeaway:** [One-sentence main point]\n"
70
- "- **Strategic Impact:** [Market or operational implications]\n"
71
- "- **Risk & Mitigation:** [Identified risk factor]"
72
  )
73
 
74
- # 2. ChatML Prompt Template Construction
75
  formatted_prompt = (
76
  f"<|im_start|>system\n{system_instruction}\n"
77
- "CRITICAL: Adhere strictly to the requested structured output format. Complete your final sentence.<|im_end|>\n"
78
  f"<|im_start|>user\n{user_input}<|im_end|>\n"
79
  "<|im_start|>assistant\n"
80
  )
81
 
82
- # 3. CPU Inference Execution Pipeline
83
  try:
84
  raw_output = chat_pipeline(
85
  formatted_prompt,
@@ -93,78 +81,52 @@ def execution_engine(workspace_type, user_input, max_tokens, temp, top_p):
93
 
94
  processed_text = raw_output[0]["generated_text"].split("<|im_start|>assistant\n")[-1].strip()
95
 
96
- # Sentence clean-up guardrail
97
  if processed_text and processed_text[-1] not in [".", "!", "?", '"', "*"]:
98
  last_punctuation = max(processed_text.rfind("."), processed_text.rfind("!"), processed_text.rfind("?"))
99
  if last_punctuation != -1:
100
  processed_text = processed_text[:last_punctuation + 1]
101
-
102
- # Calculate Execution Metadata Metrics
103
- latency = round(time.time() - start_time, 2)
104
- prompt_tokens = len(formatted_prompt.split())
105
- output_tokens = len(processed_text.split())
106
- total_tokens = prompt_tokens + output_tokens
107
-
108
- # Enterprise System Analytics Metadata Block
109
- metadata_block = (
110
- "\n\n---\n"
111
- "### ⚑ Runtime Analytics & Inference Metadata\n"
112
- f"- **Inference Engine:** PyTorch CPU Direct Pipeline (`Qwen2.5-1.5B-Instruct`)\n"
113
- f"- **Context Window Capacity:** 32,768 Tokens *(Used: {total_tokens} tokens)*\n"
114
- f"- **Prompt-Level KV Caching:** Enabled *(Zero-copy Key-Value state reuse)*\n"
115
- f"- **Structured Output Schema:** Enforced ChatML Schema Validation\n"
116
- f"- **Execution Latency:** `{latency}s` | **Generation Speed:** ~{round(output_tokens/max(latency, 0.01), 1)} tokens/sec"
117
- )
118
-
119
- return processed_text + metadata_block
120
 
121
  except Exception as error:
122
  return f"❌ **Pipeline Fault:** Execution error on CPU node. Details: {str(error)}"
123
 
124
 
125
  # ==========================================
126
- # 3. MODULAR UI DASHBOARD (ENTERPRISE PRODUCT SUITE)
127
  # ==========================================
128
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
129
-
130
- # Hero Title Header
131
- gr.Markdown("# πŸ€– Enterprise AI News Debate & Summary Agent Suite")
132
- gr.Markdown(
133
- "A low-latency, modular NLP workstation engineered for adversarial debate logic and automated "
134
- "complex-news generalization. Optimized for high-throughput CPU inference with zero truncation errors."
135
- )
136
 
137
- # Key Performance Indicator (KPI) Banner (Inspired by Viso.ai layout)
138
  with gr.Row():
 
 
 
 
 
 
 
 
 
 
 
 
139
  with gr.Column(scale=1):
140
- gr.Markdown("### 32,768\n**Context Window Limit**")
141
- with gr.Column(scale=1):
142
- gr.Markdown("### < 1.8s\n**Average CPU Latency**")
143
- with gr.Column(scale=1):
144
- gr.Markdown("### Prompt KV-Cache\n**State Reuse Engine**")
145
- with gr.Column(scale=1):
146
- gr.Markdown("### 100% Schema\n**Structured Output Compliance**")
147
 
148
  gr.HTML("<hr style='border: 0.5px solid #374151; margin: 15px 0;'/>")
149
 
150
- # Product Capability Feature Grid (Inspired by HappyOrNot & DOC HAWK cards)
151
- gr.Markdown("### 🌟 Platform Enterprise Capabilities")
152
- with gr.Row():
153
- with gr.Column(scale=1):
154
- gr.Markdown("#### πŸ” Context Verification\nExtracts factual context from vector databases to verify user claims.")
155
- with gr.Column(scale=1):
156
- gr.Markdown("#### πŸ“Š Logic Evaluation\nScores debate arguments out of 10 based on structure and factual accuracy.")
157
- with gr.Column(scale=1):
158
- gr.Markdown("#### 🏷️ Categorized Insights\nBreaks outputs into strategic takeaways, risk alerts, and operational impacts.")
159
- with gr.Column(scale=1):
160
- gr.Markdown("#### ⚑ Optimized Inference\nLeverages prompt-level KV caching and low-memory micro-models on CPU.")
161
-
162
- gr.HTML("<hr style='border: 0.5px solid #374151; margin: 20px 0;'/>")
163
-
164
- # Main Workspace Area
165
  with gr.Row():
166
 
167
- # Left Panel Input Controls
168
  with gr.Column(scale=1):
169
  gr.Markdown("### πŸ› οΈ Agent Workspaces")
170
 
@@ -174,8 +136,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
174
  with gr.Tab(label="πŸŽ™οΈ News Debate Agent", id=0):
175
  gr.Markdown("*State a topic and your stance. The agent will pull factual context, debate you, and score your logic.*")
176
 
 
177
  debate_example_picker = gr.Dropdown(
178
- label="πŸ’‘ Preset Debate Topics",
179
  choices=[
180
  "LLMs are indispensable for application development.",
181
  "Electric vehicles are the only way to save the environment.",
@@ -185,18 +148,21 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
185
 
186
  debate_text_input = gr.Textbox(
187
  label="Your Stance",
188
- placeholder="Select or type your claim here..."
189
  )
190
 
 
191
  debate_example_picker.change(fn=lambda x: x, inputs=debate_example_picker, outputs=debate_text_input)
 
192
  run_debate_action = gr.Button("Deploy Counter-Argument & Score", variant="primary")
193
 
194
  # Tab (ii): Text Simplification Module
195
  with gr.Tab(label="πŸ“° Snippet Generalizer", id=1):
196
- gr.Markdown("*Paste complex, jargon-heavy news columns here to output plain-English executive summaries.*")
197
 
 
198
  generalizer_example_picker = gr.Dropdown(
199
- label="πŸ’‘ Preset News Snippets",
200
  choices=[
201
  "Despite aggressive macroeconomic headwinds and structural quantitative tightening by central banking systems, retail index trends demonstrated non-linear elasticity during fiscal evaluations.",
202
  "The ecosystem validation vectors indicate substantial degradation in sub-surface lithospheric layers owing to chemical manufacturing runoffs, sparking compliance friction."
@@ -211,22 +177,25 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
211
  placeholder="Paste text block or official reports here..."
212
  )
213
 
 
214
  generalizer_example_picker.change(fn=lambda x: x, inputs=generalizer_example_picker, outputs=simplify_text_input)
215
- run_simplifier_action = gr.Button("Deploy Generalization Engine", variant="primary")
 
216
 
217
  gr.HTML("<div style='margin-bottom: 20px;'></div>")
 
218
 
219
- # Param Configuration Accordion
220
  with gr.Accordion("βš™οΈ Runtime Parameter Tuning", open=False):
221
- max_tokens_val = gr.Slider(minimum=64, maximum=300, value=180, step=16, label="Max Tokens")
222
  temp_val = gr.Slider(minimum=0.1, maximum=1.0, value=0.25, step=0.05, label="Temperature (Low = Deterministic)")
223
  top_p_val = gr.Slider(minimum=0.50, maximum=1.00, value=0.85, step=0.05, label="Top-p")
224
 
225
- # Right Panel Output Stream
226
  with gr.Column(scale=1):
227
  gr.Markdown("### πŸ–₯️ Active Output Stream")
228
  active_output_panel = gr.Markdown(
229
- value="*System ready. Select a workspace on the left panel and click deploy to review output and metadata analytics.*"
230
  )
231
 
232
  # ==========================================
@@ -245,4 +214,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
245
  )
246
 
247
  if __name__ == "__main__":
248
- 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
 
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",
 
30
 
31
 
32
  # ==========================================
33
+ # 2. FEW-SHOT PROMPT COMPILER
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
+ # 1. Structural Instructions + Few-Shot Examples (The Quality Anchors)
 
 
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
+ # 2. 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
+ # 3. CPU Execution Pipeline
71
  try:
72
  raw_output = chat_pipeline(
73
  formatted_prompt,
 
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. MODULAR UI DASHBOARD (GRADIO 6.0)
98
  # ==========================================
99
+ with gr.Blocks() as demo:
 
 
 
 
 
 
 
100
 
101
+ # Header Module
102
  with gr.Row():
103
+ with gr.Column(scale=3):
104
+ gr.Markdown("# πŸ€– AI News Debate & Summary Agent Suite")
105
+ gr.Markdown(
106
+ "**About This Agent:** This framework utilizes localized vector embeddings alongside "
107
+ "few-shot prompting to anchor small model reasoning. It delivers deterministic, "
108
+ "concise outputs optimized for zero-error execution on base CPU clusters. "
109
+ "This framework processes user statements and news artifacts to provide dual NLP functions: "
110
+ "*Adversarial Debate Environment* using real-time vectorized context verification to engage with users "
111
+ "and *Automated Generalization Engine* for complex-news summarization and analysis."
112
+ )
113
+
114
+ # Status Module Panel
115
  with gr.Column(scale=1):
116
+ gr.HighlightedText(value=[("ONLINE", "🟒 NLP Match Engine Status")], show_label=False)
117
+ gr.Markdown(
118
+ "πŸͺͺ **Agent Profile:** Few-Shot Context Assistant \n"
119
+ "πŸ“¦ **Model Matrix:** Qwen2.5-1.5B-Instruct \n"
120
+ "🧬 **Vectorization Engine:** Text FAISS Engine \n"
121
+ "πŸ’» **Processing Hardware:** Base CPU Tier"
122
+ )
123
 
124
  gr.HTML("<hr style='border: 0.5px solid #374151; margin: 15px 0;'/>")
125
 
126
+ # Lower Workspace Splitting Layout
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  with gr.Row():
128
 
129
+ # Left Panel Workspace
130
  with gr.Column(scale=1):
131
  gr.Markdown("### πŸ› οΈ Agent Workspaces")
132
 
 
136
  with gr.Tab(label="πŸŽ™οΈ News Debate Agent", id=0):
137
  gr.Markdown("*State a topic and your stance. The agent will pull factual context, debate you, and score your logic.*")
138
 
139
+ # Robust Example System: Swapped to an immune dropdown component to bypass the hidden tab rendering bug
140
  debate_example_picker = gr.Dropdown(
141
+ label="πŸ’‘ Suggestions for Preset Debate Topic",
142
  choices=[
143
  "LLMs are indispensable for application development.",
144
  "Electric vehicles are the only way to save the environment.",
 
148
 
149
  debate_text_input = gr.Textbox(
150
  label="Your Stance",
151
+ placeholder="Select or paste your claim here..."
152
  )
153
 
154
+ # Dynamically push selected dropdown choice into the text input instantly
155
  debate_example_picker.change(fn=lambda x: x, inputs=debate_example_picker, outputs=debate_text_input)
156
+
157
  run_debate_action = gr.Button("Deploy Counter-Argument & Score", variant="primary")
158
 
159
  # Tab (ii): Text Simplification Module
160
  with gr.Tab(label="πŸ“° Snippet Generalizer", id=1):
161
+ gr.Markdown("*Paste complex, jargon-heavy news columns here to output plain-English, executive summaries.*")
162
 
163
+ # Robust Example System: Swapped to an immune dropdown component to bypass the hidden tab rendering bug
164
  generalizer_example_picker = gr.Dropdown(
165
+ label="πŸ’‘ Examples for Preset Complex News",
166
  choices=[
167
  "Despite aggressive macroeconomic headwinds and structural quantitative tightening by central banking systems, retail index trends demonstrated non-linear elasticity during fiscal evaluations.",
168
  "The ecosystem validation vectors indicate substantial degradation in sub-surface lithospheric layers owing to chemical manufacturing runoffs, sparking compliance friction."
 
177
  placeholder="Paste text block or official reports here..."
178
  )
179
 
180
+ # Dynamically push selected dropdown choice into the text input instantly
181
  generalizer_example_picker.change(fn=lambda x: x, inputs=generalizer_example_picker, outputs=simplify_text_input)
182
+
183
+ run_simplifier_action = gr.Button("Deploy for generalization system", variant="primary")
184
 
185
  gr.HTML("<div style='margin-bottom: 20px;'></div>")
186
+
187
 
188
+ # Param Configuration Module (Hidden Tuning to keep CPU fast and reliable)
189
  with gr.Accordion("βš™οΈ Runtime Parameter Tuning", open=False):
190
+ max_tokens_val = gr.Slider(minimum=64, maximum=256, value=140, step=16, label="Max tokens")
191
  temp_val = gr.Slider(minimum=0.1, maximum=1.0, value=0.25, step=0.05, label="Temperature (Low = Deterministic)")
192
  top_p_val = gr.Slider(minimum=0.50, maximum=1.00, value=0.85, step=0.05, label="Top-p")
193
 
194
+ # Right Panel Output Monitor
195
  with gr.Column(scale=1):
196
  gr.Markdown("### πŸ–₯️ Active Output Stream")
197
  active_output_panel = gr.Markdown(
198
+ value="*System ready. Select a workspace on the left panel and click deploy to review parsed generation stream.*"
199
  )
200
 
201
  # ==========================================
 
214
  )
215
 
216
  if __name__ == "__main__":
217
+ demo.launch(server_name="0.0.0.0", server_port=7860, theme=gr.themes.Soft())