support all the languages!

modified:   app.py
	modified:   research/activity0.yaml
This commit is contained in:
Russell Ballestrini 2024-08-09 08:20:48 -04:00
parent 3b542285f6
commit 17777d727d
2 changed files with 141 additions and 11 deletions

71
app.py
View file

@ -1766,6 +1766,9 @@ def loop_through_steps_until_question(
current_section_id = activity_state.section_id
current_step_id = activity_state.step_id
# Get the user's language preference from metadata
user_language = activity_state.dict_metadata.get("language", "English")
while True:
section = next(
(
@ -1786,7 +1789,10 @@ def loop_through_steps_until_question(
# Emit the current step content blocks
content = "\n\n".join(step["content_blocks"])
new_message = Message(username="System", content=content, room_id=room.id)
translated_content = translate_text(content, user_language)
new_message = Message(
username="System", content=translated_content, room_id=room.id
)
db.session.add(new_message)
db.session.commit()
@ -1795,7 +1801,7 @@ def loop_through_steps_until_question(
{
"id": new_message.id,
"username": "System",
"content": content,
"content": translated_content,
},
room=room_name,
)
@ -1803,8 +1809,11 @@ def loop_through_steps_until_question(
# Check if the current step has a question
if "question" in step:
question_content = f"Question: {step['question']}"
translated_question_content = translate_text(
question_content, user_language
)
new_message = Message(
username="System", content=question_content, room_id=room.id
username="System", content=translated_question_content, room_id=room.id
)
db.session.add(new_message)
db.session.commit()
@ -1814,7 +1823,7 @@ def loop_through_steps_until_question(
{
"id": new_message.id,
"username": "System",
"content": question_content,
"content": translated_question_content,
},
room=room_name,
)
@ -2082,6 +2091,8 @@ def handle_activity_response(room_name, user_response, username):
db.session.add(activity_state)
db.session.commit()
user_language = activity_state.dict_metadata.get("language", "English")
# Provide feedback based on the category
feedback = provide_feedback(
transition,
@ -2089,6 +2100,7 @@ def handle_activity_response(room_name, user_response, username):
step["question"],
step["tokens_for_ai"],
user_response,
user_language,
username,
activity_state.json_metadata,
json.dumps(new_metadata),
@ -2096,8 +2108,9 @@ def handle_activity_response(room_name, user_response, username):
# Store and emit the feedback
if feedback:
translated_feedback = translate_text(feedback, user_language)
new_message = Message(
username="System", content=feedback, room_id=room.id
username="System", content=translated_feedback, room_id=room.id
)
db.session.add(new_message)
db.session.commit()
@ -2115,8 +2128,13 @@ def handle_activity_response(room_name, user_response, username):
# Emit the transition content blocks if they exist
if "content_blocks" in transition:
transition_content = "\n\n".join(transition["content_blocks"])
translated_transition_content = translate_text(
transition_content, user_language
)
new_message = Message(
username="System", content=transition_content, room_id=room.id
username="System",
content=translated_transition_content,
room_id=room.id,
)
db.session.add(new_message)
db.session.commit()
@ -2126,7 +2144,7 @@ def handle_activity_response(room_name, user_response, username):
{
"id": new_message.id,
"username": "System",
"content": transition_content,
"content": translated_transition_content,
},
room=room_name,
)
@ -2183,8 +2201,13 @@ def handle_activity_response(room_name, user_response, username):
# Emit the question again
question_content = f"Question: {step['question']}"
translated_question_content = translate_text(
question_content, user_language
)
new_message = Message(
username="System", content=question_content, room_id=room.id
username="System",
content=translated_question_content,
room_id=room.id,
)
db.session.add(new_message)
db.session.commit()
@ -2194,7 +2217,7 @@ def handle_activity_response(room_name, user_response, username):
{
"id": new_message.id,
"username": "System",
"content": question_content,
"content": translated_question_content,
},
room=room_name,
)
@ -2416,6 +2439,7 @@ def provide_feedback(
category,
question,
user_response,
user_language,
tokens_for_ai,
username,
json_metadata,
@ -2423,7 +2447,7 @@ def provide_feedback(
):
feedback = ""
if "ai_feedback" in transition:
tokens_for_ai += " " + transition["ai_feedback"]["tokens_for_ai"]
tokens_for_ai += f" Provide the feedback in {user_language}. {transition.get('ai_feedback',{}).get('tokens_for_ai', '')}."
ai_feedback = generate_ai_feedback(
category,
question,
@ -2438,6 +2462,33 @@ def provide_feedback(
return feedback
def translate_text(text, target_language):
# Guard clause for default language
if target_language.lower() == "english":
return text
openai_client, model_name = get_openai_client_and_model()
messages = [
{
"role": "system",
"content": f"Translate the following text to {target_language}:",
},
{
"role": "user",
"content": text,
},
]
try:
completion = openai_client.chat.completions.create(
model=model_name, messages=messages, max_tokens=2000, temperature=0.7
)
translation = completion.choices[0].message.content.strip()
return translation
except Exception as e:
return f"Error: {e}"
if __name__ == "__main__":
import argparse

View file

@ -8,6 +8,7 @@ sections:
content_blocks:
- "Welcome to the GNU Manifesto course! 👋"
- "You will learn about the GNU Manifesto and its significance."
- section_id: "section_1"
title: "Introduction to The GNU Manifesto"
steps:
@ -25,6 +26,7 @@ sections:
- partial_understanding
- off_topic
- asking_clarifying_questions
- set_language
transitions:
correct:
content_blocks:
@ -46,6 +48,15 @@ sections:
- "I see you have some questions. Let's address them. ❓"
ai_feedback:
tokens_for_ai: "Answer the student's clarifying questions and provide additional information in a friendly and engaging manner. Use emojis like ❓ and 💬."
counts_as_attempt: false
set_language:
content_blocks:
- "Language preference updated. Please continue in your preferred language."
ai_feedback:
tokens_for_ai: "Acknowledge the language change and confirm the update."
metadata_add:
language: "the-users-response"
counts_as_attempt: false
- step_id: "step_2"
title: "Importance of The GNU Manifesto"
@ -60,6 +71,7 @@ sections:
- partial_understanding
- off_topic
- asking_clarifying_questions
- set_language
transitions:
correct:
content_blocks:
@ -81,6 +93,15 @@ sections:
- "I see you have some questions. Let's address them. ❓"
ai_feedback:
tokens_for_ai: "Answer the student's clarifying questions and provide additional information in a friendly and engaging manner. Use emojis like ❓ and 💬."
counts_as_attempt: false
set_language:
content_blocks:
- "Language preference updated. Please continue in your preferred language."
ai_feedback:
tokens_for_ai: "Acknowledge the language change and confirm the update."
metadata_add:
language: "the-users-response"
counts_as_attempt: false
- section_id: "section_2"
title: "Key Concepts of The GNU Manifesto"
@ -98,6 +119,7 @@ sections:
- partial_understanding
- off_topic
- asking_clarifying_questions
- set_language
transitions:
correct:
content_blocks:
@ -119,6 +141,15 @@ sections:
- "I see you have some questions. Let's address them. ❓"
ai_feedback:
tokens_for_ai: "Answer the student's clarifying questions and provide additional information in a friendly and engaging manner. Use emojis like ❓ and 💬."
counts_as_attempt: false
set_language:
content_blocks:
- "Language preference updated. Please continue in your preferred language."
ai_feedback:
tokens_for_ai: "Acknowledge the language change and confirm the update."
metadata_add:
language: "the-users-response"
counts_as_attempt: false
- step_id: "step_2"
title: "Why GNU Will Be Free"
@ -133,6 +164,7 @@ sections:
- partial_understanding
- off_topic
- asking_clarifying_questions
- set_language
transitions:
correct:
content_blocks:
@ -154,6 +186,15 @@ sections:
- "I see you have some questions. Let's address them. ❓"
ai_feedback:
tokens_for_ai: "Answer the student's clarifying questions and provide additional information in a friendly and engaging manner. Use emojis like ❓ and 💬."
counts_as_attempt: false
set_language:
content_blocks:
- "Language preference updated. Please continue in your preferred language."
ai_feedback:
tokens_for_ai: "Acknowledge the language change and confirm the update."
metadata_add:
language: "the-users-response"
counts_as_attempt: false
- section_id: "section_3"
title: "Contributing to GNU"
@ -170,6 +211,7 @@ sections:
- partial_understanding
- off_topic
- asking_clarifying_questions
- set_language
transitions:
correct:
content_blocks:
@ -191,6 +233,15 @@ sections:
- "I see you have some questions. Let's address them. ❓"
ai_feedback:
tokens_for_ai: "Answer the student's clarifying questions and provide additional information in a friendly and engaging manner. Use emojis like ❓ and 💬."
counts_as_attempt: false
set_language:
content_blocks:
- "Language preference updated. Please continue in your preferred language."
ai_feedback:
tokens_for_ai: "Acknowledge the language change and confirm the update."
metadata_add:
language: "the-users-response"
counts_as_attempt: false
- step_id: "step_2"
title: "Ways to Contribute"
@ -204,6 +255,7 @@ sections:
- partial_understanding
- off_topic
- asking_clarifying_questions
- set_language
transitions:
correct:
content_blocks:
@ -225,6 +277,15 @@ sections:
- "I see you have some questions. Let's address them. ❓"
ai_feedback:
tokens_for_ai: "Answer the student's clarifying questions and provide additional information in a friendly and engaging manner. Use emojis like ❓ and 💬."
counts_as_attempt: false
set_language:
content_blocks:
- "Language preference updated. Please continue in your preferred language."
ai_feedback:
tokens_for_ai: "Acknowledge the language change and confirm the update."
metadata_add:
language: "the-users-response"
counts_as_attempt: false
- section_id: "section_4"
title: "Legacy of The GNU Manifesto"
@ -241,6 +302,7 @@ sections:
- partial_understanding
- off_topic
- asking_clarifying_questions
- set_language
transitions:
correct:
content_blocks:
@ -262,6 +324,15 @@ sections:
- "I see you have some questions. Let's address them. ❓"
ai_feedback:
tokens_for_ai: "Answer the student's clarifying questions and provide additional information in a friendly and engaging manner. Use emojis like ❓ and 💬."
counts_as_attempt: false
set_language:
content_blocks:
- "Language preference updated. Please continue in your preferred language."
ai_feedback:
tokens_for_ai: "Acknowledge the language change and confirm the update."
metadata_add:
language: "the-users-response"
counts_as_attempt: false
- step_id: "step_2"
title: "Future of Free Software"
@ -275,6 +346,7 @@ sections:
- partial_understanding
- off_topic
- asking_clarifying_questions
- set_language
transitions:
correct:
content_blocks:
@ -296,6 +368,14 @@ sections:
- "I see you have some questions. Let's address them. ❓"
ai_feedback:
tokens_for_ai: "Answer the student's clarifying questions and provide additional information in a friendly and engaging manner. Use emojis like ❓ and 💬."
set_language:
content_blocks:
- "Language preference updated. Please continue in your preferred language."
ai_feedback:
tokens_for_ai: "Acknowledge the language change and confirm the update."
metadata_add:
language: "the-users-response"
counts_as_attempt: false
- section_id: "section_5"
title: "Congratulations!"
@ -307,4 +387,3 @@ sections:
- "You have learned about the key concepts, principles, and impact of the GNU Manifesto."
- "This knowledge will help you understand the importance of software freedom and the Free Software Movement."
- "We are proud of your dedication and hard work. Well done! 🌟"