wony617
commited on
Commit
Β·
a487d1c
1
Parent(s):
859119b
Update configurations as a control box
Browse files- agent/handler.py +115 -41
- app.py +51 -39
agent/handler.py
CHANGED
@@ -19,21 +19,42 @@ from translator.project_config import get_available_projects, get_project_config
|
|
19 |
# State management
|
20 |
class ChatState:
|
21 |
def __init__(self):
|
22 |
-
self.step = "welcome" # welcome ->
|
23 |
-
|
|
|
|
|
24 |
self.target_language = "ko"
|
25 |
self.k_files = 10
|
26 |
self.files_to_translate = []
|
27 |
self.additional_instruction = ""
|
28 |
self.current_file_content = {"translated": ""}
|
29 |
-
self.pr_result = None
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
"
|
34 |
-
"
|
35 |
-
|
|
|
|
|
|
|
|
|
36 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
|
39 |
state = ChatState()
|
@@ -74,7 +95,22 @@ def process_file_search_handler(project: str, lang: str, k: int, history: list)
|
|
74 |
state.k_files = k
|
75 |
state.step = "find_files"
|
76 |
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
state.files_to_translate = (
|
79 |
[file[0] for file in files_list]
|
80 |
if files_list
|
@@ -289,30 +325,40 @@ def sync_language_displays(lang):
|
|
289 |
return lang
|
290 |
|
291 |
|
292 |
-
def
|
293 |
-
"""Update
|
294 |
global state
|
295 |
-
|
296 |
-
#
|
297 |
-
if
|
298 |
-
|
|
|
|
|
|
|
|
|
299 |
|
300 |
# Get default reference PR URL from project config if not provided
|
301 |
-
if not reference_pr_url:
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
{
|
308 |
-
"token": token,
|
309 |
-
"owner": owner,
|
310 |
-
"repo_name": repo,
|
311 |
-
"reference_pr_url": reference_pr_url,
|
312 |
-
}
|
313 |
-
)
|
314 |
|
315 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
316 |
|
317 |
|
318 |
def update_prompt_preview(language, file_path, additional_instruction):
|
@@ -348,7 +394,14 @@ def send_message(message, history):
|
|
348 |
|
349 |
|
350 |
# Button handlers with tab switching
|
351 |
-
def start_translate_handler(history,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
352 |
os.environ["ANTHROPIC_API_KEY"] = anthropic_key
|
353 |
|
354 |
state.additional_instruction = additional_instruction
|
@@ -363,17 +416,24 @@ def approve_handler(history, owner, repo, reference_pr_url):
|
|
363 |
global state
|
364 |
state.step = "create_github_pr"
|
365 |
|
366 |
-
#
|
367 |
-
state.
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
# Validate GitHub configuration
|
372 |
-
github_config = state.github_config
|
373 |
-
if not all([github_config.get("token"), owner, repo]):
|
374 |
-
response = "β GitHub configuration incomplete. Please provide GitHub Token, Owner, and Repository Name in Tab 3."
|
375 |
history.append(["GitHub PR creation request", response])
|
376 |
return history, "", update_status()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
377 |
|
378 |
# If reference PR is not provided, use the agent to find one
|
379 |
if not github_config.get("reference_pr_url"):
|
@@ -440,9 +500,23 @@ def approve_handler(history, owner, repo, reference_pr_url):
|
|
440 |
|
441 |
|
442 |
def restart_handler(history):
|
443 |
-
"""Resets the state
|
444 |
global state
|
|
|
|
|
|
|
|
|
445 |
state = ChatState()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
446 |
welcome_msg = get_welcome_message()
|
447 |
new_hist = [[None, welcome_msg]]
|
448 |
return new_hist, "", update_status(), gr.Tabs(selected=0)
|
|
|
19 |
# State management
|
20 |
class ChatState:
|
21 |
def __init__(self):
|
22 |
+
self.step = "welcome" # welcome -> find_files -> translate -> create_github_pr
|
23 |
+
|
24 |
+
# Transient state (reset on restart)
|
25 |
+
self.selected_project = "transformers"
|
26 |
self.target_language = "ko"
|
27 |
self.k_files = 10
|
28 |
self.files_to_translate = []
|
29 |
self.additional_instruction = ""
|
30 |
self.current_file_content = {"translated": ""}
|
31 |
+
self.pr_result = None
|
32 |
+
|
33 |
+
# Persistent settings (preserved across restarts)
|
34 |
+
self.persistent_settings = {
|
35 |
+
"anthropic_api_key": "",
|
36 |
+
"github_config": {
|
37 |
+
"token": "",
|
38 |
+
"owner": "",
|
39 |
+
"repo_name": "",
|
40 |
+
"reference_pr_url": "",
|
41 |
+
}
|
42 |
}
|
43 |
+
|
44 |
+
def reset_transient_state(self):
|
45 |
+
"""Reset only the workflow state, keep persistent settings"""
|
46 |
+
self.step = "welcome"
|
47 |
+
self.selected_project = "transformers"
|
48 |
+
self.target_language = "ko"
|
49 |
+
self.k_files = 10
|
50 |
+
self.files_to_translate = []
|
51 |
+
self.additional_instruction = ""
|
52 |
+
self.current_file_content = {"translated": ""}
|
53 |
+
self.pr_result = None
|
54 |
+
|
55 |
+
@property
|
56 |
+
def github_config(self):
|
57 |
+
return self.persistent_settings["github_config"]
|
58 |
|
59 |
|
60 |
state = ChatState()
|
|
|
95 |
state.k_files = k
|
96 |
state.step = "find_files"
|
97 |
|
98 |
+
try:
|
99 |
+
status_report, files_list = report_translation_target_files(project, lang, k)
|
100 |
+
except Exception as e:
|
101 |
+
if "rate limit" in str(e).lower():
|
102 |
+
response = f"""β **GitHub API Rate Limit Exceeded**
|
103 |
+
|
104 |
+
{str(e)}
|
105 |
+
|
106 |
+
**π‘ To fix this:**
|
107 |
+
1. Set GitHub Token in Configuration panel above
|
108 |
+
2. Click "πΎ Save Configuration"
|
109 |
+
3. Try "Find Files" again"""
|
110 |
+
history.append(["File search request", response])
|
111 |
+
return history, "", update_status(), gr.Tabs(selected=0), gr.update(choices=[])
|
112 |
+
else:
|
113 |
+
raise # Re-raise non-rate-limit errors
|
114 |
state.files_to_translate = (
|
115 |
[file[0] for file in files_list]
|
116 |
if files_list
|
|
|
325 |
return lang
|
326 |
|
327 |
|
328 |
+
def update_persistent_config(anthropic_key, github_token, github_owner, github_repo, reference_pr_url):
|
329 |
+
"""Update persistent configuration settings."""
|
330 |
global state
|
331 |
+
|
332 |
+
# Update API keys
|
333 |
+
if anthropic_key:
|
334 |
+
state.persistent_settings["anthropic_api_key"] = anthropic_key
|
335 |
+
os.environ["ANTHROPIC_API_KEY"] = anthropic_key
|
336 |
+
|
337 |
+
if github_token:
|
338 |
+
os.environ["GITHUB_TOKEN"] = github_token
|
339 |
|
340 |
# Get default reference PR URL from project config if not provided
|
341 |
+
if not reference_pr_url and state.selected_project:
|
342 |
+
try:
|
343 |
+
config = get_project_config(state.selected_project)
|
344 |
+
reference_pr_url = config.reference_pr_url
|
345 |
+
except:
|
346 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
347 |
|
348 |
+
# Save GitHub configuration to persistent settings
|
349 |
+
state.persistent_settings["github_config"].update({
|
350 |
+
"token": github_token or "",
|
351 |
+
"owner": github_owner or "",
|
352 |
+
"repo_name": github_repo or "",
|
353 |
+
"reference_pr_url": reference_pr_url or "",
|
354 |
+
})
|
355 |
+
|
356 |
+
return f"β
Configuration saved! GitHub: {github_owner}/{github_repo}"
|
357 |
+
|
358 |
+
|
359 |
+
def update_github_config(token, owner, repo, reference_pr_url):
|
360 |
+
"""Legacy function for backward compatibility."""
|
361 |
+
return update_persistent_config("", token, owner, repo, reference_pr_url)
|
362 |
|
363 |
|
364 |
def update_prompt_preview(language, file_path, additional_instruction):
|
|
|
394 |
|
395 |
|
396 |
# Button handlers with tab switching
|
397 |
+
def start_translate_handler(history, file_to_translate, additional_instruction=""):
|
398 |
+
# Use persistent anthropic key
|
399 |
+
anthropic_key = state.persistent_settings["anthropic_api_key"]
|
400 |
+
if not anthropic_key:
|
401 |
+
response = "β Please set Anthropic API key in Configuration panel first."
|
402 |
+
history.append(["Translation request", response])
|
403 |
+
return history, "", update_status(), gr.Tabs(selected=0)
|
404 |
+
|
405 |
os.environ["ANTHROPIC_API_KEY"] = anthropic_key
|
406 |
|
407 |
state.additional_instruction = additional_instruction
|
|
|
416 |
global state
|
417 |
state.step = "create_github_pr"
|
418 |
|
419 |
+
# Use persistent settings for token, update other values
|
420 |
+
github_config = state.persistent_settings["github_config"]
|
421 |
+
if not github_config.get("token"):
|
422 |
+
response = "β Please set GitHub Token in Configuration panel first."
|
|
|
|
|
|
|
|
|
|
|
423 |
history.append(["GitHub PR creation request", response])
|
424 |
return history, "", update_status()
|
425 |
+
|
426 |
+
if not owner or not repo:
|
427 |
+
response = "β Please set GitHub Owner and Repository Name in Configuration panel first."
|
428 |
+
history.append(["GitHub PR creation request", response])
|
429 |
+
return history, "", update_status()
|
430 |
+
|
431 |
+
# Update reference PR URL (can be set per PR)
|
432 |
+
if reference_pr_url:
|
433 |
+
state.persistent_settings["github_config"]["reference_pr_url"] = reference_pr_url
|
434 |
+
|
435 |
+
# Use persistent settings
|
436 |
+
github_config = state.persistent_settings["github_config"]
|
437 |
|
438 |
# If reference PR is not provided, use the agent to find one
|
439 |
if not github_config.get("reference_pr_url"):
|
|
|
500 |
|
501 |
|
502 |
def restart_handler(history):
|
503 |
+
"""Resets the workflow state but preserves persistent settings."""
|
504 |
global state
|
505 |
+
# Backup persistent settings
|
506 |
+
backup_settings = state.persistent_settings.copy()
|
507 |
+
|
508 |
+
# Reset state
|
509 |
state = ChatState()
|
510 |
+
|
511 |
+
# Restore persistent settings
|
512 |
+
state.persistent_settings = backup_settings
|
513 |
+
|
514 |
+
# Restore environment variables
|
515 |
+
if backup_settings["anthropic_api_key"]:
|
516 |
+
os.environ["ANTHROPIC_API_KEY"] = backup_settings["anthropic_api_key"]
|
517 |
+
if backup_settings["github_config"]["token"]:
|
518 |
+
os.environ["GITHUB_TOKEN"] = backup_settings["github_config"]["token"]
|
519 |
+
|
520 |
welcome_msg = get_welcome_message()
|
521 |
new_hist = [[None, welcome_msg]]
|
522 |
return new_hist, "", update_status(), gr.Tabs(selected=0)
|
app.py
CHANGED
@@ -17,6 +17,7 @@ from agent.handler import (
|
|
17 |
update_prompt_preview,
|
18 |
update_status,
|
19 |
update_github_config,
|
|
|
20 |
)
|
21 |
from translator.model import Languages
|
22 |
from translator.project_config import get_available_projects
|
@@ -112,12 +113,54 @@ with gr.Blocks(
|
|
112 |
gr.Markdown("### π Hugging Face i18n Agent")
|
113 |
|
114 |
chatbot = gr.Chatbot(
|
115 |
-
value=[[None, get_welcome_message()]], scale=1, height=
|
116 |
show_copy_button=True
|
117 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
|
119 |
# Controller interface
|
120 |
with gr.Column(scale=2):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
# Quick Controller
|
122 |
with gr.Column(elem_classes=["control-panel"]):
|
123 |
gr.Markdown("### π οΈ Quick Controls")
|
@@ -165,10 +208,6 @@ with gr.Blocks(
|
|
165 |
value="ko",
|
166 |
interactive=False,
|
167 |
)
|
168 |
-
anthropic_key = gr.Textbox(
|
169 |
-
label="π Anthropic API key for translation generation",
|
170 |
-
type="password",
|
171 |
-
)
|
172 |
additional_instruction = gr.Textbox(
|
173 |
label="π Additional instructions (Optional - e.g., custom glossary)",
|
174 |
placeholder="Example: Translate 'model' as 'λͺ¨λΈ' consistently",
|
@@ -190,26 +229,9 @@ with gr.Blocks(
|
|
190 |
|
191 |
with gr.TabItem("3. Upload PR", id=2):
|
192 |
with gr.Group():
|
193 |
-
github_token = gr.Textbox(
|
194 |
-
label="π GitHub Token",
|
195 |
-
type="password",
|
196 |
-
placeholder="ghp_xxxxxxxxxxxxxxxxxxxx",
|
197 |
-
)
|
198 |
-
github_owner = gr.Textbox(
|
199 |
-
label="π€ GitHub Owner/Username",
|
200 |
-
placeholder="your-username",
|
201 |
-
)
|
202 |
-
github_repo = gr.Textbox(
|
203 |
-
label="π Repository Name",
|
204 |
-
placeholder="your-repository",
|
205 |
-
)
|
206 |
reference_pr_url = gr.Textbox(
|
207 |
-
label="π Reference PR URL (Optional
|
208 |
-
placeholder="
|
209 |
-
)
|
210 |
-
|
211 |
-
save_config_btn = gr.Button(
|
212 |
-
"πΎ Save GitHub Config", elem_classes="action-button"
|
213 |
)
|
214 |
approve_btn = gr.Button(
|
215 |
"β
Generate GitHub PR", elem_classes="action-button"
|
@@ -218,16 +240,6 @@ with gr.Blocks(
|
|
218 |
"π Restart Translation", elem_classes="action-button"
|
219 |
)
|
220 |
|
221 |
-
# Chat Controller
|
222 |
-
with gr.Column(elem_classes=["control-panel"]):
|
223 |
-
gr.Markdown("### π¬ Chat with agent (Only simple chat is available)")
|
224 |
-
msg_input = gr.Textbox(
|
225 |
-
placeholder="Type your message here... (e.g. 'what', 'how', or 'help')",
|
226 |
-
container=False,
|
227 |
-
scale=4,
|
228 |
-
)
|
229 |
-
send_btn = gr.Button("Send", scale=1, elem_classes="action-button")
|
230 |
-
|
231 |
# Event Handlers
|
232 |
|
233 |
find_btn.click(
|
@@ -253,20 +265,20 @@ with gr.Blocks(
|
|
253 |
# Button event handlers
|
254 |
start_translate_btn.click(
|
255 |
fn=start_translate_handler,
|
256 |
-
inputs=[chatbot,
|
257 |
outputs=[chatbot, msg_input, status_display, control_tabs],
|
258 |
)
|
259 |
|
260 |
-
#
|
261 |
save_config_btn.click(
|
262 |
-
fn=
|
263 |
-
inputs=[
|
264 |
outputs=[msg_input],
|
265 |
)
|
266 |
|
267 |
approve_btn.click(
|
268 |
fn=approve_handler,
|
269 |
-
inputs=[chatbot,
|
270 |
outputs=[chatbot, msg_input, status_display],
|
271 |
)
|
272 |
|
|
|
17 |
update_prompt_preview,
|
18 |
update_status,
|
19 |
update_github_config,
|
20 |
+
update_persistent_config,
|
21 |
)
|
22 |
from translator.model import Languages
|
23 |
from translator.project_config import get_available_projects
|
|
|
113 |
gr.Markdown("### π Hugging Face i18n Agent")
|
114 |
|
115 |
chatbot = gr.Chatbot(
|
116 |
+
value=[[None, get_welcome_message()]], scale=1, height=525,
|
117 |
show_copy_button=True
|
118 |
)
|
119 |
+
|
120 |
+
# Chat input directly under main chat
|
121 |
+
gr.Markdown("### π¬ Chat with agent")
|
122 |
+
with gr.Row():
|
123 |
+
msg_input = gr.Textbox(
|
124 |
+
placeholder="Type your message here... (e.g. 'what', 'how', or 'help')",
|
125 |
+
container=False,
|
126 |
+
scale=4,
|
127 |
+
)
|
128 |
+
send_btn = gr.Button("Send", scale=1, elem_classes="action-button")
|
129 |
|
130 |
# Controller interface
|
131 |
with gr.Column(scale=2):
|
132 |
+
# Configuration Panel
|
133 |
+
with gr.Column(elem_classes=["control-panel"]):
|
134 |
+
gr.Markdown("### βοΈ Configuration")
|
135 |
+
|
136 |
+
with gr.Accordion("π§ API & GitHub Settings", open=True):
|
137 |
+
config_anthropic_key = gr.Textbox(
|
138 |
+
label="π Anthropic API Key",
|
139 |
+
type="password",
|
140 |
+
placeholder="sk-ant-...",
|
141 |
+
)
|
142 |
+
config_github_token = gr.Textbox(
|
143 |
+
label="π GitHub Token (Required for PR, Optional for file search)",
|
144 |
+
type="password",
|
145 |
+
placeholder="ghp_...",
|
146 |
+
)
|
147 |
+
|
148 |
+
with gr.Row():
|
149 |
+
config_github_owner = gr.Textbox(
|
150 |
+
label="π€ GitHub Owner",
|
151 |
+
placeholder="your-username",
|
152 |
+
scale=1,
|
153 |
+
)
|
154 |
+
config_github_repo = gr.Textbox(
|
155 |
+
label="π Repository Name",
|
156 |
+
placeholder="your-repository",
|
157 |
+
scale=1,
|
158 |
+
)
|
159 |
+
|
160 |
+
save_config_btn = gr.Button(
|
161 |
+
"πΎ Save Configuration", elem_classes="action-button"
|
162 |
+
)
|
163 |
+
|
164 |
# Quick Controller
|
165 |
with gr.Column(elem_classes=["control-panel"]):
|
166 |
gr.Markdown("### π οΈ Quick Controls")
|
|
|
208 |
value="ko",
|
209 |
interactive=False,
|
210 |
)
|
|
|
|
|
|
|
|
|
211 |
additional_instruction = gr.Textbox(
|
212 |
label="π Additional instructions (Optional - e.g., custom glossary)",
|
213 |
placeholder="Example: Translate 'model' as 'λͺ¨λΈ' consistently",
|
|
|
229 |
|
230 |
with gr.TabItem("3. Upload PR", id=2):
|
231 |
with gr.Group():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
232 |
reference_pr_url = gr.Textbox(
|
233 |
+
label="π Reference PR URL (Optional)",
|
234 |
+
placeholder="Auto-filled based on project selection",
|
|
|
|
|
|
|
|
|
235 |
)
|
236 |
approve_btn = gr.Button(
|
237 |
"β
Generate GitHub PR", elem_classes="action-button"
|
|
|
240 |
"π Restart Translation", elem_classes="action-button"
|
241 |
)
|
242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
243 |
# Event Handlers
|
244 |
|
245 |
find_btn.click(
|
|
|
265 |
# Button event handlers
|
266 |
start_translate_btn.click(
|
267 |
fn=start_translate_handler,
|
268 |
+
inputs=[chatbot, file_to_translate_input, additional_instruction],
|
269 |
outputs=[chatbot, msg_input, status_display, control_tabs],
|
270 |
)
|
271 |
|
272 |
+
# Configuration Save
|
273 |
save_config_btn.click(
|
274 |
+
fn=update_persistent_config,
|
275 |
+
inputs=[config_anthropic_key, config_github_token, config_github_owner, config_github_repo, reference_pr_url],
|
276 |
outputs=[msg_input],
|
277 |
)
|
278 |
|
279 |
approve_btn.click(
|
280 |
fn=approve_handler,
|
281 |
+
inputs=[chatbot, config_github_owner, config_github_repo, reference_pr_url],
|
282 |
outputs=[chatbot, msg_input, status_display],
|
283 |
)
|
284 |
|