VectorV17 commited on
Commit
979c051
·
verified ·
1 Parent(s): 919d5d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -118
app.py CHANGED
@@ -1,162 +1,157 @@
1
  import json
2
  import logging
3
- from typing import Optional
4
- from pydantic import BaseModel, Field
5
  import gradio as gr
6
-
7
- # Optimized CPU Engine & Embeddings
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
- # 2. OPTIMIZED ENGINE REGISTRY (GGUF + KV CACHE)
30
  # ==========================================
31
- class OptimizedEngineRegistry:
32
- _instance: Optional["OptimizedEngineRegistry"] = None
33
-
34
- def __init__(self):
35
- logger.info("Initializing Embeddings Engine...")
36
- self.embedding_model = HuggingFaceEmbeddings(
37
- model_name="sentence-transformers/all-MiniLM-L6-v2"
38
- )
39
-
40
- logger.info("Loading Quantized GGUF Model via llama.cpp (4-Bit SIMD Optimized)...")
41
- # Download and run quantized Qwen2.5-1.5B GGUF for low RAM/high CPU speed
42
- self.llm = Llama.from_pretrained(
43
- repo_id="Qwen/Qwen2.5-1.5B-Instruct-GGUF",
44
- filename="*q4_k_m.gguf",
45
- n_ctx=2048, # Controlled Context Window Size
46
- n_threads=4, # Optimize for HF CPU core allocation
47
- verbose=False
48
- )
49
-
50
- logger.info("Indexing Fact Database into FAISS Vector Store...")
51
- mock_news_db = [
52
- "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.",
53
- "EV Industrial Lifecycle: Electric Vehicles emit zero greenhouse operational gases, but lithium-ion battery extraction increases heavy localized ecosystem toxicity by 42%.",
54
- "Universal Basic Income: Comprehensive economic trials indicate UBI successfully lowers base poverty indexes but sparks minor macro-inflationary pressures across basic retail commodities."
55
- ]
56
- documents = [Document(page_content=text) for text in mock_news_db]
57
- self.vector_store = FAISS.from_documents(documents, self.embedding_model)
58
-
59
- @classmethod
60
- def get_instance(cls) -> "OptimizedEngineRegistry":
61
- if cls._instance is None:
62
- cls._instance = cls()
63
- return cls._instance
 
64
 
65
  # ==========================================
66
- # 3. CONTEXT-BUDGETED EXECUTION PIPELINE
67
  # ==========================================
68
- def execution_engine(workspace_type: str, user_input: str, max_tokens: int, temp: float) -> str:
69
  if not user_input or not user_input.strip():
70
- return "⚠️️ **Error:** User input field cannot be empty."
71
 
72
  try:
73
- engine = OptimizedEngineRegistry.get_instance()
74
-
75
- # --- A. Context Retrieval & Distance Thresholding ---
76
- context_str = "No verified context matched."
77
- context_used = False
78
 
79
  if workspace_type == "debate":
80
- # Similarity Search with Score Thresholding (L2 Distance)
81
- results = engine.vector_store.similarity_search_with_score(user_input, k=1)
82
- if results and results[0][1] < 1.2: # Threshold for valid vector match
83
- context_str = results[0][0].page_content
84
- context_used = True
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
- schema_json = json.dumps(SummaryResponseSchema.model_json_schema(), indent=2)
94
- system_prompt = (
95
- "You are a news editor. Summarize the text into plain English. Respond EXCLUSIVELY in valid JSON:\n"
96
- f"{schema_json}"
97
  )
98
 
99
- # --- B. Explicit Context Window Token Budgeting ---
100
- total_ctx_limit = 2048
101
- system_tokens = len(engine.llm.tokenize(system_prompt.encode("utf-8")))
102
- user_tokens = len(engine.llm.tokenize(user_input.encode("utf-8")))
103
- allocated_budget = total_ctx_limit - (system_tokens + max_tokens + 50)
104
-
105
- # Dynamic Truncation if User Input exceeds token budget
106
- if user_tokens > allocated_budget:
107
- tokens = engine.llm.tokenize(user_input.encode("utf-8"))[:allocated_budget]
108
- user_input = engine.llm.detokenize(tokens).decode("utf-8")
109
- logger.warning("User prompt truncated to satisfy token context budget.")
110
-
111
- # --- C. Structured Schema-Constrained Generation ---
 
112
  messages = [
113
- {"role": "system", "content": system_prompt},
114
  {"role": "user", "content": user_input}
115
  ]
 
116
 
117
- response = engine.llm.create_chat_completion(
118
- messages=messages,
119
- max_tokens=int(max_tokens),
 
 
120
  temperature=float(temp),
121
- response_format={"type": "json_object"} # Native JSON Schema Enforcement
 
122
  )
123
 
124
- raw_json_str = response["choices"][0]["message"]["content"]
125
- parsed_json = json.loads(raw_json_str)
 
 
 
 
 
 
 
 
126
 
127
- # Render Clean Output
128
  if workspace_type == "debate":
 
 
129
  return (
130
- f"### 🎙️ Adversarial Counter-Argument\n{parsed_json.get('counter_argument')}\n\n"
131
- f"**Argument Logic Score:** {parsed_json.get('logic_score')}/10 \n"
132
- f"**Context Grounding Applied:** `{context_used}`"
133
  )
134
  else:
135
- bullets = "\n".join([f"* {b}" for b in parsed_json.get("summary_bullet_points", [])])
136
- return f"### 📰 Executive Summary\n{bullets}\n\n**Total Word Count:** {parsed_json.get('word_count')} words"
 
137
 
 
 
 
138
  except Exception as e:
139
- logger.error(f"Execution Error: {str(e)}", exc_info=True)
140
- return f"❌ **System Execution Fault:** {str(e)}"
141
 
142
  # ==========================================
143
- # 4. GRADIO INTERFACE
144
  # ==========================================
145
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
146
- gr.Markdown("# 🎓 CS Industrial-Grade NLP & Debate Engine")
147
- gr.Markdown("*Featuring GGUF SIMD Quantization, Token Budgeting, and Pydantic JSON Schema Validation.*")
148
 
149
  with gr.Row():
150
  with gr.Column():
151
- workspace = gr.Radio(choices=["debate", "generalize"], value="debate", label="Workspace Engine Mode")
152
- user_in = gr.Textbox(lines=4, label="Input Prompt / Claim")
153
- max_tok = gr.Slider(64, 256, value=140, label="Max Response Tokens")
154
- temp = gr.Slider(0.1, 1.0, value=0.2, label="Temperature")
155
- btn = gr.Button("Execute System Engine", variant="primary")
 
156
  with gr.Column():
157
- out = gr.Markdown("### 🖥️ Structured Engine Output\n*Awaiting execution...*")
158
 
159
- btn.click(execution_engine, inputs=[workspace, user_in, max_tok, temp], outputs=out)
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)