Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,162 +1,157 @@
|
|
| 1 |
import json
|
| 2 |
import logging
|
| 3 |
-
|
| 4 |
-
|
| 5 |
import gradio as gr
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
from llama_cpp import Llama
|
| 9 |
-
from langchain_community.vectorstores import FAISS
|
| 10 |
-
from langchain_huggingface import HuggingFaceEmbeddings
|
| 11 |
-
from langchain_core.documents import Document
|
| 12 |
-
|
| 13 |
-
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
| 14 |
-
logger = logging.getLogger("CSENewsEngine")
|
| 15 |
-
|
| 16 |
-
# ==========================================
|
| 17 |
-
# 1. STRUCTURED OUTPUT SCHEMAS (Pydantic)
|
| 18 |
-
# ==========================================
|
| 19 |
-
class DebateResponseSchema(BaseModel):
|
| 20 |
-
counter_argument: str = Field(description="One paragraph counter-argument grounded in context.")
|
| 21 |
-
logic_score: int = Field(description="Numerical argument logic score from 1 to 10.")
|
| 22 |
-
context_applied: bool = Field(description="True if verified context was successfully utilized.")
|
| 23 |
-
|
| 24 |
-
class SummaryResponseSchema(BaseModel):
|
| 25 |
-
summary_bullet_points: list[str] = Field(description="List of plain English executive summary bullets.")
|
| 26 |
-
word_count: int = Field(description="Total word count of the output summary.")
|
| 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 |
-
def execution_engine(workspace_type: str, user_input: str, max_tokens: int, temp: float)
|
| 69 |
if not user_input or not user_input.strip():
|
| 70 |
-
return "⚠️
|
| 71 |
|
| 72 |
try:
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
context_str = "No verified context matched."
|
| 77 |
-
context_used = False
|
| 78 |
|
| 79 |
if workspace_type == "debate":
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
schema_json = json.dumps(DebateResponseSchema.model_json_schema(), indent=2)
|
| 87 |
-
system_prompt = (
|
| 88 |
-
"You are an adversarial debate engine. Respond EXCLUSIVELY in valid JSON matching this schema:\n"
|
| 89 |
-
f"{schema_json}\n\n"
|
| 90 |
-
f"VERIFIED CONTEXT: {context_str}"
|
| 91 |
)
|
| 92 |
else:
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
"
|
| 96 |
-
|
| 97 |
)
|
| 98 |
|
| 99 |
-
#
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
user_tokens = len(
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
# Dynamic
|
| 106 |
-
if user_tokens >
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
| 112 |
messages = [
|
| 113 |
-
{"role": "system", "content":
|
| 114 |
{"role": "user", "content": user_input}
|
| 115 |
]
|
|
|
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
| 120 |
temperature=float(temp),
|
| 121 |
-
|
|
|
|
| 122 |
)
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
-
#
|
| 128 |
if workspace_type == "debate":
|
|
|
|
|
|
|
| 129 |
return (
|
| 130 |
-
f"### 🎙️ Adversarial Counter-Argument\n{
|
| 131 |
-
f"**Argument Logic Score:** {
|
| 132 |
-
f"**
|
| 133 |
)
|
| 134 |
else:
|
| 135 |
-
bullets =
|
| 136 |
-
|
|
|
|
| 137 |
|
|
|
|
|
|
|
|
|
|
| 138 |
except Exception as e:
|
| 139 |
-
|
| 140 |
-
|
| 141 |
|
| 142 |
# ==========================================
|
| 143 |
-
#
|
| 144 |
# ==========================================
|
| 145 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 146 |
-
gr.Markdown("#
|
| 147 |
-
gr.Markdown("
|
| 148 |
|
| 149 |
with gr.Row():
|
| 150 |
with gr.Column():
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
max_tok = gr.Slider(64, 256, value=
|
| 154 |
-
temp = gr.Slider(0.1, 1.0, value=0.2, label="Temperature")
|
| 155 |
-
|
|
|
|
| 156 |
with gr.Column():
|
| 157 |
-
out = gr.Markdown("### 🖥️
|
| 158 |
|
| 159 |
-
|
| 160 |
|
| 161 |
if __name__ == "__main__":
|
| 162 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import json
|
| 2 |
import logging
|
| 3 |
+
import numpy as np
|
| 4 |
+
import torch
|
| 5 |
import gradio as gr
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
from sentence_transformers import SentenceTransformer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# ==========================================
|
| 10 |
+
# 1. LOGGING & SYSTEM INITIALIZATION
|
| 11 |
# ==========================================
|
| 12 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
| 13 |
+
logger = logging.getLogger("SimpleAIAgent")
|
| 14 |
+
|
| 15 |
+
logger.info("Loading Embedding Engine & Vector Database...")
|
| 16 |
+
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 17 |
+
|
| 18 |
+
# Real ground-truth context database
|
| 19 |
+
MOCK_NEWS_DB = [
|
| 20 |
+
"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.",
|
| 21 |
+
"EV Industrial Lifecycle: Electric Vehicles emit zero greenhouse operational gases, but lithium-ion battery extraction increases heavy localized ecosystem toxicity by 42%.",
|
| 22 |
+
"Universal Basic Income: Comprehensive economic trials indicate UBI successfully lowers base poverty indexes but sparks minor macro-inflationary pressures across basic retail commodities."
|
| 23 |
+
]
|
| 24 |
+
|
| 25 |
+
# Pre-compute document embeddings normalized for Cosine Similarity
|
| 26 |
+
doc_embeddings = embedder.encode(MOCK_NEWS_DB, normalize_embeddings=True)
|
| 27 |
+
|
| 28 |
+
def retrieve_context(query: str, similarity_threshold: float = 0.35):
|
| 29 |
+
"""Pure NumPy Cosine Similarity Vector Retrieval (No LangChain needed)."""
|
| 30 |
+
query_emb = embedder.encode([query], normalize_embeddings=True)
|
| 31 |
+
scores = np.dot(doc_embeddings, query_emb.T).flatten() # Dot product of normalized vectors = Cosine Similarity
|
| 32 |
+
best_idx = int(np.argmax(scores))
|
| 33 |
+
|
| 34 |
+
if scores[best_idx] >= similarity_threshold:
|
| 35 |
+
return MOCK_NEWS_DB[best_idx], float(scores[best_idx])
|
| 36 |
+
return "No verified context matched.", float(scores[best_idx])
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
logger.info("Loading Qwen2.5-1.5B Instruct Pipeline...")
|
| 40 |
+
chat_pipeline = pipeline(
|
| 41 |
+
"text-generation",
|
| 42 |
+
model="Qwen/Qwen2.5-1.5B-Instruct",
|
| 43 |
+
device="cpu",
|
| 44 |
+
torch_dtype=torch.float32
|
| 45 |
+
)
|
| 46 |
|
| 47 |
# ==========================================
|
| 48 |
+
# 2. CORE EXECUTION ENGINE
|
| 49 |
# ==========================================
|
| 50 |
+
def execution_engine(workspace_type: str, user_input: str, max_tokens: int, temp: float):
|
| 51 |
if not user_input or not user_input.strip():
|
| 52 |
+
return "⚠️ **System Error:** Input field cannot be empty."
|
| 53 |
|
| 54 |
try:
|
| 55 |
+
# A. Context Retrieval & JSON Prompt Setup
|
| 56 |
+
context_text = ""
|
| 57 |
+
similarity_score = 0.0
|
|
|
|
|
|
|
| 58 |
|
| 59 |
if workspace_type == "debate":
|
| 60 |
+
context_text, similarity_score = retrieve_context(user_input)
|
| 61 |
+
system_instruction = (
|
| 62 |
+
"You are an adversarial debate engine. Respond EXCLUSIVELY in valid JSON format.\n"
|
| 63 |
+
"JSON Format:\n"
|
| 64 |
+
'{\n "counter_argument": "One concise paragraph disagreeing.",\n "logic_score": 7\n}\n\n'
|
| 65 |
+
f"VERIFIED CONTEXT: {context_text}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
)
|
| 67 |
else:
|
| 68 |
+
system_instruction = (
|
| 69 |
+
"You are a news editor. Translate complex text into simple English. Respond EXCLUSIVELY in valid JSON format.\n"
|
| 70 |
+
"JSON Format:\n"
|
| 71 |
+
'{\n "summary_bullets": ["bullet point 1", "bullet point 2"]\n}'
|
| 72 |
)
|
| 73 |
|
| 74 |
+
# B. Context Window Token Budgeting (Standard HF Tokenizer)
|
| 75 |
+
tokenizer = chat_pipeline.tokenizer
|
| 76 |
+
sys_tokens = len(tokenizer.encode(system_instruction))
|
| 77 |
+
user_tokens = len(tokenizer.encode(user_input))
|
| 78 |
+
max_context_limit = 2048
|
| 79 |
+
|
| 80 |
+
# Dynamic truncation if user input exceeds available token budget
|
| 81 |
+
if (sys_tokens + user_tokens + int(max_tokens)) > max_context_limit:
|
| 82 |
+
allowed_user_tokens = max_context_limit - sys_tokens - int(max_tokens) - 30
|
| 83 |
+
truncated_ids = tokenizer.encode(user_input)[:allowed_user_tokens]
|
| 84 |
+
user_input = tokenizer.decode(truncated_ids)
|
| 85 |
+
logger.warning("User input truncated to satisfy context window token budget.")
|
| 86 |
+
|
| 87 |
+
# C. Construct Official Model Prompt
|
| 88 |
messages = [
|
| 89 |
+
{"role": "system", "content": system_instruction},
|
| 90 |
{"role": "user", "content": user_input}
|
| 91 |
]
|
| 92 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 93 |
|
| 94 |
+
# D. Run Model Inference
|
| 95 |
+
raw_output = chat_pipeline(
|
| 96 |
+
prompt,
|
| 97 |
+
max_new_tokens=int(max_tokens),
|
| 98 |
+
do_sample=True,
|
| 99 |
temperature=float(temp),
|
| 100 |
+
top_p=0.85,
|
| 101 |
+
repetition_penalty=1.1
|
| 102 |
)
|
| 103 |
|
| 104 |
+
generated_text = raw_output[0]["generated_text"][len(prompt):].strip()
|
| 105 |
+
|
| 106 |
+
# E. Extract and Parse Structured JSON Output
|
| 107 |
+
clean_json_str = generated_text
|
| 108 |
+
if "```json" in clean_json_str:
|
| 109 |
+
clean_json_str = clean_json_str.split("```json")[1].split("```")[0].strip()
|
| 110 |
+
elif "```" in clean_json_str:
|
| 111 |
+
clean_json_str = clean_json_str.split("```")[1].split("```")[0].strip()
|
| 112 |
+
|
| 113 |
+
data = json.loads(clean_json_str)
|
| 114 |
|
| 115 |
+
# F. Format Final Response
|
| 116 |
if workspace_type == "debate":
|
| 117 |
+
counter = data.get("counter_argument", generated_text)
|
| 118 |
+
score = data.get("logic_score", "N/A")
|
| 119 |
return (
|
| 120 |
+
f"### 🎙️ Adversarial Counter-Argument\n{counter}\n\n"
|
| 121 |
+
f"**Argument Logic Score:** {score}/10 \n"
|
| 122 |
+
f"**Vector Similarity Score:** `{similarity_score:.2f}`"
|
| 123 |
)
|
| 124 |
else:
|
| 125 |
+
bullets = data.get("summary_bullets", [])
|
| 126 |
+
bullet_fmt = "\n".join([f"* {b}" for b in bullets]) if isinstance(bullets, list) else str(bullets)
|
| 127 |
+
return f"### 📰 Executive Summary\n{bullet_fmt}"
|
| 128 |
|
| 129 |
+
except json.JSONDecodeError:
|
| 130 |
+
# Fallback handling if JSON formatting had minor syntax issues
|
| 131 |
+
return f"### 🖥️ Model Generation Output\n{generated_text}"
|
| 132 |
except Exception as e:
|
| 133 |
+
return f"❌ **Execution Fault:** {str(e)}"
|
| 134 |
+
|
| 135 |
|
| 136 |
# ==========================================
|
| 137 |
+
# 3. GRADIO USER INTERFACE
|
| 138 |
# ==========================================
|
| 139 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 140 |
+
gr.Markdown("# 🤖 Simplified AI News & Debate Agent")
|
| 141 |
+
gr.Markdown("Lightweight RAG Engine featuring explicit Token Budgeting, NumPy Vector Search, and JSON Structured Output.")
|
| 142 |
|
| 143 |
with gr.Row():
|
| 144 |
with gr.Column():
|
| 145 |
+
mode = gr.Radio(["debate", "generalize"], value="debate", label="Workspace Engine Mode")
|
| 146 |
+
inp = gr.Textbox(lines=4, label="Input Prompt / Claim", placeholder="Enter your stance or article snippet...")
|
| 147 |
+
max_tok = gr.Slider(64, 256, value=120, step=16, label="Max Response Tokens")
|
| 148 |
+
temp = gr.Slider(0.1, 1.0, value=0.2, step=0.05, label="Temperature")
|
| 149 |
+
submit_btn = gr.Button("Deploy Request", variant="primary")
|
| 150 |
+
|
| 151 |
with gr.Column():
|
| 152 |
+
out = gr.Markdown("### 🖥️ Response Output\n*Awaiting execution...*")
|
| 153 |
|
| 154 |
+
submit_btn.click(execution_engine, inputs=[mode, inp, max_tok, temp], outputs=out)
|
| 155 |
|
| 156 |
if __name__ == "__main__":
|
| 157 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|