Spaces:
Running
Running
File size: 2,320 Bytes
5401755 |
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 |
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()
|