Spaces:
Sleeping
Sleeping
File size: 4,230 Bytes
da6d052 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
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)
def is_valid_username(username):
return username in allowed_usernames
st.title(':male-teacher: AI ROBLOX Tutor')
st.header(':one: Tutor: Ask questions for programming your own Roblox game!')
user_id = st.text_input(label='Assigned ID.')
if is_valid_username(user_id):
st.write(f'ID: :violet[{user_id}]')
query = st.text_input(
label='Write your question.',
placeholder='ex. What properties should I adjust to change the transparency of a Roblox Part?'
)
st.write(f'Query: :violet[{query}]')
query_1 = 'AT_TUTOR_1: ' + query
# ์ด๋ฏธ์ง ์
๋ ฅ
image_uploaded1 = st.file_uploader("OPTIONAL: Upload an image", type=["png", "jpg", "jpeg"])
def tutor_persona_query(query, image_data=None):
import openai
openai.api_key = OPENAI_API_KEY
VISION_PROMPT_MESSAGES = [
{
"role": "system",
"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."
},
{"role": "user", "content": query},
]
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-4-turbo",
"messages": VISION_PROMPT_MESSAGES,
"max_tokens": 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
# ๋ฒํผ ํด๋ฆญ
button1 = st.button(':sparkles: ask :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 button1:
query_1 = query_1
save_message(user_id, query_1)
if image_uploaded1 is not None:
image_data = image_uploaded1.read()
answer = tutor_persona_query(query, image_data)
else:
answer = tutor_persona_query(query)
answer_1 = 'AT_TUTOR_1: ' + answer
save_message(user_id, answer_1)
st.write(f'{answer}')
else:
st.warning("Invalid username. Please enter a valid username.")
|