Problemsolver / app.py
soojeongcrystal's picture
Update app.py
0f4b1a1 verified
import streamlit as st
import pandas as pd
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from datetime import datetime
# Set page title and favicon
st.set_page_config(page_title="๊ณ ๋ฏผ ํ•ด๊ฒฐ์‚ฌ", page_icon=":sunglasses:")
# Google Sheets ์ธ์ฆ ์„ค์ •
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name("analog-marking-416002-e369840763fa.json", scope)
client = gspread.authorize(creds)
# Google Sheets ๋ฌธ์„œ ์—ด๊ธฐ
sheet = client.open("generation_streamlit_0302").sheet1
# ์‚ฌ์šฉ์ž ์กฐ์–ธ ๋ฐ์ดํ„ฐ๋ฅผ Google Sheets ๋ฌธ์„œ์— ์ถ”๊ฐ€ํ•˜๋Š” ํ•จ์ˆ˜
def record_advice(experience, concern, advice):
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
sheet.append_row([timestamp, experience, concern, advice])
# ์Šคํƒ€์ผ๋ง์„ ์œ„ํ•œ CSS
st.markdown("""
<style>
.header {
font-size: 30px;
font-weight: bold;
margin-bottom: 20px;
color: #333; /* Dark gray color */
padding: 10px;
border-radius: 10px;
}
.description-box {
background-color: #f0f0f0; /* Light gray background */
padding: 10px;
border-radius: 10px;
margin-bottom: 20px; /* ์กฐ์ •๋œ ์—ฌ๋ฐฑ */
}
</style>
""", unsafe_allow_html=True)
# ํ—ค๋” ํ‘œ์‹œ
st.markdown('<div class="header"> ํŒ€์žฅ๋“ค ๊ณ ๋ฏผ์— ์กฐ์–ธ ํ•œ๋งˆ๋”” โ˜๐Ÿป </div>', unsafe_allow_html=True)
# ์‚ฌ์šฉ์ž ์•ˆ๋‚ด ๋ฌธ๊ตฌ ํ‘œ์‹œ
st.markdown("""
<div class="description-box">
<p> How to use </br>
1. ์กฐ์–ธ์„ ํ•ด์ฃผ๊ณ  ์‹ถ์€ ํŒ€์žฅ ์—ฐ์ฐจ๋ฅผ ๊ณจ๋ผ์ฃผ์„ธ์š”. </br>
2. ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ์„ ํƒ๋œ ์—ฐ์ฐจ์˜ ๊ณ ๋ฏผ ์ค‘ ํ•˜๋‚˜๊ฐ€ ๋žœ๋ค์œผ๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. </br>
3. ๊ณ ๋ฏผ ์•„๋ž˜์— ์œ„์น˜ํ•œ ์ž…๋ ฅ ์ฐฝ์— ์กฐ์–ธ์„ ์ž‘์„ฑํ•œ ํ›„, '์กฐ์–ธ ์ œ์ถœํ•˜๊ธฐ' ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”. </br>
4. ์•„๋ž˜์— ์„ฑ๊ณต์ ์œผ๋กœ ์ œ์ถœ๋˜์—ˆ๋‹ค๋Š” ์ดˆ๋ก์ƒ‰ ๋ฉ”์‹œ์ง€๊ฐ€ ๋œจ๋Š” ๊ฒƒ์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”. </br>
</p>
</div>
""", unsafe_allow_html=True)
# Load Excel file into DataFrame
df = pd.read_excel("painpoint21.xlsx")
# ๋ฒ„ํŠผ ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ
buttons = st.columns(3)
experience_levels = ['์ €์—ฐ์ฐจ ํŒ€์žฅ', '์ค‘๊ฐ„์—ฐ์ฐจ ํŒ€์žฅ', '๊ณ ์—ฐ์ฐจ ํŒ€์žฅ']
selected_concern = None
# ๋ฒ„ํŠผ ์ƒ์„ฑ ๋ฐ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ
for i, experience_level in enumerate(experience_levels):
with buttons[i]:
if st.session_state.get(experience_level):
st.button(experience_level, disabled=True) # ํ™œ์„ฑํ™”๋œ ๋ฒ„ํŠผ ๋น„ํ™œ์„ฑํ™” ํ‘œ์‹œ
elif st.button(experience_level):
# ์„ ํƒ๋œ experience_level์„ ์ €์žฅ
st.session_state['experience'] = experience_level # ์ด ๋ถ€๋ถ„์ด ์ถ”๊ฐ€๋จ
st.session_state[experience_level] = True
with st.spinner("๊ณ ๋ฏผ์„ ๋žœ๋ค์œผ๋กœ ๋ฝ‘๋Š” ์ค‘์ž…๋‹ˆ๋‹ค. ์ž ์‹œ ๊ธฐ๋‹ค๋ ค์ฃผ์„ธ์š”..."):
concerns = df[experience_level].dropna()
if not concerns.empty:
selected_concern = concerns.sample(n=1).iloc[0]
st.session_state['selected_concern'] = selected_concern
else:
st.error(f"{experience_level}์— ํ•ด๋‹นํ•˜๋Š” ๊ณ ๋ฏผ์ด ์—†์Šต๋‹ˆ๋‹ค.")
# ์„ ํƒ๋œ ๊ณ ๋ฏผ ๋‚ด์šฉ์„ ์ „์ฒด ํ™”๋ฉด์— ํ‘œ์‹œ
if 'selected_concern' in st.session_state:
st.markdown(f"<h3 style='font-size: 20px;'>์„ ํƒ๋œ ๊ณ ๋ฏผ:</h3>", unsafe_allow_html=True)
st.markdown(f"<div style='white-space: pre-wrap; word-wrap: break-word; color: navy;'>{st.session_state['selected_concern']}</div>", unsafe_allow_html=True)
# ๊ณ ๋ฏผ๊ณผ ์กฐ์–ธ ์š”์ฒญ ์‚ฌ์ด์— ๊ณต๊ฐ„ ์ถ”๊ฐ€
st.write("")
if 'selected_concern' in st.session_state:
advice = st.text_area(f"### ๊ณ ๋ฏผ์— ๋Œ€ํ•œ ์กฐ์–ธ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”:", key="advice_input", value="", height=200) # text_area์˜ ๋†’์ด๋ฅผ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค.
st.write("<style>div[data-testid='stButton']>button {font-size: 18px;}</style>", unsafe_allow_html=True) # ๋ฒ„ํŠผ์˜ ๊ธ€์ž ํฌ๊ธฐ๋ฅผ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค.
submit_advice_button = st.button("์กฐ์–ธ ์ œ์ถœํ•˜๊ธฐ")
if submit_advice_button and advice.strip():
record_advice(st.session_state['experience'], st.session_state['selected_concern'], advice)
st.success("์กฐ์–ธ์ด ์„ฑ๊ณต์ ์œผ๋กœ ์ œ์ถœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!")
# ์‚ฌ์šฉ์ž๊ฐ€ ์กฐ์–ธ์„ ์ œ์ถœํ•œ ํ›„ ์ž…๋ ฅ ํ•„๋“œ๋ฅผ ๋น„์šฐ๋Š” ๋Œ€์‹ , ์‚ฌ์šฉ์ž์—๊ฒŒ ํŽ˜์ด์ง€ ์ƒˆ๋กœ๊ณ ์นจ์„ ์œ ๋„
st.markdown("์ƒˆ๋กœ์šด ๊ณ ๋ฏผ์„ ์‚ดํŽด๋ณด๋ ค๋ฉด, ํŽ˜์ด์ง€๋ฅผ ์ƒˆ๋กœ๊ณ ์นจํ•ด์ฃผ์„ธ์š”.")