VectorV17 commited on
Commit
fb50d42
·
verified ·
1 Parent(s): 1fe83c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +208 -173
app.py CHANGED
@@ -1,25 +1,31 @@
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%.",
@@ -30,188 +36,217 @@ vector_store = FAISS.from_documents(documents, embedding_model)
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,
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. 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
-
133
- with gr.Tabs() as active_workspace_tabs:
134
-
135
- # Tab (i): Debate Module
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.",
145
- "Universal Basic Income will just make people lazy."
146
- ]
147
- )
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."
169
- ],
170
- value="Despite aggressive macroeconomic headwinds and structural quantitative tightening by central banking systems, retail index trends demonstrated non-linear elasticity during fiscal evaluations."
171
- )
172
-
173
- simplify_text_input = gr.Textbox(
174
- label="Complex News Snippet",
175
- lines=4,
176
- value="Despite aggressive macroeconomic headwinds and structural quantitative tightening by central banking systems, retail index trends demonstrated non-linear elasticity during fiscal evaluations.",
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
- # ==========================================
202
- # 4. EVENT CONTROLLERS
203
- # ==========================================
204
- run_debate_action.click(
205
- fn=lambda user_in, tokens, t, p: execution_engine("debate", user_in, tokens, t, p),
206
- inputs=[debate_text_input, max_tokens_val, temp_val, top_p_val],
207
- outputs=active_output_panel
208
- )
209
-
210
- run_simplifier_action.click(
211
- fn=lambda user_in, tokens, t, p: execution_engine("generalize", user_in, tokens, t, p),
212
- inputs=[simplify_text_input, max_tokens_val, temp_val, top_p_val],
213
- outputs=active_output_panel
214
- )
215
-
216
- if __name__ == "__main__":
217
- demo.launch(server_name="0.0.0.0", server_port=7860, theme=gr.themes.Soft())
 
1
+ import json
2
+ import time
3
  import torch
4
+ import gradio as gr
5
+ from transformers import AutoModelForCausalLM, AutoTokenizer
6
  from langchain_community.vectorstores import FAISS
7
  from langchain_huggingface import HuggingFaceEmbeddings
8
  from langchain_core.documents import Document
9
 
10
  # ==========================================
11
+ # 1. ENGINE & VECTOR DATABASE INITIALIZATION
12
  # ==========================================
13
+ MODEL_ID = "Qwen/Qwen2.5-1.5B-Instruct"
14
+
15
+ print("Loading Embedding Engine...")
16
  embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
17
 
18
+ print(f"Loading Model & Tokenizer ({MODEL_ID}) on CPU...")
19
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
20
+ model = AutoModelForCausalLM.from_pretrained(
21
+ MODEL_ID,
22
+ torch_dtype=torch.float32,
23
+ device_map="cpu",
24
+ low_cpu_mem_usage=True
25
  )
26
+ model.eval()
27
 
28
+ # Local RAG Knowledge Base
29
  mock_news_db = [
30
  "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.",
31
  "EV Industrial Lifecycle: Electric Vehicles emit zero greenhouse operational gases, but lithium-ion battery extraction increases heavy localized ecosystem toxicity by 42%.",
 
36
 
37
 
38
  # ==========================================
39
+ # 2. CORE EXECUTION ENGINE WITH STRUCTURED SCHEMAS
40
  # ==========================================
41
  def execution_engine(workspace_type, user_input, max_tokens, temp, top_p):
42
  if not user_input.strip():
43
+ return "⚠️ **System Input Error:** Workspace field cannot be empty."
44
 
45
+ start_time = time.time()
46
+
47
+ # 1. Build System Instructions & Define Output Schemas
48
  if workspace_type == "debate":
49
  retriever = vector_store.as_retriever(search_kwargs={"k": 1})
50
  matched_docs = retriever.invoke(user_input)
51
+ context = matched_docs[0].page_content if matched_docs else "No context found."
52
+
53
  system_instruction = (
54
+ "You are an adversarial debate assistant. Analyze the user's stance using the context provided.\n"
55
+ f"Context: {context}\n\n"
56
+ "Return your response ONLY as a valid JSON object matching this schema:\n"
57
+ "{\n"
58
+ ' "counter_argument": "string (1-2 sentences)",\n'
59
+ ' "logic_score": integer (1 to 10),\n'
60
+ ' "factual_alignment": "string"\n'
61
+ "}"
62
  )
63
+ else: # Summarization Mode
64
  system_instruction = (
65
+ "You are a news summarization assistant. Translate complex text into plain English.\n\n"
66
+ "Return your response ONLY as a valid JSON object matching this schema:\n"
67
+ "{\n"
68
+ ' "summary_points": ["bullet 1", "bullet 2"],\n'
69
+ ' "core_takeaway": "string"\n'
70
+ "}"
71
  )
72
 
73
+ # 2. Official ChatML Format using HuggingFace Tokenizer API
74
+ messages = [
75
+ {"role": "system", "content": system_instruction},
76
+ {"role": "user", "content": user_input}
77
+ ]
78
+
79
+ formatted_prompt = tokenizer.apply_chat_template(
80
+ messages,
81
+ tokenize=False,
82
+ add_generation_prompt=True
83
  )
84
 
85
+ # Token Counting
86
+ input_ids = tokenizer(formatted_prompt, return_tensors="pt").input_ids
87
+ prompt_token_count = input_ids.shape[1]
88
+
89
+ # 3. Optimized Execution Block
90
  try:
91
+ with torch.inference_mode():
92
+ output_tokens = model.generate(
93
+ input_ids,
94
+ max_new_tokens=int(max_tokens),
95
+ do_sample=True,
96
+ temperature=float(temp),
97
+ top_p=float(top_p),
98
+ repetition_penalty=1.1,
99
+ use_cache=True,
100
+ pad_token_id=tokenizer.eos_token_id
101
+ )
102
+
103
+ # Extract generated tokens
104
+ generated_ids = output_tokens[0][prompt_token_count:]
105
+ raw_output_text = tokenizer.decode(generated_ids, skip_special_tokens=True).strip()
106
 
107
+ latency = round(time.time() - start_time, 2)
108
+ gen_token_count = len(generated_ids)
109
+
110
+ # 4. JSON Output Parsing & Schema Validation
111
+ try:
112
+ clean_json_str = raw_output_text.replace("```json", "").replace("```", "").strip()
113
+ parsed_data = json.loads(clean_json_str)
114
+
115
+ if workspace_type == "debate":
116
+ formatted_response = (
117
+ f"### ⚖️ Counter-Argument\n{parsed_data.get('counter_argument', 'N/A')}\n\n"
118
+ f"**Argument Logic Score:** `{parsed_data.get('logic_score', 'N/A')}/10`\n\n"
119
+ f"**Factual Alignment:** {parsed_data.get('factual_alignment', 'N/A')}"
120
+ )
121
+ else:
122
+ bullets = "\n".join([f"* {p}" for p in parsed_data.get('summary_points', [])])
123
+ formatted_response = (
124
+ f"### 📝 Executive Summary\n{bullets}\n\n"
125
+ f"**Core Takeaway:** {parsed_data.get('core_takeaway', 'N/A')}"
126
+ )
127
+ except json.JSONDecodeError:
128
+ formatted_response = f"⚠️ *JSON Parsing Fallback (Raw Model Output):*\n\n{raw_output_text}"
129
+
130
+ # 5. Technical Metadata Metrics Block
131
+ metrics = (
132
+ "\n\n---\n"
133
+ "### 📊 Actual Execution Metrics\n"
134
+ f"- **Prompt Tokens:** `{prompt_token_count}` | **Generated Tokens:** `{gen_token_count}`\n"
135
+ f"- **Execution Latency:** `{latency}s` (~{round(gen_token_count/max(latency, 0.01), 1)} tokens/sec)\n"
136
+ f"- **Autoregressive KV Cache:** Active (`use_cache=True`)\n"
137
+ f"- **Context Limit:** 32,768 tokens (Utilization: `{round((prompt_token_count + gen_token_count)/32768 * 100, 2)}%`)"
138
+ )
139
+
140
+ return formatted_response + metrics
141
 
142
  except Exception as error:
143
+ return f"❌ **Pipeline Fault:** Execution error. Details: {str(error)}"
144
 
145
 
146
  # ==========================================
147
+ # 3. CUSTOM INJECTED CSS STYLING
148
  # ==========================================
149
+ custom_css = """
150
+ body, .gradio-container {
151
+ background-color: #0D1117 !important;
152
+ font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif !important;
153
+ color: #E6EDF3 !important;
154
+ }
155
+
156
+ /* Header Navbar */
157
+ .app-header {
158
+ background-color: #161B22;
159
+ padding: 16px 28px;
160
+ border-bottom: 1px solid #30363D;
161
+ margin-bottom: 24px;
162
+ border-radius: 8px;
163
+ display: flex;
164
+ justify-content: space-between;
165
+ align-items: center;
166
+ }
167
+ .app-title {
168
+ font-size: 22px;
169
+ font-weight: 700;
170
+ color: #FFFFFF;
171
+ margin: 0;
172
+ }
173
+ .app-subtitle {
174
+ font-size: 13px;
175
+ color: #8B949E;
176
+ margin-top: 4px;
177
+ }
178
+
179
+ /* Webtures-inspired KPI Dashboard Cards */
180
+ .kpi-container {
181
+ display: grid;
182
+ grid-template-columns: repeat(4, 1fr);
183
+ gap: 16px;
184
+ margin-bottom: 24px;
185
+ }
186
+ .kpi-card {
187
+ background: #161B22;
188
+ border: 1px solid #30363D;
189
+ border-radius: 10px;
190
+ padding: 18px;
191
+ text-align: left;
192
+ }
193
+ .kpi-value {
194
+ font-size: 28px;
195
+ font-weight: 800;
196
+ color: #58A6FF;
197
+ line-height: 1.1;
198
+ }
199
+ .kpi-label {
200
+ font-size: 12px;
201
+ font-weight: 600;
202
+ text-transform: uppercase;
203
+ color: #8B949E;
204
+ margin-top: 6px;
205
+ }
206
+
207
+ /* DOC HAWK / HappyOrNot-inspired Capability Grid */
208
+ .feature-grid {
209
+ display: grid;
210
+ grid-template-columns: repeat(4, 1fr);
211
+ gap: 16px;
212
+ margin-bottom: 28px;
213
+ }
214
+ .feature-card {
215
+ background: #161B22;
216
+ border-left: 4px solid #1F6FE5;
217
+ border-radius: 6px;
218
+ padding: 16px;
219
+ }
220
+ .feature-title {
221
+ font-weight: 600;
222
+ font-size: 14px;
223
+ color: #F0F6FC;
224
+ margin-bottom: 6px;
225
+ }
226
+ .feature-desc {
227
+ font-size: 12px;
228
+ color: #8B949E;
229
+ line-height: 1.4;
230
+ }
231
+
232
+ /* Custom Styled Input Action Buttons */
233
+ button.primary-btn {
234
+ background: #238636 !important;
235
+ border: 1px solid rgba(240, 246, 252, 0.1) !important;
236
+ color: #FFFFFF !important;
237
+ font-weight: 600 !important;
238
+ border-radius: 6px !important;
239
+ transition: all 0.2s ease !important;
240
+ }
241
+ button.primary-btn:hover {
242
+ background: #2EA043 !important;
243
+ }
244
+ """
245
 
246
+ # ==========================================
247
+ # 4. INDUSTRY-GRADE GRADIO UI DASHBOARD
248
+ # ==========================================
249
+ with gr.Blocks(css=custom_css, title="AI News Debate & Summary Suite") as demo:
250
+
251
+ # 1. Header Navigation Bar
252
+ gr.HTML("""