vietdata's picture
first update
5401755
raw
history blame
2.32 kB
import gradio as gr
# Simulated user data for demonstration
user_data = {"hello": "hello"}
# Sample English text to translate
english_text = "Translate this text to Vietnamese."
# User session dictionary to store logged-in status
user_sessions = {}
def login(username, password):
# Authenticate user
if username in user_data and user_data[username] == password:
user_sessions[username] = True
return f"Welcome, {username}!", gr.update(visible=False), gr.update(visible=True)
else:
return "Invalid username or password.", gr.update(visible=True), gr.update(visible=False)
def logout(username):
# Log out user and reset session
if username in user_sessions:
del user_sessions[username]
return "Logged out. Please log in again.", gr.update(visible=True), gr.update(visible=False)
def submit_translation(translation):
# Save the translation and provide feedback
return f"Translation submitted: {translation}"
# Define the Gradio interface
with gr.Blocks() as demo:
# Login section
with gr.Column(visible=True) as login_section:
username_input = gr.Textbox(placeholder="Enter your username", label="Username")
password_input = gr.Textbox(placeholder="Enter your password", label="Password", type="password")
login_button = gr.Button("Login")
login_output = gr.Textbox(label="Login Status", interactive=False)
# Translation section (initially hidden)
with gr.Column(visible=False) as translation_section:
gr.Textbox(value=english_text, label="English Text", interactive=False)
translation_input = gr.Textbox(placeholder="Enter your translation here", label="Your Translation")
submit_button = gr.Button("Submit Translation")
translation_output = gr.Textbox(label="Submission Status", interactive=False)
logout_button = gr.Button("Logout")
# Button functions
login_button.click(
login, inputs=[username_input, password_input], outputs=[login_output, login_section, translation_section]
)
submit_button.click(
submit_translation, inputs=translation_input, outputs=translation_output
)
logout_button.click(
logout, inputs=[username_input], outputs=[login_output, login_section, translation_section]
)
demo.launch()