Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
# Simulated user data for demonstration
|
4 |
+
user_data = {"hello": "hello"}
|
5 |
+
|
6 |
+
# Sample English text to translate
|
7 |
+
english_text = "Translate this text to Vietnamese."
|
8 |
+
|
9 |
+
# User session dictionary to store logged-in status
|
10 |
+
user_sessions = {}
|
11 |
+
|
12 |
+
def login(username, password):
|
13 |
+
# Authenticate user
|
14 |
+
if username in user_data and user_data[username] == password:
|
15 |
+
user_sessions[username] = True
|
16 |
+
gr.Info("Login successfully. Welcome!")
|
17 |
+
return gr.update(visible=False), gr.update(visible=True)
|
18 |
+
else:
|
19 |
+
raise gr.Error("Username or Password is invalid! Try again!")
|
20 |
+
return gr.update(visible=True), gr.update(visible=False)
|
21 |
+
|
22 |
+
def logout(username):
|
23 |
+
# Log out user and reset session
|
24 |
+
if username in user_sessions:
|
25 |
+
del user_sessions[username]
|
26 |
+
gr.Warning("You need to login to access this subnet and do task ⛔️!", duration=5)
|
27 |
+
return gr.update(visible=True), gr.update(visible=False)
|
28 |
+
|
29 |
+
def submit_translation(translation):
|
30 |
+
# Save the translation and provide feedback
|
31 |
+
gr.Info("Submit successfully")
|
32 |
+
|
33 |
+
|
34 |
+
# Define the Gradio interface
|
35 |
+
with gr.Blocks() as demo:
|
36 |
+
# Login section
|
37 |
+
with gr.Column(visible=True) as login_section:
|
38 |
+
username_input = gr.Textbox(placeholder="Enter your username", label="Username")
|
39 |
+
password_input = gr.Textbox(placeholder="Enter your password", label="Password", type="password")
|
40 |
+
login_button = gr.Button("Login")
|
41 |
+
|
42 |
+
|
43 |
+
# Working section (initially hidden)
|
44 |
+
with gr.Column(visible=False) as translation_section:
|
45 |
+
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", label= "Question", interactive=False)
|
46 |
+
#code = gr.Textbox(placeholder="import numpy as np\ndef calculate(...):\n ...\n return results\n...\nprint(results)", label="Code from AI model", lines = 6)
|
47 |
+
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)
|
48 |
+
submit_button = gr.Button("Submit code")
|
49 |
+
logout_button = gr.Button("Logout")
|
50 |
+
|
51 |
+
# Button functions
|
52 |
+
password_input.submit(
|
53 |
+
login, inputs=[username_input, password_input], outputs=[login_section, translation_section]
|
54 |
+
)
|
55 |
+
|
56 |
+
login_button.click(
|
57 |
+
login, inputs=[username_input, password_input], outputs=[login_section, translation_section]
|
58 |
+
)
|
59 |
+
submit_button.click(
|
60 |
+
submit_translation, inputs=code_box)
|
61 |
+
|
62 |
+
logout_button.click(
|
63 |
+
logout, inputs=[username_input], outputs=[login_section, translation_section]
|
64 |
+
)
|
65 |
+
|
66 |
+
#demo.launch(auth = ('admin', 'admin'))
|
67 |
+
demo.launch()
|