File size: 8,437 Bytes
fb50d42
b094b22
979c051
b094b22
 
 
919d5d6
 
b094b22
919d5d6
b094b22
 
979c051
b094b22
 
 
 
 
 
 
979c051
b094b22
 
979c051
 
 
 
b094b22
 
979c051
45d3092
919d5d6
55db09f
919d5d6
b094b22
 
55db09f
fb50d42
b094b22
 
 
55db09f
919d5d6
55db09f
b094b22
55db09f
 
 
 
 
b094b22
 
 
55db09f
 
b094b22
fb50d42
55db09f
b094b22
55db09f
b094b22
 
 
 
 
55db09f
979c051
b094b22
979c051
55db09f
 
fb50d42
b094b22
 
 
55db09f
 
 
 
 
 
 
 
 
 
 
b094b22
 
fb50d42
b094b22
55db09f
b094b22
 
 
55db09f
b094b22
55db09f
b094b22
 
 
55db09f
 
 
b094b22
55db09f
b094b22
55db09f
45d3092
c5e95f0
55db09f
 
b094b22
55db09f
 
b094b22
55db09f
b094b22
 
 
 
 
 
55db09f
b094b22
55db09f
b094b22
55db09f
b094b22
55db09f
b094b22
 
 
 
 
 
 
55db09f
b094b22
55db09f
b094b22
 
55db09f
b094b22
55db09f
 
 
 
b094b22
55db09f
 
 
979c051
55db09f
b094b22
 
 
 
 
c5e95f0
b094b22
 
 
 
 
45d3092
c5e95f0
919d5d6
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
import gradio as gr
import torch
from transformers import pipeline
from langchain_community.vectorstores import FAISS
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_core.documents import Document

# ==========================================
# 1. INITIALIZE ENGINE & VECTOR DATA
# ==========================================
print("Loading Lightweight Embedding Engine...")
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")

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

# Fact-checking RAG context database
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]
vector_store = FAISS.from_documents(documents, embedding_model)


# ==========================================
# 2. OPTIMIZED EXECUTION ENGINE
# ==========================================
def execution_engine(workspace_type, user_input, max_tokens, temp, top_p):
    if not user_input.strip():
        return "⚠️ **System Input Error:** Input field cannot be empty."

    if workspace_type == "debate":
        retriever = 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 context available."
        
        # Concise prompt structure ensuring score presence
        system_instruction = (
            "You are a sharp, polite adversarial debate assistant. Disagree with the user's stance.\n"
            f"VERIFIED CONTEXT: {context}\n\n"
            "INSTRUCTIONS:\n"
            "1. Write exactly 2 concise counter-argument sentences.\n"
            "2. Always end your response with: '**Argument Logic Score: X/10**' (replace X with 1-10)."
        )
    else:  # Generalize mode
        system_instruction = (
            "You are an expert news editor. Translate complex statements into simple English.\n"
            "Output exactly 2 concise bullet points summarizing key points under 40 words total."
        )

    # ChatML Template Formatting
    formatted_prompt = (
        f"<|im_start|>system\n{system_instruction}<|im_end|>\n"
        f"<|im_start|>user\n{user_input}<|im_end|>\n"
        "<|im_start|>assistant\n"
    )

    try:
        # Greedy decoding (do_sample=False) accelerates CPU generation significantly
        raw_output = chat_pipeline(
            formatted_prompt,
            max_new_tokens=int(max_tokens),
            do_sample=False,  # Faster deterministic execution on CPU
            pad_token_id=chat_pipeline.tokenizer.eos_token_id
        )
        
        processed_text = raw_output[0]["generated_text"].split("<|im_start|>assistant\n")[-1].strip()
        
        # Safe Truncation Cleanup: Retain score characters (*, ), digits) without stripping score line
        valid_endings = [".", "!", "?", '"', "*", ")", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
        if processed_text and processed_text[-1] not in valid_endings:
            last_punc = max(
                processed_text.rfind("."), 
                processed_text.rfind("!"), 
                processed_text.rfind("?"),
                processed_text.rfind("*")
            )
            if last_punc != -1:
                processed_text = processed_text[:last_punc + 1]
                
        return processed_text

    except Exception as error:
        return f"❌ **Pipeline Fault:** Details: {str(error)}"


# ==========================================
# 3. GRADIO INTERFACE
# ==========================================
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    
    with gr.Row():
        with gr.Column(scale=3):
            gr.Markdown("# πŸ€– AI News Debate & Summary Agent Suite")
            gr.Markdown("Localized vector embeddings + few-shot prompting optimized for CPU execution.")
        
        with gr.Column(scale=1):
            gr.HighlightedText(value=[("ONLINE", "🟒 CPU Mode")], show_label=False)

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

    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("### πŸ› οΈ Agent Workspaces")
            
            with gr.Tabs():
                with gr.Tab(label="πŸŽ™οΈ News Debate Agent"):
                    debate_example_picker = gr.Dropdown(
                        label="πŸ’‘ Preset Topics",
                        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="Enter your claim...")
                    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")

                with gr.Tab(label="πŸ“° Snippet Generalizer"):
                    generalizer_example_picker = gr.Dropdown(
                        label="πŸ’‘ 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."
                    )
                    generalizer_example_picker.change(fn=lambda x: x, inputs=generalizer_example_picker, outputs=simplify_text_input)
                    run_simplifier_action = gr.Button("Deploy Generalization Engine", variant="primary")
            
            with gr.Accordion("βš™οΈ Parameter Tuning", open=False):
                max_tokens_val = gr.Slider(minimum=100, maximum=300, value=200, step=20, label="Max tokens")
                temp_val = gr.Slider(minimum=0.1, maximum=1.0, value=0.25, step=0.05, label="Temperature")
                top_p_val = gr.Slider(minimum=0.50, maximum=1.00, value=0.85, step=0.05, label="Top-p")

        with gr.Column(scale=1):
            gr.Markdown("### πŸ–₯️ Active Output Stream")
            active_output_panel = gr.Markdown(value="*System ready.*")

    # 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
    )

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