lugiiing commited on
Commit
21a4c2b
ยท
verified ยท
1 Parent(s): 8a0e469

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -0
app.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import openai
3
+ import streamlit as st
4
+ from dotenv import load_dotenv
5
+ import boto3
6
+ import base64
7
+ import datetime
8
+ import json
9
+
10
+ load_dotenv()
11
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
12
+ client = openai.OpenAI(api_key=OPENAI_API_KEY)
13
+
14
+ dynamodb = boto3.resource(
15
+ 'dynamodb',
16
+ aws_access_key_id=os.getenv('AWS_ACCESS_KEY_ID'),
17
+ aws_secret_access_key=os.getenv('AWS_SECRET_ACCESS_KEY'),
18
+ region_name='ap-northeast-2' # ์›ํ•˜๋Š” ๋ฆฌ์ „์œผ๋กœ ๋ณ€๊ฒฝ
19
+ )
20
+ table = dynamodb.Table('ChatbotConversations')
21
+
22
+ original_allowed_usernames = os.getenv('ALLOWED_NAMES')
23
+ allowed_usernames = json.loads(original_allowed_usernames)
24
+
25
+ def is_valid_username(username):
26
+ return username in allowed_usernames
27
+
28
+ st.title(':male-teacher: AI ROBLOX Tutor - Feedback')
29
+ st.header(':two: Tutor for Feedack: Get feedbacks and suggestions on your own Roblox game codes!')
30
+
31
+ user_id = st.text_input(label='Assigned ID.')
32
+
33
+
34
+ if is_valid_username(user_id):
35
+ st.write(f'ID: :violet[{user_id}]')
36
+
37
+ purpose = st.text_input(
38
+ label='Write the purpose of the code that you want to get feedback on.',
39
+ placeholder='purpose of your code'
40
+ )
41
+
42
+ st.write(f'Purpose: :violet[{purpose}]')
43
+
44
+ eval = st.text_input(
45
+ label='Write the code that you want to get feedback on.',
46
+ placeholder='copy and paste syntax-based code script'
47
+ )
48
+
49
+ st.write(f'Codes: :violet[{eval}]')
50
+
51
+ query2 = "purpose: " + purpose + ". texted codes: " + eval
52
+
53
+ query_2 = 'AT_TUTOR_2: ' + query2
54
+
55
+ # ์ด๋ฏธ์ง€ ์ž…๋ ฅ
56
+ image_uploaded2 = st.file_uploader("OPTIONAL: Upload an image of the code that you want to get feedback on", type=["png", "jpg", "jpeg"])
57
+
58
+ def evaluator_persona_query(eval, purpose, image_data=None):
59
+ import openai
60
+ openai.api_key = OPENAI_API_KEY
61
+
62
+ VISION_PROMPT_MESSAGES = [
63
+ {
64
+ "role": "system",
65
+ "content": "You are a tutor who gives feedback about the Roblox code made with a Roblox plugin called Blocklua. Situation: A student is using a plugin called Blocklua to program using block-based code. The student ask for corrective and explanatory feedback of their own code of the Roblox games of their own, sometimes including images. If they ask you to give feedback and suggestion about the code they uploaded or the code in the image, give them the corrective or explanatory feedback and suggestion based on the aspect of Algorithm & procedures, Automatiologic, perfection. "
66
+ ##CSTA & ISTE (2011). Computational Thinking in K-12 education โ€“ teacher resources, 2nd edition, CSTA & ISTE
67
+ },
68
+ {"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},
69
+ ]
70
+
71
+ if image_data is not None:
72
+ encoded_image = base64.b64encode(image_data).decode("utf-8")
73
+ VISION_PROMPT_MESSAGES.append({"role": "user", "content": encoded_image})
74
+
75
+ params = {
76
+ "model": "gpt-4-turbo",
77
+ "messages": VISION_PROMPT_MESSAGES,
78
+ "max_tokens": 1024,
79
+ }
80
+
81
+ trimmed_answer = "" # trimmed_answer ์ดˆ๊ธฐํ™”
82
+ try:
83
+ full_answer = openai.chat.completions.create(**params)
84
+ trimmed_answer = full_answer.choices[0].message.content
85
+ except Exception as e:
86
+ print(f"Error: {e}")
87
+ trimmed_answer = f"Error: {e}" # ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ trimmed_answer์— ํ• ๋‹น
88
+
89
+ return trimmed_answer
90
+
91
+ # ๋ฒ„ํŠผ ํด๋ฆญ
92
+ button2 = st.button(':sparkles: suggestions :sparkles:')
93
+
94
+ def save_message(user_id, message, timestamp=None):
95
+ if timestamp is None:
96
+ timestamp = datetime.datetime.now()
97
+
98
+ # datetime ๊ฐ์ฒด๋ฅผ DynamoDB ์ง€์› ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜
99
+ if isinstance(timestamp, datetime.datetime):
100
+ timestamp = int(timestamp.timestamp()) # Unix ํƒ€์ž„์Šคํƒฌํ”„(์ดˆ ๋‹จ์œ„ ์ •์ˆ˜)๋กœ ๋ณ€ํ™˜
101
+
102
+ table.put_item(
103
+ Item={
104
+ 'UserID': user_id,
105
+ 'Timestamp': timestamp,
106
+ 'Message': message
107
+ }
108
+ )
109
+
110
+
111
+ if button2:
112
+ eval = eval
113
+ save_message(user_id, query_2)
114
+
115
+ if image_uploaded2 is not None:
116
+ image_data = image_uploaded2.read()
117
+ answer = evaluator_persona_query(eval, purpose, image_data)
118
+ else:
119
+ answer = evaluator_persona_query(eval, purpose)
120
+
121
+ answer_2 = 'AT_TUTOR_2: ' + answer
122
+ save_message(user_id, answer_2)
123
+ st.write(f'{answer}')
124
+
125
+ else:
126
+ st.warning("Invalid username. Please enter a valid username.")
127
+