File size: 14,053 Bytes
45d3092
 
 
 
 
 
 
 
 
 
 
8c82254
fb50d42
45d3092
771f726
 
 
 
45d3092
 
 
 
 
 
771f726
45d3092
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
598b09a
45d3092
 
 
 
 
 
 
598b09a
8f9986a
45d3092
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
771f726
45d3092
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb50d42
771f726
45d3092
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb50d42
 
45d3092
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb50d42
 
45d3092
 
 
 
 
 
 
 
 
8f9986a
 
45d3092
 
 
8f9986a
45d3092
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
771f726
45d3092
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5e95f0
45d3092
 
 
c5e95f0
45d3092
 
c5e95f0
45d3092
 
 
 
 
 
 
 
 
 
c5e95f0
 
 
 
 
 
 
45d3092
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5e95f0
 
 
45d3092
 
 
 
 
 
 
 
 
 
 
 
c5e95f0
45d3092
 
 
 
 
 
 
 
 
 
 
c5e95f0
 
45d3092
 
 
c5e95f0
45d3092
 
 
 
 
c5e95f0
5fb7f26
45d3092
 
 
 
c5e95f0
 
45d3092
 
 
c5e95f0
45d3092
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"""
AI News Debate & Summary Agent Suite
----------------------------------
An enterprise-grade, lightweight RAG & Few-Shot NLP application designed for 
Hugging Face Spaces CPU Basic instances.
"""

import logging
import re
from typing import Optional

import torch
import gradio as gr
from transformers import pipeline
from langchain_community.vectorstores import FAISS
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_core.documents import Document

# ==============================================================================
# 1. LOGGING & SYSTEM CONFIGURATION
# ==============================================================================
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [%(levelname)s] %(name)s - %(message)s"
)
logger = logging.getLogger("AIAgentSuite")


# ==============================================================================
# 2. SINGLETON RESOURCE & ENGINE REGISTRY
# ==============================================================================
class EngineRegistry:
    """
    Singleton registry managing model loading, pipeline state, 
    and FAISS vector stores to maximize memory efficiency on CPU Basic hardware.
    """
    _instance: Optional["EngineRegistry"] = None

    def __init__(self) -> None:
        logger.info("Initializing Lightweight Sentence-Transformer Embedding Engine...")
        self.embedding_model = HuggingFaceEmbeddings(
            model_name="sentence-transformers/all-MiniLM-L6-v2"
        )

        logger.info("Initializing High-Performance Qwen2.5-1.5B Core on CPU...")
        self.chat_pipeline = pipeline(
            "text-generation",
            model="Qwen/Qwen2.5-1.5B-Instruct",
            device="cpu",
            torch_dtype=torch.float32
        )

        logger.info("Indexing Fact-Checking RAG Context Database into FAISS...")
        mock_news_db = [
            "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.",
            "EV Industrial Lifecycle: Electric Vehicles emit zero greenhouse operational gases, but lithium-ion battery extraction increases heavy localized ecosystem toxicity by 42%.",
            "Universal Basic Income: Comprehensive economic trials indicate UBI successfully lowers base poverty indexes but sparks minor macro-inflationary pressures across basic retail commodities."
        ]
        documents = [Document(page_content=text) for text in mock_news_db]
        self.vector_store = FAISS.from_documents(documents, self.embedding_model)
        logger.info("Engine Registry successfully loaded into system memory.")

    @classmethod
    def get_instance(cls) -> "EngineRegistry":
        if cls._instance is None:
            cls._instance = cls()
        return cls._instance


# ==============================================================================
# 3. CORE EXECUTION & PROMPT COMPILER
# ==============================================================================
def execution_engine(
    workspace_type: str,
    user_input: str,
    max_tokens: int,
    temp: float,
    top_p: float
) -> str:
    """
    Processes workspace queries through vector retrieval, ChatML prompt compilation, 
    and output post-processing.
    """
    if not user_input or not user_input.strip():
        return "⚠️ **System Input Error:** Workspace field cannot be processed empty."

    try:
        registry = EngineRegistry.get_instance()

        # Workspace Routing & Prompt Engineering
        if workspace_type == "debate":
            retriever = registry.vector_store.as_retriever(search_kwargs={"k": 1})
            matched_docs = retriever.invoke(user_input)
            context = matched_docs[0].page_content if matched_docs else "No specific context available."

            system_instruction = (
                "You are a sharp, polite adversarial debate assistant. Disagree with the user's stance. "
                "Provide exactly one paragraph of logical counter-argument, then provide a score out of 10.\n\n"
                f"VERIFIED CONTEXT TO USE: {context}\n\n"
                "### GOOD EXAMPLE RESPONSE FORMAT:\n"
                "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"
                "**Argument Logic Score: 6/10**"
            )
        else:  # Generalize mode
            system_instruction = (
                "You are an expert news editor. Translate complex, jargon-heavy statements into plain, simple English. "
                "Output your answer as clear, high-level summary bullet points under 50 words total.\n\n"
                "### GOOD EXAMPLE RESPONSE FORMAT:\n"
                "* Global markets fell sharply due to sudden central bank interest rate changes.\n"
                "* Local manufacturing activity slowed down to an all-time low."
            )

        # Qwen Official ChatML Formatting
        formatted_prompt = (
            f"<|im_start|>system\n{system_instruction}\n"
            "CRITICAL: Keep your response concise. Do not ramble. Complete your final sentence perfectly.<|im_end|>\n"
            f"<|im_start|>user\n{user_input}<|im_end|>\n"
            "<|im_start|>assistant\n"
        )

        # CPU Pipeline Execution
        raw_output = registry.chat_pipeline(
            formatted_prompt,
            max_new_tokens=int(max_tokens),
            do_sample=True,
            temperature=float(temp),
            top_p=float(top_p),
            top_k=40,
            repetition_penalty=1.2
        )

        processed_text = raw_output[0]["generated_text"].split("<|im_start|>assistant\n")[-1].strip()

        # Programmatic Clean Up: Ensure no trailing cut-off sentences are displayed
        if processed_text and processed_text[-1] not in [".", "!", "?", '"', "*"]:
            last_punctuation = max(processed_text.rfind("."), processed_text.rfind("!"), processed_text.rfind("?"))
            if last_punctuation != -1:
                processed_text = processed_text[:last_punctuation + 1]

        return processed_text

    except Exception as error:
        logger.error(f"Pipeline Fault: {str(error)}", exc_info=True)
        return f"❌ **Pipeline Fault:** Execution error on CPU node. Details: {str(error)}"


# ==============================================================================
# 4. INDUSTRIAL GRADIO UI INTERFACE
# ==============================================================================
CUSTOM_CSS = """
/* Enterprise Layout & Styling */
body, .gradio-container {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
.header-box {
    padding: 20px;
    background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
    border: 1px solid #334155;
    border-radius: 10px;
    margin-bottom: 15px;
    color: #f8fafc;
}
.status-card {
    background: #1e293b;
    border: 1px solid #334155;
    border-radius: 8px;
    padding: 12px 16px;
}
.metric-badge {
    display: inline-block;
    background: #0284c7;
    color: white;
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 600;
}
"""

with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", neutral_hue="slate"), css=CUSTOM_CSS, title="AI Agent Suite") as demo:

    # Header & Status Section
    with gr.Row(elem_classes=["header-box"]):
        with gr.Column(scale=3):
            gr.Markdown("# πŸ€– AI News Debate & Summary Agent Suite")
            gr.Markdown(
                "**About This Agent:** This framework utilizes localized vector embeddings alongside "
                "few-shot prompting to anchor small model reasoning. It delivers deterministic, "
                "concise outputs optimized for zero-error execution on base CPU clusters. "
                "This framework processes user statements and news artifacts to provide dual NLP functions: " 
                "*Adversarial Debate Environment* using real-time vectorized context verification to engage with users "
                "and *Automated Generalization Engine* for complex-news summarization and analysis."
            )

        with gr.Column(scale=1, elem_classes=["status-card"]):
            gr.HighlightedText(
                value=[("ONLINE", "🟒 NLP Match Engine Status")], 
                show_label=False,
                combine_adjacent=True
            )
            gr.Markdown(
                "πŸͺͺ **Agent Profile:** Few-Shot Context Assistant  \n"
                "πŸ“¦ **Model Matrix:** Qwen2.5-1.5B-Instruct  \n"
                "🧬 **Vectorization Engine:** Text FAISS Engine  \n"
                "πŸ’» **Processing Hardware:** Base CPU Tier"
            )

    gr.HTML("<hr style='border: 0.5px solid #374151; margin: 10px 0;'/>")

    # Main Workspace Layout
    with gr.Row():
        
        # Left Panel: Control & Input Workspaces
        with gr.Column(scale=1):
            gr.Markdown("### πŸ› οΈ Agent Workspaces")

            with gr.Tabs() as active_workspace_tabs:

                # Workspace 1: Debate Agent
                with gr.Tab(label="πŸŽ™οΈ News Debate Agent", id=0):
                    gr.Markdown("*State a topic and your stance. The agent will pull factual context, debate you, and score your logic.*")

                    debate_example_picker = gr.Dropdown(
                        label="πŸ’‘ Suggestions for Preset Debate Topic",
                        choices=[
                            "LLMs are indispensable for application development.",
                            "Electric vehicles are the only way to save the environment.",
                            "Universal Basic Income will just make people lazy."
                        ]
                    )

                    debate_text_input = gr.Textbox(
                        label="Your Stance",
                        placeholder="Select or paste your claim here...",
                        lines=3
                    )

                    debate_example_picker.change(
                        fn=lambda x: x,
                        inputs=debate_example_picker,
                        outputs=debate_text_input
                    )

                    run_debate_action = gr.Button("Deploy Counter-Argument & Score", variant="primary")

                # Workspace 2: Text Simplification Agent
                with gr.Tab(label="πŸ“° Snippet Generalizer", id=1):
                    gr.Markdown("*Paste complex, jargon-heavy news columns here to output plain-English, executive summaries.*")

                    generalizer_example_picker = gr.Dropdown(
                        label="πŸ’‘ Examples for Preset Complex News",
                        choices=[
                            "Despite aggressive macroeconomic headwinds and structural quantitative tightening by central banking systems, retail index trends demonstrated non-linear elasticity during fiscal evaluations.",
                            "The ecosystem validation vectors indicate substantial degradation in sub-surface lithospheric layers owing to chemical manufacturing runoffs, sparking compliance friction."
                        ],
                        value="Despite aggressive macroeconomic headwinds and structural quantitative tightening by central banking systems, retail index trends demonstrated non-linear elasticity during fiscal evaluations."
                    )

                    simplify_text_input = gr.Textbox(
                        label="Complex News Snippet",
                        lines=4,
                        value="Despite aggressive macroeconomic headwinds and structural quantitative tightening by central banking systems, retail index trends demonstrated non-linear elasticity during fiscal evaluations.",
                        placeholder="Paste text block or official reports here..."
                    )

                    generalizer_example_picker.change(
                        fn=lambda x: x,
                        inputs=generalizer_example_picker,
                        outputs=simplify_text_input
                    )

                    run_simplifier_action = gr.Button("Deploy for generalization system", variant="primary")

            gr.HTML("<div style='margin-bottom: 15px;'></div>")

            # Runtime Parameter Tuning
            with gr.Accordion("βš™οΈ Runtime Parameter Tuning", open=False):
                max_tokens_val = gr.Slider(minimum=64, maximum=256, value=140, step=16, label="Max tokens")
                temp_val = gr.Slider(minimum=0.1, maximum=1.0, value=0.25, step=0.05, label="Temperature (Low = Deterministic)")
                top_p_val = gr.Slider(minimum=0.50, maximum=1.00, value=0.85, step=0.05, label="Top-p")

        # Right Panel: Output Execution Stream
        with gr.Column(scale=1):
            gr.Markdown("### πŸ–₯️ Active Output Stream")
            active_output_panel = gr.Markdown(
                value="*System ready. Select a workspace on the left panel and click deploy to review parsed generation stream.*"
            )

    # Event Bindings
    run_debate_action.click(
        fn=lambda user_in, tokens, t, p: execution_engine("debate", user_in, tokens, t, p),
        inputs=[debate_text_input, max_tokens_val, temp_val, top_p_val],
        outputs=active_output_panel
    )

    run_simplifier_action.click(
        fn=lambda user_in, tokens, t, p: execution_engine("generalize", user_in, tokens, t, p),
        inputs=[simplify_text_input, max_tokens_val, temp_val, top_p_val],
        outputs=active_output_panel
    )

# Pre-warm singleton engine during container launch to prevent UI cold starts
EngineRegistry.get_instance()

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860, theme=gr.themes.Soft())