Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,202 +1,306 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import torch
|
| 4 |
import gradio as gr
|
| 5 |
-
from transformers import
|
| 6 |
from langchain_community.vectorstores import FAISS
|
| 7 |
from langchain_huggingface import HuggingFaceEmbeddings
|
| 8 |
from langchain_core.documents import Document
|
| 9 |
|
| 10 |
-
# ==========================================
|
| 11 |
-
# 1.
|
| 12 |
-
# ==========================================
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 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 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 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 |
-
|
| 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 |
-
# 3. Optimized Execution Block
|
| 90 |
try:
|
| 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 |
-
f"**Factual Alignment:** {parsed_data.get('factual_alignment', 'N/A')}"
|
| 121 |
-
)
|
| 122 |
-
else:
|
| 123 |
-
bullets = "\n".join([f"* {p}" for p in parsed_data.get('summary_points', [])])
|
| 124 |
-
formatted_response = (
|
| 125 |
-
f"### π Executive Summary\n{bullets}\n\n"
|
| 126 |
-
f"**Core Takeaway:** {parsed_data.get('core_takeaway', 'N/A')}"
|
| 127 |
-
)
|
| 128 |
-
except json.JSONDecodeError:
|
| 129 |
-
# Fallback if raw text output isn't strict JSON
|
| 130 |
-
formatted_response = f"β οΈ *JSON Parsing Fallback (Raw Model Output):*\n\n{raw_output_text}"
|
| 131 |
-
|
| 132 |
-
# 5. Technical Metadata Metrics Block
|
| 133 |
-
metrics = (
|
| 134 |
-
"\n\n---\n"
|
| 135 |
-
"### π Actual Execution Metrics\n"
|
| 136 |
-
f"- **Prompt Tokens:** `{prompt_token_count}` | **Generated Tokens:** `{gen_token_count}`\n"
|
| 137 |
-
f"- **Execution Latency:** `{latency}s` (~{round(gen_token_count/max(latency, 0.01), 1)} tokens/sec)\n"
|
| 138 |
-
f"- **Autoregressive KV Cache:** Active (`use_cache=True`)\n"
|
| 139 |
-
f"- **Context Limit:** 32,768 tokens (Utilization: `{round((prompt_token_count + gen_token_count)/32768 * 100, 2)}%`)"
|
| 140 |
)
|
| 141 |
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
except Exception as error:
|
| 145 |
-
|
|
|
|
|
|
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
#
|
| 151 |
-
with gr.
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
|
|
|
|
|
|
|
|
|
| 155 |
with gr.Row():
|
|
|
|
|
|
|
| 156 |
with gr.Column(scale=1):
|
| 157 |
-
gr.Markdown("### π οΈ Workspaces")
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
choices=[
|
| 163 |
"LLMs are indispensable for application development.",
|
| 164 |
"Electric vehicles are the only way to save the environment.",
|
| 165 |
"Universal Basic Income will just make people lazy."
|
| 166 |
]
|
| 167 |
)
|
| 168 |
-
debate_input = gr.Textbox(label="Your Stance", placeholder="Enter claim...")
|
| 169 |
-
debate_picker.change(fn=lambda x: x, inputs=debate_picker, outputs=debate_input)
|
| 170 |
-
btn_debate = gr.Button("Deploy Debate Agent", variant="primary")
|
| 171 |
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
lines=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
)
|
| 178 |
-
btn_summary = gr.Button("Deploy Generalizer", variant="primary")
|
| 179 |
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
with gr.Column(scale=1):
|
| 186 |
gr.Markdown("### π₯οΈ Active Output Stream")
|
| 187 |
-
|
|
|
|
|
|
|
| 188 |
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
|
|
|
| 193 |
)
|
| 194 |
|
| 195 |
-
|
| 196 |
-
fn=lambda
|
| 197 |
-
inputs=[
|
| 198 |
-
outputs=
|
| 199 |
)
|
| 200 |
|
|
|
|
|
|
|
|
|
|
| 201 |
if __name__ == "__main__":
|
| 202 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
AI News Debate & Summary Agent Suite
|
| 3 |
+
----------------------------------
|
| 4 |
+
An enterprise-grade, lightweight RAG & Few-Shot NLP application designed for
|
| 5 |
+
Hugging Face Spaces CPU Basic instances.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import logging
|
| 9 |
+
import re
|
| 10 |
+
from typing import Optional
|
| 11 |
+
|
| 12 |
import torch
|
| 13 |
import gradio as gr
|
| 14 |
+
from transformers import pipeline
|
| 15 |
from langchain_community.vectorstores import FAISS
|
| 16 |
from langchain_huggingface import HuggingFaceEmbeddings
|
| 17 |
from langchain_core.documents import Document
|
| 18 |
|
| 19 |
+
# ==============================================================================
|
| 20 |
+
# 1. LOGGING & SYSTEM CONFIGURATION
|
| 21 |
+
# ==============================================================================
|
| 22 |
+
logging.basicConfig(
|
| 23 |
+
level=logging.INFO,
|
| 24 |
+
format="%(asctime)s [%(levelname)s] %(name)s - %(message)s"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
)
|
| 26 |
+
logger = logging.getLogger("AIAgentSuite")
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# ==============================================================================
|
| 30 |
+
# 2. SINGLETON RESOURCE & ENGINE REGISTRY
|
| 31 |
+
# ==============================================================================
|
| 32 |
+
class EngineRegistry:
|
| 33 |
+
"""
|
| 34 |
+
Singleton registry managing model loading, pipeline state,
|
| 35 |
+
and FAISS vector stores to maximize memory efficiency on CPU Basic hardware.
|
| 36 |
+
"""
|
| 37 |
+
_instance: Optional["EngineRegistry"] = None
|
| 38 |
+
|
| 39 |
+
def __init__(self) -> None:
|
| 40 |
+
logger.info("Initializing Lightweight Sentence-Transformer Embedding Engine...")
|
| 41 |
+
self.embedding_model = HuggingFaceEmbeddings(
|
| 42 |
+
model_name="sentence-transformers/all-MiniLM-L6-v2"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
)
|
| 44 |
+
|
| 45 |
+
logger.info("Initializing High-Performance Qwen2.5-1.5B Core on CPU...")
|
| 46 |
+
self.chat_pipeline = pipeline(
|
| 47 |
+
"text-generation",
|
| 48 |
+
model="Qwen/Qwen2.5-1.5B-Instruct",
|
| 49 |
+
device="cpu",
|
| 50 |
+
torch_dtype=torch.float32
|
|
|
|
| 51 |
)
|
| 52 |
|
| 53 |
+
logger.info("Indexing Fact-Checking RAG Context Database into FAISS...")
|
| 54 |
+
mock_news_db = [
|
| 55 |
+
"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.",
|
| 56 |
+
"EV Industrial Lifecycle: Electric Vehicles emit zero greenhouse operational gases, but lithium-ion battery extraction increases heavy localized ecosystem toxicity by 42%.",
|
| 57 |
+
"Universal Basic Income: Comprehensive economic trials indicate UBI successfully lowers base poverty indexes but sparks minor macro-inflationary pressures across basic retail commodities."
|
| 58 |
+
]
|
| 59 |
+
documents = [Document(page_content=text) for text in mock_news_db]
|
| 60 |
+
self.vector_store = FAISS.from_documents(documents, self.embedding_model)
|
| 61 |
+
logger.info("Engine Registry successfully loaded into system memory.")
|
| 62 |
+
|
| 63 |
+
@classmethod
|
| 64 |
+
def get_instance(cls) -> "EngineRegistry":
|
| 65 |
+
if cls._instance is None:
|
| 66 |
+
cls._instance = cls()
|
| 67 |
+
return cls._instance
|
| 68 |
+
|
| 69 |
|
| 70 |
+
# ==============================================================================
|
| 71 |
+
# 3. CORE EXECUTION & PROMPT COMPILER
|
| 72 |
+
# ==============================================================================
|
| 73 |
+
def execution_engine(
|
| 74 |
+
workspace_type: str,
|
| 75 |
+
user_input: str,
|
| 76 |
+
max_tokens: int,
|
| 77 |
+
temp: float,
|
| 78 |
+
top_p: float
|
| 79 |
+
) -> str:
|
| 80 |
+
"""
|
| 81 |
+
Processes workspace queries through vector retrieval, ChatML prompt compilation,
|
| 82 |
+
and output post-processing.
|
| 83 |
+
"""
|
| 84 |
+
if not user_input or not user_input.strip():
|
| 85 |
+
return "β οΈ **System Input Error:** Workspace field cannot be processed empty."
|
| 86 |
|
|
|
|
| 87 |
try:
|
| 88 |
+
registry = EngineRegistry.get_instance()
|
| 89 |
+
|
| 90 |
+
# Workspace Routing & Prompt Engineering
|
| 91 |
+
if workspace_type == "debate":
|
| 92 |
+
retriever = registry.vector_store.as_retriever(search_kwargs={"k": 1})
|
| 93 |
+
matched_docs = retriever.invoke(user_input)
|
| 94 |
+
context = matched_docs[0].page_content if matched_docs else "No specific context available."
|
| 95 |
+
|
| 96 |
+
system_instruction = (
|
| 97 |
+
"You are a sharp, polite adversarial debate assistant. Disagree with the user's stance. "
|
| 98 |
+
"Provide exactly one paragraph of logical counter-argument, then provide a score out of 10.\n\n"
|
| 99 |
+
f"VERIFIED CONTEXT TO USE: {context}\n\n"
|
| 100 |
+
"### GOOD EXAMPLE RESPONSE FORMAT:\n"
|
| 101 |
+
"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"
|
| 102 |
+
"**Argument Logic Score: 6/10**"
|
| 103 |
+
)
|
| 104 |
+
else: # Generalize mode
|
| 105 |
+
system_instruction = (
|
| 106 |
+
"You are an expert news editor. Translate complex, jargon-heavy statements into plain, simple English. "
|
| 107 |
+
"Output your answer as clear, high-level summary bullet points under 50 words total.\n\n"
|
| 108 |
+
"### GOOD EXAMPLE RESPONSE FORMAT:\n"
|
| 109 |
+
"* Global markets fell sharply due to sudden central bank interest rate changes.\n"
|
| 110 |
+
"* Local manufacturing activity slowed down to an all-time low."
|
| 111 |
)
|
| 112 |
|
| 113 |
+
# Qwen Official ChatML Formatting
|
| 114 |
+
formatted_prompt = (
|
| 115 |
+
f"<|im_start|>system\n{system_instruction}\n"
|
| 116 |
+
"CRITICAL: Keep your response concise. Do not ramble. Complete your final sentence perfectly.<|im_end|>\n"
|
| 117 |
+
f"<|im_start|>user\n{user_input}<|im_end|>\n"
|
| 118 |
+
"<|im_start|>assistant\n"
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
# CPU Pipeline Execution
|
| 122 |
+
raw_output = registry.chat_pipeline(
|
| 123 |
+
formatted_prompt,
|
| 124 |
+
max_new_tokens=int(max_tokens),
|
| 125 |
+
do_sample=True,
|
| 126 |
+
temperature=float(temp),
|
| 127 |
+
top_p=float(top_p),
|
| 128 |
+
top_k=40,
|
| 129 |
+
repetition_penalty=1.2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
)
|
| 131 |
|
| 132 |
+
processed_text = raw_output[0]["generated_text"].split("<|im_start|>assistant\n")[-1].strip()
|
| 133 |
+
|
| 134 |
+
# Programmatic Clean Up: Ensure no trailing cut-off sentences are displayed
|
| 135 |
+
if processed_text and processed_text[-1] not in [".", "!", "?", '"', "*"]:
|
| 136 |
+
last_punctuation = max(processed_text.rfind("."), processed_text.rfind("!"), processed_text.rfind("?"))
|
| 137 |
+
if last_punctuation != -1:
|
| 138 |
+
processed_text = processed_text[:last_punctuation + 1]
|
| 139 |
+
|
| 140 |
+
return processed_text
|
| 141 |
|
| 142 |
except Exception as error:
|
| 143 |
+
logger.error(f"Pipeline Fault: {str(error)}", exc_info=True)
|
| 144 |
+
return f"β **Pipeline Fault:** Execution error on CPU node. Details: {str(error)}"
|
| 145 |
+
|
| 146 |
|
| 147 |
+
# ==============================================================================
|
| 148 |
+
# 4. INDUSTRIAL GRADIO UI INTERFACE
|
| 149 |
+
# ==============================================================================
|
| 150 |
+
CUSTOM_CSS = """
|
| 151 |
+
/* Enterprise Layout & Styling */
|
| 152 |
+
body, .gradio-container {
|
| 153 |
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
| 154 |
+
}
|
| 155 |
+
.header-box {
|
| 156 |
+
padding: 20px;
|
| 157 |
+
background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
|
| 158 |
+
border: 1px solid #334155;
|
| 159 |
+
border-radius: 10px;
|
| 160 |
+
margin-bottom: 15px;
|
| 161 |
+
color: #f8fafc;
|
| 162 |
+
}
|
| 163 |
+
.status-card {
|
| 164 |
+
background: #1e293b;
|
| 165 |
+
border: 1px solid #334155;
|
| 166 |
+
border-radius: 8px;
|
| 167 |
+
padding: 12px 16px;
|
| 168 |
+
}
|
| 169 |
+
.metric-badge {
|
| 170 |
+
display: inline-block;
|
| 171 |
+
background: #0284c7;
|
| 172 |
+
color: white;
|
| 173 |
+
padding: 2px 8px;
|
| 174 |
+
border-radius: 4px;
|
| 175 |
+
font-size: 0.75rem;
|
| 176 |
+
font-weight: 600;
|
| 177 |
+
}
|
| 178 |
+
"""
|
| 179 |
|
| 180 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", neutral_hue="slate"), css=CUSTOM_CSS, title="AI Agent Suite") as demo:
|
| 181 |
+
|
| 182 |
+
# Header & Status Section
|
| 183 |
+
with gr.Row(elem_classes=["header-box"]):
|
| 184 |
+
with gr.Column(scale=3):
|
| 185 |
+
gr.Markdown("# π€ AI News Debate & Summary Agent Suite")
|
| 186 |
+
gr.Markdown(
|
| 187 |
+
"**About This Agent:** This framework utilizes localized vector embeddings alongside "
|
| 188 |
+
"few-shot prompting to anchor small model reasoning. It delivers deterministic, "
|
| 189 |
+
"concise outputs optimized for zero-error execution on base CPU clusters. "
|
| 190 |
+
"This framework processes user statements and news artifacts to provide dual NLP functions: "
|
| 191 |
+
"*Adversarial Debate Environment* using real-time vectorized context verification to engage with users "
|
| 192 |
+
"and *Automated Generalization Engine* for complex-news summarization and analysis."
|
| 193 |
+
)
|
| 194 |
+
|
| 195 |
+
with gr.Column(scale=1, elem_classes=["status-card"]):
|
| 196 |
+
gr.HighlightedText(
|
| 197 |
+
value=[("ONLINE", "π’ NLP Match Engine Status")],
|
| 198 |
+
show_label=False,
|
| 199 |
+
combine_adjacent=True
|
| 200 |
+
)
|
| 201 |
+
gr.Markdown(
|
| 202 |
+
"πͺͺ **Agent Profile:** Few-Shot Context Assistant \n"
|
| 203 |
+
"π¦ **Model Matrix:** Qwen2.5-1.5B-Instruct \n"
|
| 204 |
+
"𧬠**Vectorization Engine:** Text FAISS Engine \n"
|
| 205 |
+
"π» **Processing Hardware:** Base CPU Tier"
|
| 206 |
+
)
|
| 207 |
|
| 208 |
+
gr.HTML("<hr style='border: 0.5px solid #374151; margin: 10px 0;'/>")
|
| 209 |
+
|
| 210 |
+
# Main Workspace Layout
|
| 211 |
with gr.Row():
|
| 212 |
+
|
| 213 |
+
# Left Panel: Control & Input Workspaces
|
| 214 |
with gr.Column(scale=1):
|
| 215 |
+
gr.Markdown("### π οΈ Agent Workspaces")
|
| 216 |
+
|
| 217 |
+
with gr.Tabs() as active_workspace_tabs:
|
| 218 |
+
|
| 219 |
+
# Workspace 1: Debate Agent
|
| 220 |
+
with gr.Tab(label="ποΈ News Debate Agent", id=0):
|
| 221 |
+
gr.Markdown("*State a topic and your stance. The agent will pull factual context, debate you, and score your logic.*")
|
| 222 |
+
|
| 223 |
+
debate_example_picker = gr.Dropdown(
|
| 224 |
+
label="π‘ Suggestions for Preset Debate Topic",
|
| 225 |
choices=[
|
| 226 |
"LLMs are indispensable for application development.",
|
| 227 |
"Electric vehicles are the only way to save the environment.",
|
| 228 |
"Universal Basic Income will just make people lazy."
|
| 229 |
]
|
| 230 |
)
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
+
debate_text_input = gr.Textbox(
|
| 233 |
+
label="Your Stance",
|
| 234 |
+
placeholder="Select or paste your claim here...",
|
| 235 |
+
lines=3
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
debate_example_picker.change(
|
| 239 |
+
fn=lambda x: x,
|
| 240 |
+
inputs=debate_example_picker,
|
| 241 |
+
outputs=debate_text_input
|
| 242 |
+
)
|
| 243 |
+
|
| 244 |
+
run_debate_action = gr.Button("Deploy Counter-Argument & Score", variant="primary")
|
| 245 |
+
|
| 246 |
+
# Workspace 2: Text Simplification Agent
|
| 247 |
+
with gr.Tab(label="π° Snippet Generalizer", id=1):
|
| 248 |
+
gr.Markdown("*Paste complex, jargon-heavy news columns here to output plain-English, executive summaries.*")
|
| 249 |
+
|
| 250 |
+
generalizer_example_picker = gr.Dropdown(
|
| 251 |
+
label="π‘ Examples for Preset Complex News",
|
| 252 |
+
choices=[
|
| 253 |
+
"Despite aggressive macroeconomic headwinds and structural quantitative tightening by central banking systems, retail index trends demonstrated non-linear elasticity during fiscal evaluations.",
|
| 254 |
+
"The ecosystem validation vectors indicate substantial degradation in sub-surface lithospheric layers owing to chemical manufacturing runoffs, sparking compliance friction."
|
| 255 |
+
],
|
| 256 |
value="Despite aggressive macroeconomic headwinds and structural quantitative tightening by central banking systems, retail index trends demonstrated non-linear elasticity during fiscal evaluations."
|
| 257 |
)
|
|
|
|
| 258 |
|
| 259 |
+
simplify_text_input = gr.Textbox(
|
| 260 |
+
label="Complex News Snippet",
|
| 261 |
+
lines=4,
|
| 262 |
+
value="Despite aggressive macroeconomic headwinds and structural quantitative tightening by central banking systems, retail index trends demonstrated non-linear elasticity during fiscal evaluations.",
|
| 263 |
+
placeholder="Paste text block or official reports here..."
|
| 264 |
+
)
|
| 265 |
+
|
| 266 |
+
generalizer_example_picker.change(
|
| 267 |
+
fn=lambda x: x,
|
| 268 |
+
inputs=generalizer_example_picker,
|
| 269 |
+
outputs=simplify_text_input
|
| 270 |
+
)
|
| 271 |
|
| 272 |
+
run_simplifier_action = gr.Button("Deploy for generalization system", variant="primary")
|
| 273 |
+
|
| 274 |
+
gr.HTML("<div style='margin-bottom: 15px;'></div>")
|
| 275 |
+
|
| 276 |
+
# Runtime Parameter Tuning
|
| 277 |
+
with gr.Accordion("βοΈ Runtime Parameter Tuning", open=False):
|
| 278 |
+
max_tokens_val = gr.Slider(minimum=64, maximum=256, value=140, step=16, label="Max tokens")
|
| 279 |
+
temp_val = gr.Slider(minimum=0.1, maximum=1.0, value=0.25, step=0.05, label="Temperature (Low = Deterministic)")
|
| 280 |
+
top_p_val = gr.Slider(minimum=0.50, maximum=1.00, value=0.85, step=0.05, label="Top-p")
|
| 281 |
+
|
| 282 |
+
# Right Panel: Output Execution Stream
|
| 283 |
with gr.Column(scale=1):
|
| 284 |
gr.Markdown("### π₯οΈ Active Output Stream")
|
| 285 |
+
active_output_panel = gr.Markdown(
|
| 286 |
+
value="*System ready. Select a workspace on the left panel and click deploy to review parsed generation stream.*"
|
| 287 |
+
)
|
| 288 |
|
| 289 |
+
# Event Bindings
|
| 290 |
+
run_debate_action.click(
|
| 291 |
+
fn=lambda user_in, tokens, t, p: execution_engine("debate", user_in, tokens, t, p),
|
| 292 |
+
inputs=[debate_text_input, max_tokens_val, temp_val, top_p_val],
|
| 293 |
+
outputs=active_output_panel
|
| 294 |
)
|
| 295 |
|
| 296 |
+
run_simplifier_action.click(
|
| 297 |
+
fn=lambda user_in, tokens, t, p: execution_engine("generalize", user_in, tokens, t, p),
|
| 298 |
+
inputs=[simplify_text_input, max_tokens_val, temp_val, top_p_val],
|
| 299 |
+
outputs=active_output_panel
|
| 300 |
)
|
| 301 |
|
| 302 |
+
# Pre-warm singleton engine during container launch to prevent UI cold starts
|
| 303 |
+
EngineRegistry.get_instance()
|
| 304 |
+
|
| 305 |
if __name__ == "__main__":
|
| 306 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, theme=gr.themes.Soft())
|