Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -82,7 +82,7 @@ def execution_engine(workspace_type, user_input, max_tokens, temp, top_p):
|
|
| 82 |
add_generation_prompt=True
|
| 83 |
)
|
| 84 |
|
| 85 |
-
# Token Counting
|
| 86 |
input_ids = tokenizer(formatted_prompt, return_tensors="pt").input_ids
|
| 87 |
prompt_token_count = input_ids.shape[1]
|
| 88 |
|
|
@@ -96,11 +96,11 @@ def execution_engine(workspace_type, user_input, max_tokens, temp, top_p):
|
|
| 96 |
temperature=float(temp),
|
| 97 |
top_p=float(top_p),
|
| 98 |
repetition_penalty=1.1,
|
| 99 |
-
use_cache=True,
|
| 100 |
pad_token_id=tokenizer.eos_token_id
|
| 101 |
)
|
| 102 |
|
| 103 |
-
# Extract generated tokens
|
| 104 |
generated_ids = output_tokens[0][prompt_token_count:]
|
| 105 |
raw_output_text = tokenizer.decode(generated_ids, skip_special_tokens=True).strip()
|
| 106 |
|
|
@@ -109,6 +109,7 @@ def execution_engine(workspace_type, user_input, max_tokens, temp, top_p):
|
|
| 109 |
|
| 110 |
# 4. JSON Output Parsing & Schema Validation
|
| 111 |
try:
|
|
|
|
| 112 |
clean_json_str = raw_output_text.replace("```json", "").replace("```", "").strip()
|
| 113 |
parsed_data = json.loads(clean_json_str)
|
| 114 |
|
|
@@ -125,6 +126,7 @@ def execution_engine(workspace_type, user_input, max_tokens, temp, top_p):
|
|
| 125 |
f"**Core Takeaway:** {parsed_data.get('core_takeaway', 'N/A')}"
|
| 126 |
)
|
| 127 |
except json.JSONDecodeError:
|
|
|
|
| 128 |
formatted_response = f"⚠️ *JSON Parsing Fallback (Raw Model Output):*\n\n{raw_output_text}"
|
| 129 |
|
| 130 |
# 5. Technical Metadata Metrics Block
|
|
@@ -144,109 +146,57 @@ def execution_engine(workspace_type, user_input, max_tokens, temp, top_p):
|
|
| 144 |
|
| 145 |
|
| 146 |
# ==========================================
|
| 147 |
-
# 3.
|
| 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 |
-
.kpi-value {
|
| 194 |
-
font-size: 28px;
|
| 195 |
-
font-weight: 800;
|
| 196 |
-
color: #58A6FF;
|
| 197 |
-
line-height: 1.1;
|
| 198 |
-
}
|
| 199 |
-
.kpi-label {
|
| 200 |
-
font-size: 12px;
|
| 201 |
-
font-weight: 600;
|
| 202 |
-
text-transform: uppercase;
|
| 203 |
-
color: #8B949E;
|
| 204 |
-
margin-top: 6px;
|
| 205 |
-
}
|
| 206 |
-
|
| 207 |
-
/* DOC HAWK / HappyOrNot-inspired Capability Grid */
|
| 208 |
-
.feature-grid {
|
| 209 |
-
display: grid;
|
| 210 |
-
grid-template-columns: repeat(4, 1fr);
|
| 211 |
-
gap: 16px;
|
| 212 |
-
margin-bottom: 28px;
|
| 213 |
-
}
|
| 214 |
-
.feature-card {
|
| 215 |
-
background: #161B22;
|
| 216 |
-
border-left: 4px solid #1F6FE5;
|
| 217 |
-
border-radius: 6px;
|
| 218 |
-
padding: 16px;
|
| 219 |
-
}
|
| 220 |
-
.feature-title {
|
| 221 |
-
font-weight: 600;
|
| 222 |
-
font-size: 14px;
|
| 223 |
-
color: #F0F6FC;
|
| 224 |
-
margin-bottom: 6px;
|
| 225 |
-
}
|
| 226 |
-
.feature-desc {
|
| 227 |
-
font-size: 12px;
|
| 228 |
-
color: #8B949E;
|
| 229 |
-
line-height: 1.4;
|
| 230 |
-
}
|
| 231 |
-
|
| 232 |
-
/* Custom Styled Input Action Buttons */
|
| 233 |
-
button.primary-btn {
|
| 234 |
-
background: #238636 !important;
|
| 235 |
-
border: 1px solid rgba(240, 246, 252, 0.1) !important;
|
| 236 |
-
color: #FFFFFF !important;
|
| 237 |
-
font-weight: 600 !important;
|
| 238 |
-
border-radius: 6px !important;
|
| 239 |
-
transition: all 0.2s ease !important;
|
| 240 |
-
}
|
| 241 |
-
button.primary-btn:hover {
|
| 242 |
-
background: #2EA043 !important;
|
| 243 |
-
}
|
| 244 |
-
"""
|
| 245 |
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
|
|
|
|
|
| 82 |
add_generation_prompt=True
|
| 83 |
)
|
| 84 |
|
| 85 |
+
# Token Counting (Exact Engine Computation)
|
| 86 |
input_ids = tokenizer(formatted_prompt, return_tensors="pt").input_ids
|
| 87 |
prompt_token_count = input_ids.shape[1]
|
| 88 |
|
|
|
|
| 96 |
temperature=float(temp),
|
| 97 |
top_p=float(top_p),
|
| 98 |
repetition_penalty=1.1,
|
| 99 |
+
use_cache=True, # Activates autoregressive KV caching during output generation
|
| 100 |
pad_token_id=tokenizer.eos_token_id
|
| 101 |
)
|
| 102 |
|
| 103 |
+
# Extract only newly generated tokens
|
| 104 |
generated_ids = output_tokens[0][prompt_token_count:]
|
| 105 |
raw_output_text = tokenizer.decode(generated_ids, skip_special_tokens=True).strip()
|
| 106 |
|
|
|
|
| 109 |
|
| 110 |
# 4. JSON Output Parsing & Schema Validation
|
| 111 |
try:
|
| 112 |
+
# Strip markdown block wrappers if model includes them
|
| 113 |
clean_json_str = raw_output_text.replace("```json", "").replace("```", "").strip()
|
| 114 |
parsed_data = json.loads(clean_json_str)
|
| 115 |
|
|
|
|
| 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
|
|
|
|
| 146 |
|
| 147 |
|
| 148 |
# ==========================================
|
| 149 |
+
# 3. GRADIO 6.0 DASHBOARD
|
| 150 |
# ==========================================
|
| 151 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 152 |
+
gr.Markdown("# 🤖 AI News Debate & Summary Agent Suite")
|
| 153 |
+
gr.Markdown("B.Tech CSE Implementation: Modular NLP Suite leveraging local RAG, token counting, and ChatML structured parsing.")
|
| 154 |
+
|
| 155 |
+
with gr.Row():
|
| 156 |
+
with gr.Column(scale=1):
|
| 157 |
+
gr.Markdown("### 🛠️ Workspaces")
|
| 158 |
+
with gr.Tabs():
|
| 159 |
+
with gr.Tab("🎙️ News Debate"):
|
| 160 |
+
debate_picker = gr.Dropdown(
|
| 161 |
+
label="Preset Topics",
|
| 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 |
+
with gr.Tab("📰 News Summarizer"):
|
| 173 |
+
summary_input = gr.Textbox(
|
| 174 |
+
label="Complex News Snippet",
|
| 175 |
+
lines=4,
|
| 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 |
+
with gr.Accordion("⚙️ Parameters", open=False):
|
| 181 |
+
max_tokens_val = gr.Slider(64, 300, 180, step=16, label="Max Tokens")
|
| 182 |
+
temp_val = gr.Slider(0.1, 1.0, 0.25, step=0.05, label="Temperature")
|
| 183 |
+
top_p_val = gr.Slider(0.5, 1.0, 0.85, step=0.05, label="Top-p")
|
| 184 |
+
|
| 185 |
+
with gr.Column(scale=1):
|
| 186 |
+
gr.Markdown("### 🖥️ Active Output Stream")
|
| 187 |
+
output_panel = gr.Markdown(value="*System Ready. Select a workspace and deploy.*")
|
| 188 |
+
|
| 189 |
+
btn_debate.click(
|
| 190 |
+
fn=lambda text, tok, t, p: execution_engine("debate", text, tok, t, p),
|
| 191 |
+
inputs=[debate_input, max_tokens_val, temp_val, top_p_val],
|
| 192 |
+
outputs=output_panel
|
| 193 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
+
btn_summary.click(
|
| 196 |
+
fn=lambda text, tok, t, p: execution_engine("generalize", text, tok, t, p),
|
| 197 |
+
inputs=[summary_input, max_tokens_val, temp_val, top_p_val],
|
| 198 |
+
outputs=output_panel
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
if __name__ == "__main__":
|
| 202 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|