File size: 8,629 Bytes
3bfe3dd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
"""Module for gradio chat-based translation agent interface."""
import base64
import os
import gradio as gr
from dotenv import load_dotenv
from agent.handler import (
approve_handler,
get_welcome_message,
process_file_search_handler,
restart_handler,
send_message,
start_translate_handler,
sync_language_displays,
update_status,
update_github_config,
)
from translator.model import Languages
load_dotenv()
css = """
.gradio-container {
background: linear-gradient(135deg, #ffeda7 0%, #ffbebf 100%);
}
.chat-container {
background: rgba(255, 255, 180, 0.25);
border-radius: 18px;
box-shadow: 0 4px 24px rgba(0,0,0,0.08);
padding: 1.5em;
backdrop-filter: blur(8px);
border: 1px solid rgba(255,255,180,0.25);
width: 100%;
height: 100%;
}
.control-panel {
background: rgba(255, 255, 180, 0.25);
border-radius: 18px;
box-shadow: 0 4px 24px rgba(0,0,0,0.08);
padding: 1.5em;
backdrop-filter: blur(8px);
border: 1px solid rgba(255,255,180,0.25);
width: 100%;
}
.status-card {
width: 100%
}
.action-button {
background: linear-gradient(135deg, #ff8c8c 0%, #f9a889 100%) !important;
color: white !important;
border: none !important;
font-weight: 600 !important;
box-shadow: 0 4px 12px rgba(0,0,0,0.15) !important;
transition: all 0.3s ease-in-out !important;
}
.action-button:hover {
background: linear-gradient(135deg, #f9a889 0%, #ff8c8c 100%) !important;
box-shadow: 0 6px 16px rgba(0,0,0,0.2) !important;
transform: translateY(-2px) !important;
}
.simple-tabs .tab-nav button {
background: transparent !important;
color: #4A5568 !important;
box-shadow: none !important;
transform: none !important;
border: none !important;
border-bottom: 2px solid #E2E8F0 !important;
border-radius: 0 !important;
font-weight: 600 !important;
}
.simple-tabs .tab-nav button.selected {
color: #f97316 !important;
border-bottom: 2px solid #f97316 !important;
}
.simple-tabs .tab-nav button:hover {
background: #f3f4f6 !important;
color: #f97316 !important;
box-shadow: none !important;
transform: none !important;
}
"""
# Create the main interface
with gr.Blocks(
css=css, title=" π Hugging Face Transformers Docs i18n made easy"
) as demo:
# Title
with open("images/hfkr_logo.png", "rb") as img_file:
base64_img = base64.b64encode(img_file.read()).decode()
gr.Markdown(
f'<img src="data:image/png;base64,{base64_img}" style="display: block; margin-left: auto; margin-right: auto; height: 15em;"/>'
)
gr.Markdown(
'<h1 style="text-align: center;"> π Hugging Face Transformers Docs i18n made easy</h1>'
)
# Content
with gr.Row():
# Chat interface
with gr.Column(scale=4, elem_classes=["chat-container"]):
gr.Markdown("### π Hugging Face i18n Agent")
chatbot = gr.Chatbot(
value=[[None, get_welcome_message()]], scale=1, height=585
)
# Controller interface
with gr.Column(scale=2):
# Quick Controller
with gr.Column(elem_classes=["control-panel"]):
gr.Markdown("### π οΈ Quick Controls")
status_display = gr.HTML(update_status())
with gr.Tabs(elem_classes="simple-tabs") as control_tabs:
with gr.TabItem("1. Find Files", id=0):
with gr.Group():
lang_dropdown = gr.Dropdown(
choices=[language.value for language in Languages],
label="π Translate To",
value="ko",
)
k_input = gr.Number(
label="π First k missing translated docs",
value=1,
minimum=1,
maximum=100,
)
find_btn = gr.Button(
"π Find Files to Translate",
elem_classes="action-button",
)
with gr.TabItem("2. Translate", id=1):
with gr.Group():
translate_lang_display = gr.Dropdown(
choices=[language.value for language in Languages],
label="π Translation Language",
value="ko",
interactive=False,
)
anthropic_key = gr.Textbox(
label="π Anthropic API key for translation generation",
type="password",
)
start_translate_btn = gr.Button(
"π Start Translation", elem_classes="action-button"
)
with gr.TabItem("3. Upload PR", id=2):
with gr.Group():
github_token = gr.Textbox(
label="π GitHub Token",
type="password",
placeholder="ghp_xxxxxxxxxxxxxxxxxxxx",
)
github_owner = gr.Textbox(
label="π€ GitHub Owner/Username",
placeholder="your-username",
)
github_repo = gr.Textbox(
label="π Repository Name",
placeholder="your-repository",
)
reference_pr_url = gr.Textbox(
label="π Reference PR URL (Optional - Agent will find one if not provided)",
placeholder="reference PR URL",
)
save_config_btn = gr.Button(
"πΎ Save GitHub Config", elem_classes="action-button"
)
approve_btn = gr.Button(
"β
Generate GitHub PR", elem_classes="action-button"
)
restart_btn = gr.Button(
"π Restart Translation", elem_classes="action-button"
)
# Chat Controller
with gr.Column(elem_classes=["control-panel"]):
gr.Markdown("### π¬ Chat with agent")
msg_input = gr.Textbox(
placeholder="Type your message here... (e.g. 'what', 'how', or 'help')",
container=False,
scale=4,
)
send_btn = gr.Button("Send", scale=1, elem_classes="action-button")
# Event Handlers
find_btn.click(
fn=process_file_search_handler,
inputs=[lang_dropdown, k_input, chatbot],
outputs=[chatbot, msg_input, status_display, control_tabs],
)
# Sync language across tabs
lang_dropdown.change(
fn=sync_language_displays,
inputs=[lang_dropdown],
outputs=[translate_lang_display],
)
# Button event handlers
start_translate_btn.click(
fn=start_translate_handler,
inputs=[chatbot, anthropic_key],
outputs=[chatbot, msg_input, status_display, control_tabs],
)
# GitHub Config Save
save_config_btn.click(
fn=update_github_config,
inputs=[github_token, github_owner, github_repo, reference_pr_url],
outputs=[msg_input],
)
approve_btn.click(
fn=approve_handler,
inputs=[chatbot, github_owner, github_repo, reference_pr_url],
outputs=[chatbot, msg_input, status_display],
)
restart_btn.click(
fn=restart_handler,
inputs=[chatbot],
outputs=[chatbot, msg_input, status_display, control_tabs],
)
send_btn.click(
fn=send_message,
inputs=[msg_input, chatbot],
outputs=[chatbot, msg_input, status_display],
)
msg_input.submit(
fn=send_message,
inputs=[msg_input, chatbot],
outputs=[chatbot, msg_input, status_display],
)
root_path = os.environ.get("GRADIO_ROOT_PATH")
demo.launch(root_path=root_path)
|