Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
import openai | |
import os | |
import instructions | |
# OpenAI API ์ค์ | |
openai.api_key = os.getenv("OPENAI_API_KEY") | |
st.set_page_config(layout="wide") | |
# ํ์ผ ์ ๋ก๋ ๋ฐ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ํจ์ | |
def upload_and_process_file(): | |
uploaded_file = st.file_uploader("์ด์ ์ํ๊ธฐ๋ก๋ถ ํ์ผ ์ ๋ก๋", type=['csv', 'json']) | |
if uploaded_file is not None: | |
if uploaded_file.type == 'text/csv': | |
data = pd.read_csv(uploaded_file) | |
elif uploaded_file.type == 'application/json': | |
data = pd.read_json(uploaded_file) | |
return data | |
return None | |
# ์ํ๊ธฐ๋ก๋ถ ๋ฌธ๊ตฌ ์์ฑ ํจ์ | |
def generate_living_record(data): | |
data_str = str(data) | |
response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo-16k", | |
messages=[ | |
{"role": "system", | |
"content": instructions.instruction | |
}, | |
{"role": "user", | |
"content": data_str | |
} | |
], | |
temperature=0.7, | |
max_tokens=10000, | |
top_p=1, | |
frequency_penalty=0, | |
presence_penalty=0 | |
) | |
return response.choices[0].message.content | |
# Streamlit ์ฑ ๊ตฌ์ฑ | |
st.title("์ํ๊ธฐ๋ก๋ถ ์์ฑ ์๋น์ค") | |
st.write("์ด์ ์ํ๊ธฐ๋ก๋ถ ํ์ผ์ ์ ๋ก๋ํ๊ฑฐ๋, ์ง์ ์ํ๊ธฐ๋ก๋ถ ๋ด์ฉ์ ์ ๋ ฅํ์ธ์.") | |
st.write("GPT๋ ์ค์ ํ ์ ์์ต๋๋ค. ์ํ๋ ๊ฒฐ๊ณผ๊ฐ ๋์ค์ง ์๋๋ค๋ฉด ์์ฑ ๋ฒํผ์ ๋ค์ ๋๋ฌ๋ณด์ธ์.") | |
# ํ์ผ ์ ๋ก๋ ๊ธฐ๋ฅ | |
uploaded_data = upload_and_process_file() | |
#์์ ํค์๋ | |
attitude_keywords = [ | |
"์ง์ค๋ ฅ", "๊ณผ์ ์ดํด", "๋ฐํ", "๋ฌธ์ ํด๊ฒฐ๋ ฅ", "๊ณผ์ ์ํ", "๊ด์ฐฐ๋ ฅ", | |
"ํ๊ตฌ๋ ฅ", "์๊ธฐ ์๊ฐ ํํ", "๋ ผ๋ฆฌ์ ์ฌ๊ณ ๋ ฅ", "์ด๋์ ๊ฒฝ", "ํ๋์ฌ", | |
"๋ฆฌ๋์ฝ", "๋ ผ๋ฆฌ์ ๋นํ๋ ฅ", "๊ต๊ณผ ํ์ต ์ฑ์ทจ๋", "๋ชฉํ์์", "๊ธฐ๋ณธ ํ์ต", | |
"๋์ ๊ณผ์ ", "๋ฏธ์ ๊ฐ๊ฐ", "์์ ์ ์ฌ๋ฅ", "์์ ์ ์ฌ๋ฅ", "ํธ๊ธฐ์ฌ", | |
"์ง๋ฌธ", "๋ชจ๋ ํ๋" | |
] | |
relationship_keywords = [ | |
"ํ๋์ฑ", "์น์ ํจ", "๊ณต๊ฐ ๋ฅ๋ ฅ", "์ปค๋ฎค๋์ผ์ด์ ", "๋ฆฌ๋์ญ", "๊ฒฝ์", "ํ๋ ฅ", | |
"์ธ์ ์๊ตฌ", "์์ฌ์ํต ๋ฌธ์ ", "์ ๋ขฐ", "์ํธ์กด์ค", "๊ณต์ ", "์ดํด์ฌ", "์น๋ฐํจ", | |
"์ง์งํจ", "๋์", "๋ฐฐ๋ ค", "๊ฑฐ๋ถ", "๋น๋ฐ", "์๊ธฐ์ค์ฌ์ ", "์ธ๋ก์", "๋น๋", | |
"๋ฌด๊ด์ฌ", "์งํฌ", "๊ฐ๋ฑ", "๊ต๋ฅ ํ๋ฐํจ", "์ํต ์ํจ", "๋ค์ ํจ" | |
] | |
personality_keywords = [ | |
"์์ ๊ฐ", "์ฑ ์๊ฐ", "ํฌ์ฉ๋ ฅ", "๊ธ์ ์ ํ๋", "์ ์ฐ์ฑ", | |
"๊ฐ์์ฑ", "์๋ฏผํจ", "ํ๋ฐํจ", "์ฐจ๋ถํจ", "์ ๊ทน์ ", | |
"์๊ทน์ ", "์์ค์", "๊ฒธ์ํจ", "์์ ๊ฐ", "์์์ ", | |
"๋ฌด๊ธฐ๋ ฅ", "์ฑ์ค", "์ฌ์ธํจ", "์ฐ๋งํจ", "์กฐ๊ธํจ", | |
"๊ฑฐ์น ๋ค", "๊ธ์ ์ ", "์น์ ํจ", "๋ถ๋ ธ", "์ธ๋ด์ฌ", | |
"๋ ๋ฆฝ์ ", "์์ฒ", "์จํํจ", "๋ฐ๋ปํจ", "๋ฏธ์", | |
"์ฌ์น", "์ ๋จธ", "์ต์ธํจ", "์ฐ์ธํจ", "๋ถ์ ์ " | |
] | |
# ์ปฌ๋ผ ์์ฑ | |
col1, col2 = st.columns(2) | |
with col1: | |
# ์ ๋ ฅ ํ๋ ๋์ ์์ฑ ์ ์ ์ธ์ ์ํ ์ ๋ฐ์ดํธ | |
for i in range(5): | |
record_key = f"record_{i}" | |
# ์ ๋ก๋๋ ๋ฐ์ดํฐ๊ฐ ์๊ณ ํด๋น ์ธ๋ฑ์ค์ ๋ฐ์ดํฐ๊ฐ ์๋ ๊ฒฝ์ฐ | |
if uploaded_data is not None and i < len(uploaded_data): | |
data_value = uploaded_data.iloc[i, 0] | |
if pd.isna(data_value): | |
st.session_state[record_key] = "" | |
else: | |
st.session_state[record_key] = str(data_value) | |
elif record_key not in st.session_state: | |
st.session_state[record_key] = "" | |
# ์์ ฏ ์์ฑ | |
for i in range(5): | |
record_key = f"record_{i}" | |
with st.expander(f"์ ๋ ฅ์ฐฝ {i+1}", expanded=False): | |
st.text_area("์ํ๊ธฐ๋ก๋ถ ๋ด์ฉ ์ ๋ ฅ", key=record_key, height=150, value=st.session_state[record_key]) | |
with col2: | |
# ํค์๋ ์ ํ ์ต์ | |
st.write("ํค์๋ ์ ํ") | |
selected_attitude = st.multiselect("ํ์ตํ๋", attitude_keywords) | |
selected_relationship = st.multiselect("๊ต์ฐ๊ด๊ณ", relationship_keywords) | |
selected_personality = st.multiselect("์ฑ๊ฒฉ", personality_keywords) | |
# ๋ฐ์ดํฐ ์ฒ๋ฆฌ ๋ฐ ๊ฒฐ๊ณผ ์ถ๋ ฅ | |
if st.button("์์ฑ"): | |
with st.spinner('์์ฑ์ค...'): | |
input_data = "\n".join([st.session_state[f'record_{i}'] for i in range(5) if st.session_state[f'record_{i}']]) | |
# ํค์๋๋ฅผ ์ ๋ ฅ ๋ฐ์ดํฐ์ ์ถ๊ฐ | |
keyword_data = " ".join(selected_attitude + selected_relationship + selected_personality) | |
full_data = input_data + "\n" + keyword_data | |
generated_record = generate_living_record(full_data) | |
st.write("์์ฑ๋ ์ํ๊ธฐ๋ก๋ถ:", generated_record) |