Spaces:
Sleeping
Sleeping
Commit ·
e2d338a
1
Parent(s): e9c1814
updated paste functions and web search
Browse files- app.py +83 -2
- core_logic.py +17 -3
app.py
CHANGED
|
@@ -23,6 +23,9 @@ with gr.Blocks() as demo:
|
|
| 23 |
# This state keeps track of the filename for the current session
|
| 24 |
chat_id_state = gr.State("")
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
with gr.Row():
|
| 27 |
# --- Left Panel: Sidebar History ---
|
| 28 |
with gr.Column(scale=1, variant="secondary"):
|
|
@@ -64,7 +67,8 @@ with gr.Blocks() as demo:
|
|
| 64 |
scale=8
|
| 65 |
)
|
| 66 |
|
| 67 |
-
# 2. Provide a distinct, clear upload node that connects to your perception agent pipeline
|
|
|
|
| 68 |
upload_btn = gr.UploadButton(
|
| 69 |
"📎 Attach Documents/Images",
|
| 70 |
file_count="multiple",
|
|
@@ -72,6 +76,9 @@ with gr.Blocks() as demo:
|
|
| 72 |
scale=2
|
| 73 |
)
|
| 74 |
|
|
|
|
|
|
|
|
|
|
| 75 |
# --- Right Panel: Agentic Control Tower ---
|
| 76 |
with gr.Column(scale=1, variant="secondary"):
|
| 77 |
gr.Markdown("### 🚀 CoderG Authorization Core")
|
|
@@ -98,6 +105,23 @@ with gr.Blocks() as demo:
|
|
| 98 |
|
| 99 |
# --- LOGIC FUNCTIONS ---
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
def bot_response(message, history, chat_id):
|
| 102 |
user_content = message["text"]
|
| 103 |
|
|
@@ -144,6 +168,63 @@ with gr.Blocks() as demo:
|
|
| 144 |
|
| 145 |
# --- INTERACTION ARCHITECTURE / EVENT HANDLERS ---
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
# 1. Submit Chat -> Stream Response -> Save -> Refresh Sidebar
|
| 148 |
chat_input.submit(
|
| 149 |
bot_response,
|
|
@@ -175,6 +256,6 @@ with gr.Blocks() as demo:
|
|
| 175 |
[target_repo, commit_txt, staged_files],
|
| 176 |
[output_log]
|
| 177 |
)
|
| 178 |
-
|
| 179 |
# Fully consolidated theme and styling injections down into launch() parameter fields
|
| 180 |
demo.launch(theme=gr.themes.Soft(), css="styles.css")
|
|
|
|
| 23 |
# This state keeps track of the filename for the current session
|
| 24 |
chat_id_state = gr.State("")
|
| 25 |
|
| 26 |
+
# Hidden state tracking variable to maintain files across the chat session
|
| 27 |
+
staged_files_state = gr.State([])
|
| 28 |
+
|
| 29 |
with gr.Row():
|
| 30 |
# --- Left Panel: Sidebar History ---
|
| 31 |
with gr.Column(scale=1, variant="secondary"):
|
|
|
|
| 67 |
scale=8
|
| 68 |
)
|
| 69 |
|
| 70 |
+
# 2. Wire Up the Component Handlers - 2. Provide a distinct, clear upload node that connects to your perception agent pipeline
|
| 71 |
+
# Triggered immediately when a user finishes choosing files in the file browser window
|
| 72 |
upload_btn = gr.UploadButton(
|
| 73 |
"📎 Attach Documents/Images",
|
| 74 |
file_count="multiple",
|
|
|
|
| 76 |
scale=2
|
| 77 |
)
|
| 78 |
|
| 79 |
+
# Visual text tracker showing exactly what files have successfully staged
|
| 80 |
+
upload_status = gr.Markdown("")
|
| 81 |
+
|
| 82 |
# --- Right Panel: Agentic Control Tower ---
|
| 83 |
with gr.Column(scale=1, variant="secondary"):
|
| 84 |
gr.Markdown("### 🚀 CoderG Authorization Core")
|
|
|
|
| 105 |
|
| 106 |
# --- LOGIC FUNCTIONS ---
|
| 107 |
|
| 108 |
+
# --- 1: Create a staging function to process files when uploaded ---
|
| 109 |
+
def handle_file_upload(uploaded_files, current_staged_files):
|
| 110 |
+
"""Processes files when selected via browser and appends to staging session state."""
|
| 111 |
+
if not current_staged_files:
|
| 112 |
+
current_staged_files = []
|
| 113 |
+
|
| 114 |
+
# Gradio returns a list of file objects when file_count="multiple"
|
| 115 |
+
for file_obj in uploaded_files:
|
| 116 |
+
# Check the property format of incoming file assets from Gradio 6
|
| 117 |
+
file_path = file_obj.name if hasattr(file_obj, 'name') else file_obj
|
| 118 |
+
if file_path and file_path not in current_staged_files:
|
| 119 |
+
current_staged_files.append(file_path)
|
| 120 |
+
|
| 121 |
+
# Return a visual message showing how many files are safely staged
|
| 122 |
+
status_msg = f"🟢 **{len(current_staged_files)} file(s) staged successfully and attached to next prompt.**"
|
| 123 |
+
return current_staged_files, status_msg
|
| 124 |
+
|
| 125 |
def bot_response(message, history, chat_id):
|
| 126 |
user_content = message["text"]
|
| 127 |
|
|
|
|
| 168 |
|
| 169 |
# --- INTERACTION ARCHITECTURE / EVENT HANDLERS ---
|
| 170 |
|
| 171 |
+
# Hook the UploadButton event listener loop to stage chosen files right away
|
| 172 |
+
upload_btn.upload(
|
| 173 |
+
fn=handle_file_upload,
|
| 174 |
+
inputs=[upload_btn, staged_files_state],
|
| 175 |
+
outputs=[staged_files_state, upload_status]
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
# Submission wrapper to package parameters together for your multi-format engine
|
| 179 |
+
# When submitting, we pass BOTH the text and the hidden staged files state array!
|
| 180 |
+
def process_submission(message_text, current_staged_files, history, chat_id):
|
| 181 |
+
if not message_text.strip() and not current_staged_files:
|
| 182 |
+
return history, "", current_staged_files, ""
|
| 183 |
+
|
| 184 |
+
# Packaging matching the identical format of core_logic expectations
|
| 185 |
+
payload = {
|
| 186 |
+
"text": message_text,
|
| 187 |
+
"files": current_staged_files
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
# Stream responses through bot loops and clear inputs when complete
|
| 191 |
+
for updated_history in bot_response(payload, history, chat_id):
|
| 192 |
+
# Continuously yield state update frames, wiping values upon initial loop entry
|
| 193 |
+
yield updated_history, "", [], ""
|
| 194 |
+
|
| 195 |
+
# 1. Bind Enter/Submit behavior for the chat input text box
|
| 196 |
+
chat_input.submit(
|
| 197 |
+
fn=process_submission,
|
| 198 |
+
inputs=[chat_input, staged_files_state, chatbot, chat_id_state],
|
| 199 |
+
outputs=[chatbot, chat_input, staged_files_state, upload_status]
|
| 200 |
+
).then(
|
| 201 |
+
fn=handle_save,
|
| 202 |
+
inputs=[chatbot, chat_id_state],
|
| 203 |
+
outputs=[chat_id_state, history_list]
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
# 2. Click Sidebar Item -> Load Content
|
| 207 |
+
history_list.click(
|
| 208 |
+
fn=load_past_chat,
|
| 209 |
+
inputs=[history_list],
|
| 210 |
+
outputs=[chatbot, chat_id_state]
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
# 3. New Chat Button Initialization
|
| 214 |
+
new_btn.click(
|
| 215 |
+
fn=lambda: ([], "", [], load_history(), "", "_Awaiting local environment staging completion..._"),
|
| 216 |
+
inputs=None,
|
| 217 |
+
outputs=[chatbot, chat_id_state, staged_files_state, history_list, upload_status, output_log]
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
# 4. Bind the Control Tower Approve Action button
|
| 221 |
+
approve_btn.click(
|
| 222 |
+
fn=push_authorized,
|
| 223 |
+
inputs=[target_repo, commit_txt, staged_files],
|
| 224 |
+
outputs=[output_log]
|
| 225 |
+
)
|
| 226 |
+
|
| 227 |
+
"""
|
| 228 |
# 1. Submit Chat -> Stream Response -> Save -> Refresh Sidebar
|
| 229 |
chat_input.submit(
|
| 230 |
bot_response,
|
|
|
|
| 256 |
[target_repo, commit_txt, staged_files],
|
| 257 |
[output_log]
|
| 258 |
)
|
| 259 |
+
"""
|
| 260 |
# Fully consolidated theme and styling injections down into launch() parameter fields
|
| 261 |
demo.launch(theme=gr.themes.Soft(), css="styles.css")
|
core_logic.py
CHANGED
|
@@ -104,8 +104,22 @@ def chat_function(message, history):
|
|
| 104 |
|
| 105 |
# 2. Research Trigger
|
| 106 |
if any(keyword in user_text.lower() for keyword in ["search", "docs", "latest"]):
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
else:
|
| 110 |
prompt = f"FILES:\n{context_from_files}\n\nUSER: {user_text}"
|
| 111 |
|
|
@@ -115,7 +129,7 @@ def chat_function(message, history):
|
|
| 115 |
# ONLY KEEP LAST 3 TURNS: This is the 'Master Stroke' for staying under 6k TPM
|
| 116 |
for turn in history[-3:]:
|
| 117 |
messages.append({"role": turn["role"], "content": turn["content"]})
|
| 118 |
-
|
| 119 |
messages.append({"role": "user", "content": prompt})
|
| 120 |
|
| 121 |
# Spliced directly into ./core_logic.py at the bottom of chat_function()
|
|
|
|
| 104 |
|
| 105 |
# 2. Research Trigger
|
| 106 |
if any(keyword in user_text.lower() for keyword in ["search", "docs", "latest"]):
|
| 107 |
+
# Use a fast micro-turn to distill the massive user prompt into optimized keywords
|
| 108 |
+
distill_response = client.chat.completions.create(
|
| 109 |
+
model="llama-3.1-8b-instant",
|
| 110 |
+
messages=[
|
| 111 |
+
{"role": "system", "content": "You are a search query optimizer. Convert the user's long request into a single, clean search query for a search engine. Output ONLY the search keywords, under 60 characters total. Do not use quotes, markdown, or punctuation."},
|
| 112 |
+
{"role": "user", "content": user_text}
|
| 113 |
+
],
|
| 114 |
+
temperature=0.0,
|
| 115 |
+
)
|
| 116 |
+
optimized_query = distill_response.choices[0].message.content.strip()
|
| 117 |
+
|
| 118 |
+
# Executing clean, highly target web search under the 400-character cap
|
| 119 |
+
research_context = web_search(optimized_query)
|
| 120 |
+
prompt = f"RESEARCH:\n{research_context}\n\nFILES:\n{context_from_files}\n\nUSER: {optimized_query}"
|
| 121 |
+
#research_context = web_search(user_text)
|
| 122 |
+
#prompt = f"RESEARCH:\n{research_context}\n\nFILES:\n{context_from_files}\n\nUSER: {user_text}"
|
| 123 |
else:
|
| 124 |
prompt = f"FILES:\n{context_from_files}\n\nUSER: {user_text}"
|
| 125 |
|
|
|
|
| 129 |
# ONLY KEEP LAST 3 TURNS: This is the 'Master Stroke' for staying under 6k TPM
|
| 130 |
for turn in history[-3:]:
|
| 131 |
messages.append({"role": turn["role"], "content": turn["content"]})
|
| 132 |
+
|
| 133 |
messages.append({"role": "user", "content": prompt})
|
| 134 |
|
| 135 |
# Spliced directly into ./core_logic.py at the bottom of chat_function()
|