Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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')
|
29 |
+
st.header(':one: Tutor: Ask questions for programming your own Roblox game!')
|
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 |
+
query = st.text_input(
|
37 |
+
label='Write your question.',
|
38 |
+
placeholder='ex. What properties should I adjust to change the transparency of a Roblox Part?'
|
39 |
+
)
|
40 |
+
|
41 |
+
st.write(f'Query: :violet[{query}]')
|
42 |
+
|
43 |
+
query_1 = 'AT_TUTOR_1: ' + query
|
44 |
+
|
45 |
+
# ์ด๋ฏธ์ง ์
๋ ฅ
|
46 |
+
image_uploaded1 = st.file_uploader("OPTIONAL: Upload an image", type=["png", "jpg", "jpeg"])
|
47 |
+
|
48 |
+
def tutor_persona_query(query, image_data=None):
|
49 |
+
import openai
|
50 |
+
openai.api_key = OPENAI_API_KEY
|
51 |
+
|
52 |
+
VISION_PROMPT_MESSAGES = [
|
53 |
+
{
|
54 |
+
"role": "system",
|
55 |
+
"content": "Your role: A tutor helping a student who is learning how to build a game using Roblox for the first time. Situation: Your student is using a plugin called blocklua to program using block-based code. Your student asks you a question about the Lua programming language and how to use Roblox Studio to create a Roblox game, sometimes including images. What to do: Answer the questions of the student about your programming knowledge and how to use Roblox Studio, referring to any images provided. Guidelines: If you do not know the exact answer, you can guide them to a general algorithm for programming, or what keywords to search on YouTube for using Roblox Studio. Be kind to your students and provide them with praise to keep them motivated to learn."
|
56 |
+
},
|
57 |
+
{"role": "user", "content": query},
|
58 |
+
]
|
59 |
+
|
60 |
+
if image_data is not None:
|
61 |
+
encoded_image = base64.b64encode(image_data).decode("utf-8")
|
62 |
+
VISION_PROMPT_MESSAGES.append({"role": "user", "content": encoded_image})
|
63 |
+
|
64 |
+
|
65 |
+
params = {
|
66 |
+
"model": "gpt-4-turbo",
|
67 |
+
"messages": VISION_PROMPT_MESSAGES,
|
68 |
+
"max_tokens": 1024,
|
69 |
+
}
|
70 |
+
|
71 |
+
trimmed_answer = "" # trimmed_answer ์ด๊ธฐํ
|
72 |
+
try:
|
73 |
+
full_answer = openai.chat.completions.create(**params)
|
74 |
+
trimmed_answer = full_answer.choices[0].message.content
|
75 |
+
except Exception as e:
|
76 |
+
print(f"Error: {e}")
|
77 |
+
trimmed_answer = f"Error: {e}" # ์ค๋ฅ ๋ฉ์์ง๋ฅผ trimmed_answer์ ํ ๋น
|
78 |
+
|
79 |
+
return trimmed_answer
|
80 |
+
|
81 |
+
# ๋ฒํผ ํด๋ฆญ
|
82 |
+
button1 = st.button(':sparkles: ask :sparkles:')
|
83 |
+
|
84 |
+
|
85 |
+
def save_message(user_id, message, timestamp=None):
|
86 |
+
if timestamp is None:
|
87 |
+
timestamp = datetime.datetime.now()
|
88 |
+
|
89 |
+
# datetime ๊ฐ์ฒด๋ฅผ DynamoDB ์ง์ ํ์
์ผ๋ก ๋ณํ
|
90 |
+
if isinstance(timestamp, datetime.datetime):
|
91 |
+
timestamp = int(timestamp.timestamp()) # Unix ํ์์คํฌํ(์ด ๋จ์ ์ ์)๋ก ๋ณํ
|
92 |
+
|
93 |
+
table.put_item(
|
94 |
+
Item={
|
95 |
+
'UserID': user_id,
|
96 |
+
'Timestamp': timestamp,
|
97 |
+
'Message': message
|
98 |
+
}
|
99 |
+
)
|
100 |
+
|
101 |
+
|
102 |
+
if button1:
|
103 |
+
query_1 = query_1
|
104 |
+
save_message(user_id, query_1)
|
105 |
+
|
106 |
+
if image_uploaded1 is not None:
|
107 |
+
image_data = image_uploaded1.read()
|
108 |
+
answer = tutor_persona_query(query, image_data)
|
109 |
+
else:
|
110 |
+
answer = tutor_persona_query(query)
|
111 |
+
|
112 |
+
answer_1 = 'AT_TUTOR_1: ' + answer
|
113 |
+
save_message(user_id, answer_1)
|
114 |
+
st.write(f'{answer}')
|
115 |
+
|
116 |
+
|
117 |
+
else:
|
118 |
+
st.warning("Invalid username. Please enter a valid username.")
|
119 |
+
|