mathviacode / app.py
anhdt-dsai-02's picture
Update app.py
fffd6fc verified
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
gr.Info("Login successfully. Welcome!")
return gr.update(visible=False), gr.update(visible=True)
else:
raise gr.Error("Username or Password is invalid! Try again!")
return 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]
gr.Warning("You need to login to access this subnet and do task ⛔️!", duration=5)
return gr.update(visible=True), gr.update(visible=False)
def submit_translation(translation):
# Save the translation and provide feedback
gr.Info("Submit successfully")
# 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")
# Working section (initially hidden)
with gr.Column(visible=False) as translation_section:
gr.Textbox(value="How many positive integer factors of 2020 have more than 3 factors? (As an example, 12 has 6 factors, namely 1,2,3,4,6, and 12. \n(A) 6(B) 7(C) 8(D) 9(E) 10", lines = 6, label= "Question", interactive=False)
#code = gr.Textbox(placeholder="import numpy as np\ndef calculate(...):\n ...\n return results\n...\nprint(results)", label="Code from AI model", lines = 6)
code_box = gr.Code(language="python", value="import numpy as np\ndef calculate(...):\n ...\n return results\n...\nprint(results)", label="Code from AI model", interactive = True)
submit_button = gr.Button("Submit code")
logout_button = gr.Button("Logout")
# Button functions
password_input.submit(
login, inputs=[username_input, password_input], outputs=[login_section, translation_section]
)
login_button.click(
login, inputs=[username_input, password_input], outputs=[login_section, translation_section]
)
submit_button.click(
submit_translation, inputs=code_box)
logout_button.click(
logout, inputs=[username_input], outputs=[login_section, translation_section]
)
#demo.launch(auth = ('admin', 'admin'))
demo.launch()