AITUTOR2 / app.py
lugiiing's picture
Update app.py
c322065 verified
import os
import openai
import streamlit as st
from dotenv import load_dotenv
import boto3
import base64
import datetime
import json
load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
client = openai.OpenAI(api_key=OPENAI_API_KEY)
dynamodb = boto3.resource(
'dynamodb',
aws_access_key_id=os.getenv('AWS_ACCESS_KEY_ID'),
aws_secret_access_key=os.getenv('AWS_SECRET_ACCESS_KEY'),
region_name='ap-northeast-2' # ์›ํ•˜๋Š” ๋ฆฌ์ „์œผ๋กœ ๋ณ€๊ฒฝ
)
table = dynamodb.Table('ChatbotConversations')
#original_allowed_usernames = os.getenv('ALLOWED_NAMES')
#allowed_usernames = json.loads(original_allowed_usernames)
original_allowed_usernames = os.getenv('ALLOWED_NAMES')
original_allowed_usernames = original_allowed_usernames.strip()
# JSON ํŒŒ์‹ฑ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
allowed_usernames = json.loads(original_allowed_usernames)
def is_valid_username(username):
return username in allowed_usernames
st.title(':male-teacher: AI ROBLOX Tutor - Feedback')
st.header(':two: Tutor for Feedack: Get feedbacks and suggestions on your own Roblox game codes!')
user_id = st.text_input(label='Assigned ID.')
if is_valid_username(user_id):
st.write(f'ID: :violet[{user_id}]')
purpose = st.text_input(
label='Write the purpose of the code that you want to get feedback on.',
placeholder='purpose of your code'
)
st.write(f'Purpose: :violet[{purpose}]')
eval = st.text_input(
label='Write the code that you want to get feedback on.',
placeholder='copy and paste syntax-based code script'
)
st.write(f'Codes: :violet[{eval}]')
query2 = "purpose: " + purpose + ". texted codes: " + eval
query_2 = 'AT_TUTOR_2: ' + query2
# ์ด๋ฏธ์ง€ ์ž…๋ ฅ
image_uploaded2 = st.file_uploader("OPTIONAL: Upload an image of the code that you want to get feedback on", type=["png", "jpg", "jpeg"])
def evaluator_persona_query(eval, purpose, image_data=None):
import openai
openai.api_key = OPENAI_API_KEY
VISION_PROMPT_MESSAGES = [
{
"role": "system",
"content": "Your role: You are an AI tutor providing corrective and explanatory feedback on students' Roblox game scripts. Your goal is to help learners understand computational thinking concepts through meaningful, targeted feedback. Context: - The learner is coding in Lua within the Roblox game development environment. - The learner is submitting a code snippet along with a brief description of its intended function. - The tutor provides structured feedback based on **computational thinking (CT) concepts** such as: - **Algorithm & Procedures:** Does the code follow structured logic? - **Automation & Efficiency:** Are there redundant lines or unnecessary loops? - **Debugging & Error Handling:** Does the code have syntax/logical errors? - **Modularity & Reusability:** Are functions used effectively? User Input: - A code snippet written in Lua for a game feature. - A description of what the learner **intends** the code to do. Expected AI Response: 1. **Confirm understanding** by summarizing what the code is supposed to do. 2. **Identify and correct errors** (if any) with explanations: - Highlight incorrect syntax or logic errors. - Suggest alternative, optimized code structures. 3. **Provide explanations** for computational thinking concepts that apply to this code. 4. **Suggest improvements** based on best practices in coding and game design. 5. **Encourage reflection** by asking the learner to predict how changes will affect gameplay."
##CSTA & ISTE (2011). Computational Thinking in K-12 education โ€“ teacher resources, 2nd edition, CSTA & ISTE
},
{"role": "user", "content": "Read and find the meaning of the code in the image and text and evaluate the code. purpose of the code: "+ purpose + "code to be analysed: " + eval},
]
if image_data is not None:
encoded_image = base64.b64encode(image_data).decode("utf-8")
VISION_PROMPT_MESSAGES.append({"role": "user", "content": encoded_image})
params = {
"model": "gpt-4o-mini",
"messages": VISION_PROMPT_MESSAGES,
"max_tokens": 3000, # 1024,
}
trimmed_answer = "" # trimmed_answer ์ดˆ๊ธฐํ™”
try:
full_answer = openai.chat.completions.create(**params)
trimmed_answer = full_answer.choices[0].message.content
except Exception as e:
print(f"Error: {e}")
trimmed_answer = f"Error: {e}" # ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ trimmed_answer์— ํ• ๋‹น
return trimmed_answer
# ๋ฒ„ํŠผ ํด๋ฆญ
button2 = st.button(':sparkles: suggestions :sparkles:')
def save_message(user_id, message, timestamp=None):
if timestamp is None:
timestamp = datetime.datetime.now()
# datetime ๊ฐ์ฒด๋ฅผ DynamoDB ์ง€์› ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜
if isinstance(timestamp, datetime.datetime):
timestamp = int(timestamp.timestamp()) # Unix ํƒ€์ž„์Šคํƒฌํ”„(์ดˆ ๋‹จ์œ„ ์ •์ˆ˜)๋กœ ๋ณ€ํ™˜
table.put_item(
Item={
'UserID': user_id,
'Timestamp': timestamp,
'Message': message
}
)
if button2:
eval = eval
save_message(user_id, query_2)
if image_uploaded2 is not None:
image_data = image_uploaded2.read()
answer = evaluator_persona_query(eval, purpose, image_data)
else:
answer = evaluator_persona_query(eval, purpose)
answer_2 = 'AT_TUTOR_2: ' + answer
save_message(user_id, answer_2)
st.write(f'{answer}')
else:
st.warning("Invalid username. Please enter a valid username.")