Spaces:
Paused
Paused
first update
Browse files
app.py
CHANGED
|
@@ -1,4 +1,64 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
# Simulated user data for demonstration
|
| 4 |
user_data = {"hello": "hello"}
|
|
@@ -9,13 +69,14 @@ english_text = "Translate this text to Vietnamese."
|
|
| 9 |
# User session dictionary to store logged-in status
|
| 10 |
user_sessions = {}
|
| 11 |
|
| 12 |
-
def login(username,
|
|
|
|
| 13 |
# Authenticate user
|
| 14 |
-
if
|
| 15 |
-
user_sessions[username] = True
|
| 16 |
-
return f"Welcome, {username}!", gr.update(visible=False), gr.update(visible=True)
|
| 17 |
else:
|
| 18 |
-
return "Invalid username or password.", gr.update(visible=True), gr.update(visible=False)
|
| 19 |
|
| 20 |
def logout(username):
|
| 21 |
# Log out user and reset session
|
|
@@ -23,22 +84,28 @@ def logout(username):
|
|
| 23 |
del user_sessions[username]
|
| 24 |
return "Logged out. Please log in again.", gr.update(visible=True), gr.update(visible=False)
|
| 25 |
|
| 26 |
-
def submit_translation(translation):
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Define the Gradio interface
|
| 31 |
with gr.Blocks() as demo:
|
|
|
|
| 32 |
# Login section
|
| 33 |
with gr.Column(visible=True) as login_section:
|
| 34 |
-
username_input = gr.Textbox(placeholder="Enter your
|
| 35 |
-
password_input = gr.Textbox(placeholder="Enter your password", label="Password", type="password")
|
| 36 |
login_button = gr.Button("Login")
|
| 37 |
login_output = gr.Textbox(label="Login Status", interactive=False)
|
| 38 |
|
| 39 |
# Translation section (initially hidden)
|
| 40 |
with gr.Column(visible=False) as translation_section:
|
| 41 |
-
gr.Textbox(value=english_text, label="English Text", interactive=False)
|
| 42 |
translation_input = gr.Textbox(placeholder="Enter your translation here", label="Your Translation")
|
| 43 |
submit_button = gr.Button("Submit Translation")
|
| 44 |
translation_output = gr.Textbox(label="Submission Status", interactive=False)
|
|
@@ -46,13 +113,13 @@ with gr.Blocks() as demo:
|
|
| 46 |
|
| 47 |
# Button functions
|
| 48 |
login_button.click(
|
| 49 |
-
login, inputs=[username_input,
|
| 50 |
)
|
| 51 |
submit_button.click(
|
| 52 |
-
submit_translation, inputs=translation_input, outputs=translation_output
|
| 53 |
)
|
| 54 |
logout_button.click(
|
| 55 |
logout, inputs=[username_input], outputs=[login_output, login_section, translation_section]
|
| 56 |
)
|
| 57 |
|
| 58 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from datasets import load_dataset, Dataset
|
| 3 |
+
from collections import defaultdict
|
| 4 |
+
import random
|
| 5 |
+
|
| 6 |
+
# Load the source dataset
|
| 7 |
+
source_dataset = load_dataset("vietdata/eng_echo", split="train")
|
| 8 |
+
source_texts = source_dataset["query"]
|
| 9 |
+
|
| 10 |
+
# Initialize variables
|
| 11 |
+
translations = defaultdict(list)
|
| 12 |
+
processed_data = []
|
| 13 |
+
|
| 14 |
+
# Helper function to get the next text for translation
|
| 15 |
+
def get_next_text(user_id):
|
| 16 |
+
# Filter texts that already have 10 translations
|
| 17 |
+
eligible_texts = [text for text in source_texts if len(translations[text]) < 10]
|
| 18 |
+
if not eligible_texts:
|
| 19 |
+
return "All texts are fully translated."
|
| 20 |
+
|
| 21 |
+
# Select a random eligible text for translation
|
| 22 |
+
next_text = random.choice(eligible_texts)
|
| 23 |
+
return next_text
|
| 24 |
+
|
| 25 |
+
# Function to handle translation submission
|
| 26 |
+
def submit_translation(user_id, original_text, translation):
|
| 27 |
+
# Check if text already has 10 translations
|
| 28 |
+
if len(translations[original_text]) < 10:
|
| 29 |
+
translations[original_text].append((user_id, translation))
|
| 30 |
+
|
| 31 |
+
# Check if 100 texts have enough translations to save
|
| 32 |
+
if len([t for t in translations if len(translations[t]) == 10]) >= 100:
|
| 33 |
+
save_to_translated_echo()
|
| 34 |
+
|
| 35 |
+
return "Translation submitted successfully."
|
| 36 |
+
else:
|
| 37 |
+
return "This text already has 10 translations. Please request a new text."
|
| 38 |
+
|
| 39 |
+
# Function to save completed translations to 'translated_echo'
|
| 40 |
+
def save_to_translated_echo():
|
| 41 |
+
global translations, processed_data
|
| 42 |
+
|
| 43 |
+
# Gather translations with exactly 10 versions
|
| 44 |
+
completed_translations = [
|
| 45 |
+
{"query": text, "translations": [t[1] for t in translations[text]]}
|
| 46 |
+
for text in translations if len(translations[text]) == 10
|
| 47 |
+
]
|
| 48 |
+
|
| 49 |
+
# Append to processed data
|
| 50 |
+
processed_data.extend(completed_translations)
|
| 51 |
+
|
| 52 |
+
# Reset translations
|
| 53 |
+
translations = {text: val for text, val in translations.items() if len(val) < 10}
|
| 54 |
+
|
| 55 |
+
# Convert to Hugging Face dataset format
|
| 56 |
+
translated_dataset = Dataset.from_pandas(pd.DataFrame(processed_data))
|
| 57 |
+
|
| 58 |
+
# Append to Hugging Face dataset (dummy function call)
|
| 59 |
+
translated_dataset.push_to_hub("vietdata/translated_echo", split="train")
|
| 60 |
+
|
| 61 |
+
import gradio as gr
|
| 62 |
|
| 63 |
# Simulated user data for demonstration
|
| 64 |
user_data = {"hello": "hello"}
|
|
|
|
| 69 |
# User session dictionary to store logged-in status
|
| 70 |
user_sessions = {}
|
| 71 |
|
| 72 |
+
def login(username, state):
|
| 73 |
+
state[0] = username
|
| 74 |
# Authenticate user
|
| 75 |
+
if True:
|
| 76 |
+
#user_sessions[username] = True
|
| 77 |
+
return f"Welcome, {username}!", gr.update(visible=False), gr.update(visible=True), get_next_text(username)
|
| 78 |
else:
|
| 79 |
+
return "Invalid username or password.", gr.update(visible=True), gr.update(visible=False), ""
|
| 80 |
|
| 81 |
def logout(username):
|
| 82 |
# Log out user and reset session
|
|
|
|
| 84 |
del user_sessions[username]
|
| 85 |
return "Logged out. Please log in again.", gr.update(visible=True), gr.update(visible=False)
|
| 86 |
|
| 87 |
+
def submit_translation(translation, state, job_input):
|
| 88 |
+
try:
|
| 89 |
+
submit_translation(state[0], job_input, translation)
|
| 90 |
+
origin = job_input
|
| 91 |
+
# Save the translation and provide feedback
|
| 92 |
+
return f"""Translation of "{origin}" submitted: {translation}""", get_next_text(state[0])
|
| 93 |
+
except Exception as e:
|
| 94 |
+
print(e)
|
| 95 |
+
return "Error please try submit again!", job_input
|
| 96 |
|
| 97 |
# Define the Gradio interface
|
| 98 |
with gr.Blocks() as demo:
|
| 99 |
+
state = gr.State([None])
|
| 100 |
# Login section
|
| 101 |
with gr.Column(visible=True) as login_section:
|
| 102 |
+
username_input = gr.Textbox(placeholder="Enter your token", label="Token ID")
|
|
|
|
| 103 |
login_button = gr.Button("Login")
|
| 104 |
login_output = gr.Textbox(label="Login Status", interactive=False)
|
| 105 |
|
| 106 |
# Translation section (initially hidden)
|
| 107 |
with gr.Column(visible=False) as translation_section:
|
| 108 |
+
job_input = gr.Textbox(value=english_text, label="English Text", interactive=False)
|
| 109 |
translation_input = gr.Textbox(placeholder="Enter your translation here", label="Your Translation")
|
| 110 |
submit_button = gr.Button("Submit Translation")
|
| 111 |
translation_output = gr.Textbox(label="Submission Status", interactive=False)
|
|
|
|
| 113 |
|
| 114 |
# Button functions
|
| 115 |
login_button.click(
|
| 116 |
+
login, inputs=[username_input, state], outputs=[login_output, login_section, translation_section, job_input]
|
| 117 |
)
|
| 118 |
submit_button.click(
|
| 119 |
+
submit_translation, inputs=[translation_input, state, job_input], outputs=[translation_output, job_input]
|
| 120 |
)
|
| 121 |
logout_button.click(
|
| 122 |
logout, inputs=[username_input], outputs=[login_output, login_section, translation_section]
|
| 123 |
)
|
| 124 |
|
| 125 |
+
demo.launch(debug=True)
|