Spaces:
Sleeping
Sleeping
idan shenfeld
commited on
Commit
·
ec4667c
1
Parent(s):
549219e
code cleanup
Browse files- app/app.py +2 -45
app/app.py
CHANGED
@@ -101,17 +101,14 @@ def load_languages() -> dict[str, str]:
|
|
101 |
languages_path, use_persistent = get_persistent_storage_path("languages.json")
|
102 |
local_path = Path(__file__).parent / "languages.json"
|
103 |
|
104 |
-
# If persistent storage is available but file doesn't exist yet,
|
105 |
-
# copy the local file to persistent storage
|
106 |
if use_persistent and not languages_path.exists():
|
107 |
try:
|
108 |
if local_path.exists():
|
109 |
import shutil
|
110 |
-
# Copy the file to persistent storage
|
111 |
shutil.copy(local_path, languages_path)
|
112 |
print(f"Copied languages to persistent storage at {languages_path}")
|
113 |
else:
|
114 |
-
# Create an empty languages file in persistent storage
|
115 |
with open(languages_path, "w", encoding="utf-8") as f:
|
116 |
json.dump({"English": "You are a helpful assistant."}, f, ensure_ascii=False, indent=2)
|
117 |
print(f"Created new languages file in persistent storage at {languages_path}")
|
@@ -119,24 +116,18 @@ def load_languages() -> dict[str, str]:
|
|
119 |
print(f"Error setting up persistent storage: {e}")
|
120 |
languages_path = local_path # Fall back to local path if any error occurs
|
121 |
|
122 |
-
# If the file doesn't exist at the chosen path but exists at the local path, use local
|
123 |
if not languages_path.exists() and local_path.exists():
|
124 |
languages_path = local_path
|
125 |
|
126 |
-
# If the file exists, load it
|
127 |
if languages_path.exists():
|
128 |
with open(languages_path, "r", encoding="utf-8") as f:
|
129 |
return json.load(f)
|
130 |
else:
|
131 |
-
# Return a default if no file exists
|
132 |
default_languages = {"English": "You are a helpful assistant."}
|
133 |
return default_languages
|
134 |
|
135 |
-
|
136 |
-
# Initial load
|
137 |
LANGUAGES = load_languages()
|
138 |
|
139 |
-
# User agreement text
|
140 |
USER_AGREEMENT = """
|
141 |
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.
|
142 |
|
@@ -275,14 +266,12 @@ def add_fake_like_data(
|
|
275 |
def call_pipeline(messages: list, language: str):
|
276 |
"""Call the appropriate model pipeline based on configuration"""
|
277 |
if ZERO_GPU:
|
278 |
-
# Format the messages using the tokenizer's chat template
|
279 |
tokenizer = CLIENT["tokenizer"]
|
280 |
formatted_prompt = tokenizer.apply_chat_template(
|
281 |
messages,
|
282 |
tokenize=False,
|
283 |
)
|
284 |
|
285 |
-
# Call the pipeline with the formatted text
|
286 |
response = CLIENT["pipeline"](
|
287 |
formatted_prompt,
|
288 |
clean_up_tokenization_spaces=False,
|
@@ -290,7 +279,6 @@ def call_pipeline(messages: list, language: str):
|
|
290 |
return_full_text=False,
|
291 |
)
|
292 |
|
293 |
-
# Extract the generated content
|
294 |
return response[0]["generated_text"]
|
295 |
else:
|
296 |
response = CLIENT(
|
@@ -435,7 +423,6 @@ def wrangle_edit_data(
|
|
435 |
)
|
436 |
return history
|
437 |
else:
|
438 |
-
# Add feedback on original and corrected message
|
439 |
add_fake_like_data(
|
440 |
history=history[: index + 1],
|
441 |
conversation_id=conversation_id,
|
@@ -450,7 +437,6 @@ def wrangle_edit_data(
|
|
450 |
language=language,
|
451 |
)
|
452 |
history = history[: index + 1]
|
453 |
-
# add chosen and rejected options
|
454 |
history[-1]["options"] = [
|
455 |
Option(label="chosen", value=x.value),
|
456 |
Option(label="rejected", value=original_message["content"]),
|
@@ -514,27 +500,22 @@ def close_add_language_modal():
|
|
514 |
|
515 |
def save_new_language(lang_name, system_prompt):
|
516 |
"""Save the new language and system prompt to persistent storage if available, otherwise to local file."""
|
517 |
-
global LANGUAGES
|
518 |
|
519 |
-
# Get the appropriate path
|
520 |
languages_path, use_persistent = get_persistent_storage_path("languages.json")
|
521 |
local_path = Path(__file__).parent / "languages.json"
|
522 |
|
523 |
-
# Load existing languages
|
524 |
if languages_path.exists():
|
525 |
with open(languages_path, "r", encoding="utf-8") as f:
|
526 |
data = json.load(f)
|
527 |
else:
|
528 |
data = {}
|
529 |
|
530 |
-
# Add the new language to JSON
|
531 |
data[lang_name] = system_prompt
|
532 |
|
533 |
-
# Save the updated languages
|
534 |
with open(languages_path, "w", encoding="utf-8") as f:
|
535 |
json.dump(data, f, ensure_ascii=False, indent=2)
|
536 |
|
537 |
-
# If we're using persistent storage, also update the local file as backup
|
538 |
if use_persistent and local_path != languages_path:
|
539 |
try:
|
540 |
with open(local_path, "w", encoding="utf-8") as f:
|
@@ -542,10 +523,7 @@ def save_new_language(lang_name, system_prompt):
|
|
542 |
except Exception as e:
|
543 |
print(f"Error updating local backup: {e}")
|
544 |
|
545 |
-
# Update the global LANGUAGES variable with the new data
|
546 |
LANGUAGES.update({lang_name: system_prompt})
|
547 |
-
|
548 |
-
# Return a message that will trigger a JavaScript refresh
|
549 |
return gr.Group(visible=False), gr.HTML("<script>window.location.reload();</script>"), gr.Dropdown(choices=list(LANGUAGES.keys()))
|
550 |
|
551 |
|
@@ -570,20 +548,6 @@ button#add-language-btn {
|
|
570 |
box-shadow: 0 2px 5px rgba(0,0,0,0.1) !important;
|
571 |
}
|
572 |
"""
|
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 |
-
# """
|
587 |
|
588 |
with gr.Blocks(css=css) as demo:
|
589 |
# State variable to track if user has consented
|
@@ -643,12 +607,9 @@ with gr.Blocks(css=css) as demo:
|
|
643 |
with gr.Row():
|
644 |
with gr.Column(scale=1):
|
645 |
save_language_btn = gr.Button("Save")
|
646 |
-
# with gr.Column(scale=0.2):
|
647 |
-
# pass # Empty column as spacer
|
648 |
with gr.Column(scale=1):
|
649 |
cancel_language_btn = gr.Button("Cancel")
|
650 |
|
651 |
-
# Add a hidden HTML component for page refresh
|
652 |
refresh_html = gr.HTML(visible=False)
|
653 |
|
654 |
session_id = gr.Textbox(
|
@@ -756,13 +717,9 @@ with gr.Blocks(css=css) as demo:
|
|
756 |
|
757 |
def on_app_load():
|
758 |
global LANGUAGES
|
759 |
-
# Force reload languages from file
|
760 |
LANGUAGES = load_languages()
|
761 |
-
|
762 |
-
# Get the list of languages
|
763 |
language_choices = list(LANGUAGES.keys())
|
764 |
|
765 |
-
# Return both the session ID and available language choices
|
766 |
return str(uuid.uuid4()), gr.Dropdown(choices=language_choices, value=language_choices[0])
|
767 |
|
768 |
demo.load(
|
|
|
101 |
languages_path, use_persistent = get_persistent_storage_path("languages.json")
|
102 |
local_path = Path(__file__).parent / "languages.json"
|
103 |
|
104 |
+
# If persistent storage is available but file doesn't exist yet, copy the local file to persistent storage
|
|
|
105 |
if use_persistent and not languages_path.exists():
|
106 |
try:
|
107 |
if local_path.exists():
|
108 |
import shutil
|
|
|
109 |
shutil.copy(local_path, languages_path)
|
110 |
print(f"Copied languages to persistent storage at {languages_path}")
|
111 |
else:
|
|
|
112 |
with open(languages_path, "w", encoding="utf-8") as f:
|
113 |
json.dump({"English": "You are a helpful assistant."}, f, ensure_ascii=False, indent=2)
|
114 |
print(f"Created new languages file in persistent storage at {languages_path}")
|
|
|
116 |
print(f"Error setting up persistent storage: {e}")
|
117 |
languages_path = local_path # Fall back to local path if any error occurs
|
118 |
|
|
|
119 |
if not languages_path.exists() and local_path.exists():
|
120 |
languages_path = local_path
|
121 |
|
|
|
122 |
if languages_path.exists():
|
123 |
with open(languages_path, "r", encoding="utf-8") as f:
|
124 |
return json.load(f)
|
125 |
else:
|
|
|
126 |
default_languages = {"English": "You are a helpful assistant."}
|
127 |
return default_languages
|
128 |
|
|
|
|
|
129 |
LANGUAGES = load_languages()
|
130 |
|
|
|
131 |
USER_AGREEMENT = """
|
132 |
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.
|
133 |
|
|
|
266 |
def call_pipeline(messages: list, language: str):
|
267 |
"""Call the appropriate model pipeline based on configuration"""
|
268 |
if ZERO_GPU:
|
|
|
269 |
tokenizer = CLIENT["tokenizer"]
|
270 |
formatted_prompt = tokenizer.apply_chat_template(
|
271 |
messages,
|
272 |
tokenize=False,
|
273 |
)
|
274 |
|
|
|
275 |
response = CLIENT["pipeline"](
|
276 |
formatted_prompt,
|
277 |
clean_up_tokenization_spaces=False,
|
|
|
279 |
return_full_text=False,
|
280 |
)
|
281 |
|
|
|
282 |
return response[0]["generated_text"]
|
283 |
else:
|
284 |
response = CLIENT(
|
|
|
423 |
)
|
424 |
return history
|
425 |
else:
|
|
|
426 |
add_fake_like_data(
|
427 |
history=history[: index + 1],
|
428 |
conversation_id=conversation_id,
|
|
|
437 |
language=language,
|
438 |
)
|
439 |
history = history[: index + 1]
|
|
|
440 |
history[-1]["options"] = [
|
441 |
Option(label="chosen", value=x.value),
|
442 |
Option(label="rejected", value=original_message["content"]),
|
|
|
500 |
|
501 |
def save_new_language(lang_name, system_prompt):
|
502 |
"""Save the new language and system prompt to persistent storage if available, otherwise to local file."""
|
503 |
+
global LANGUAGES
|
504 |
|
|
|
505 |
languages_path, use_persistent = get_persistent_storage_path("languages.json")
|
506 |
local_path = Path(__file__).parent / "languages.json"
|
507 |
|
|
|
508 |
if languages_path.exists():
|
509 |
with open(languages_path, "r", encoding="utf-8") as f:
|
510 |
data = json.load(f)
|
511 |
else:
|
512 |
data = {}
|
513 |
|
|
|
514 |
data[lang_name] = system_prompt
|
515 |
|
|
|
516 |
with open(languages_path, "w", encoding="utf-8") as f:
|
517 |
json.dump(data, f, ensure_ascii=False, indent=2)
|
518 |
|
|
|
519 |
if use_persistent and local_path != languages_path:
|
520 |
try:
|
521 |
with open(local_path, "w", encoding="utf-8") as f:
|
|
|
523 |
except Exception as e:
|
524 |
print(f"Error updating local backup: {e}")
|
525 |
|
|
|
526 |
LANGUAGES.update({lang_name: system_prompt})
|
|
|
|
|
527 |
return gr.Group(visible=False), gr.HTML("<script>window.location.reload();</script>"), gr.Dropdown(choices=list(LANGUAGES.keys()))
|
528 |
|
529 |
|
|
|
548 |
box-shadow: 0 2px 5px rgba(0,0,0,0.1) !important;
|
549 |
}
|
550 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
551 |
|
552 |
with gr.Blocks(css=css) as demo:
|
553 |
# State variable to track if user has consented
|
|
|
607 |
with gr.Row():
|
608 |
with gr.Column(scale=1):
|
609 |
save_language_btn = gr.Button("Save")
|
|
|
|
|
610 |
with gr.Column(scale=1):
|
611 |
cancel_language_btn = gr.Button("Cancel")
|
612 |
|
|
|
613 |
refresh_html = gr.HTML(visible=False)
|
614 |
|
615 |
session_id = gr.Textbox(
|
|
|
717 |
|
718 |
def on_app_load():
|
719 |
global LANGUAGES
|
|
|
720 |
LANGUAGES = load_languages()
|
|
|
|
|
721 |
language_choices = list(LANGUAGES.keys())
|
722 |
|
|
|
723 |
return str(uuid.uuid4()), gr.Dropdown(choices=language_choices, value=language_choices[0])
|
724 |
|
725 |
demo.load(
|