Skip to content

Commit ec4667c

Browse files
committed
code cleanup
1 parent 549219e commit ec4667c

File tree

1 file changed

+2
-45
lines changed

1 file changed

+2
-45
lines changed

app/app.py

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -101,42 +101,33 @@ def load_languages() -> dict[str, str]:
101101
languages_path, use_persistent = get_persistent_storage_path("languages.json")
102102
local_path = Path(__file__).parent / "languages.json"
103103

104-
# If persistent storage is available but file doesn't exist yet,
105-
# copy the local file to persistent storage
104+
# If persistent storage is available but file doesn't exist yet, copy the local file to persistent storage
106105
if use_persistent and not languages_path.exists():
107106
try:
108107
if local_path.exists():
109108
import shutil
110-
# Copy the file to persistent storage
111109
shutil.copy(local_path, languages_path)
112110
print(f"Copied languages to persistent storage at {languages_path}")
113111
else:
114-
# Create an empty languages file in persistent storage
115112
with open(languages_path, "w", encoding="utf-8") as f:
116113
json.dump({"English": "You are a helpful assistant."}, f, ensure_ascii=False, indent=2)
117114
print(f"Created new languages file in persistent storage at {languages_path}")
118115
except Exception as e:
119116
print(f"Error setting up persistent storage: {e}")
120117
languages_path = local_path # Fall back to local path if any error occurs
121118

122-
# If the file doesn't exist at the chosen path but exists at the local path, use local
123119
if not languages_path.exists() and local_path.exists():
124120
languages_path = local_path
125121

126-
# If the file exists, load it
127122
if languages_path.exists():
128123
with open(languages_path, "r", encoding="utf-8") as f:
129124
return json.load(f)
130125
else:
131-
# Return a default if no file exists
132126
default_languages = {"English": "You are a helpful assistant."}
133127
return default_languages
134128

135-
136-
# Initial load
137129
LANGUAGES = load_languages()
138130

139-
# User agreement text
140131
USER_AGREEMENT = """
141132
You have been asked to participate in a research study conducted by Lingo Lab from the Computer Science and Artificial Intelligence Laboratory at the Massachusetts Institute of Technology (M.I.T.), together with huggingface.
142133
@@ -275,22 +266,19 @@ def add_fake_like_data(
275266
def call_pipeline(messages: list, language: str):
276267
"""Call the appropriate model pipeline based on configuration"""
277268
if ZERO_GPU:
278-
# Format the messages using the tokenizer's chat template
279269
tokenizer = CLIENT["tokenizer"]
280270
formatted_prompt = tokenizer.apply_chat_template(
281271
messages,
282272
tokenize=False,
283273
)
284274

285-
# Call the pipeline with the formatted text
286275
response = CLIENT["pipeline"](
287276
formatted_prompt,
288277
clean_up_tokenization_spaces=False,
289278
max_length=2000,
290279
return_full_text=False,
291280
)
292281

293-
# Extract the generated content
294282
return response[0]["generated_text"]
295283
else:
296284
response = CLIENT(
@@ -435,7 +423,6 @@ def wrangle_edit_data(
435423
)
436424
return history
437425
else:
438-
# Add feedback on original and corrected message
439426
add_fake_like_data(
440427
history=history[: index + 1],
441428
conversation_id=conversation_id,
@@ -450,7 +437,6 @@ def wrangle_edit_data(
450437
language=language,
451438
)
452439
history = history[: index + 1]
453-
# add chosen and rejected options
454440
history[-1]["options"] = [
455441
Option(label="chosen", value=x.value),
456442
Option(label="rejected", value=original_message["content"]),
@@ -514,38 +500,30 @@ def close_add_language_modal():
514500

515501
def save_new_language(lang_name, system_prompt):
516502
"""Save the new language and system prompt to persistent storage if available, otherwise to local file."""
517-
global LANGUAGES # Access the global variable
503+
global LANGUAGES
518504

519-
# Get the appropriate path
520505
languages_path, use_persistent = get_persistent_storage_path("languages.json")
521506
local_path = Path(__file__).parent / "languages.json"
522507

523-
# Load existing languages
524508
if languages_path.exists():
525509
with open(languages_path, "r", encoding="utf-8") as f:
526510
data = json.load(f)
527511
else:
528512
data = {}
529513

530-
# Add the new language to JSON
531514
data[lang_name] = system_prompt
532515

533-
# Save the updated languages
534516
with open(languages_path, "w", encoding="utf-8") as f:
535517
json.dump(data, f, ensure_ascii=False, indent=2)
536518

537-
# If we're using persistent storage, also update the local file as backup
538519
if use_persistent and local_path != languages_path:
539520
try:
540521
with open(local_path, "w", encoding="utf-8") as f:
541522
json.dump(data, f, ensure_ascii=False, indent=2)
542523
except Exception as e:
543524
print(f"Error updating local backup: {e}")
544525

545-
# Update the global LANGUAGES variable with the new data
546526
LANGUAGES.update({lang_name: system_prompt})
547-
548-
# Return a message that will trigger a JavaScript refresh
549527
return gr.Group(visible=False), gr.HTML("<script>window.location.reload();</script>"), gr.Dropdown(choices=list(LANGUAGES.keys()))
550528

551529

@@ -570,20 +548,6 @@ def save_new_language(lang_name, system_prompt):
570548
box-shadow: 0 2px 5px rgba(0,0,0,0.1) !important;
571549
}
572550
"""
573-
# /* Style for the user agreement container */
574-
# .user-agreement-container {
575-
# background-color: white !important;
576-
# box-shadow: 0 2px 5px rgba(0,0,0,0.1) !important;
577-
# }
578-
# /* Ensure the markdown inside the container inherits the background */
579-
# .user-agreement-container > div {
580-
# background-color: white !important;
581-
# }
582-
# /* Target all elements inside the container */
583-
# .user-agreement-container * {
584-
# background-color: white !important;
585-
# }
586-
# """
587551

588552
with gr.Blocks(css=css) as demo:
589553
# State variable to track if user has consented
@@ -643,12 +607,9 @@ def save_new_language(lang_name, system_prompt):
643607
with gr.Row():
644608
with gr.Column(scale=1):
645609
save_language_btn = gr.Button("Save")
646-
# with gr.Column(scale=0.2):
647-
# pass # Empty column as spacer
648610
with gr.Column(scale=1):
649611
cancel_language_btn = gr.Button("Cancel")
650612

651-
# Add a hidden HTML component for page refresh
652613
refresh_html = gr.HTML(visible=False)
653614

654615
session_id = gr.Textbox(
@@ -756,13 +717,9 @@ def show_main_app():
756717

757718
def on_app_load():
758719
global LANGUAGES
759-
# Force reload languages from file
760720
LANGUAGES = load_languages()
761-
762-
# Get the list of languages
763721
language_choices = list(LANGUAGES.keys())
764722

765-
# Return both the session ID and available language choices
766723
return str(uuid.uuid4()), gr.Dropdown(choices=language_choices, value=language_choices[0])
767724

768725
demo.load(

0 commit comments

Comments
 (0)