re-eul's picture
Update app.py
eb474a2 verified
Raw History Blame Contribute Delete
11.3 kB
"""
๐ŸŒพ Mulberry ์—์ด์ „ํŠธ ํ˜‘์ƒ ์—”์ง„
์ธ์ œ๊ตฐ ์„œํ™”๋ฉด ๊ณต๋™๊ตฌ๋งค ์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ
๊ด‘๊ณ ๋น„ ์—†๋Š” ๊ด‘๊ณ  - Agent-to-Agent Negotiation Demo
"""
import streamlit as st
import time
from agent_passport import AgentPassport
from negotiation_engine import NegotiationEngine
from data.inje_data import get_items, get_households, get_agent_profile
# ํŽ˜์ด์ง€ ์„ค์ •
st.set_page_config(
page_title="Mulberry ํ˜‘์ƒ ์—”์ง„",
page_icon="๐ŸŒพ",
layout="wide",
initial_sidebar_state="expanded"
)
# ์„ธ์…˜ ์ƒํƒœ ์ดˆ๊ธฐํ™”
if 'items' not in st.session_state:
st.session_state['items'] = get_items()
if 'agents' not in st.session_state:
# ์—์ด์ „ํŠธ ์ดˆ๊ธฐํ™”
st.session_state['agents'] = {}
# ์ˆ˜์š” ์ง‘๊ณ„ ์—์ด์ „ํŠธ
demand_profile = get_agent_profile('demand_agent')
demand_agent = AgentPassport(
agent_name=demand_profile['agent_name'],
initial_region=demand_profile['initial_region'],
task_type=demand_profile['task_type']
)
demand_agent.self_status_info['spirit_score'] = demand_profile['spirit_score']
demand_agent.update_rapport(demand_profile['rapport_level'])
st.session_state['agents']['demand'] = demand_agent
# ๊ณต๊ธ‰์‚ฌ ์—์ด์ „ํŠธ๋“ค
for agent_id in ['agent_inje_apple', 'agent_flour_supplier', 'agent_clothes_supplier', 'agent_hwangtae', 'agent_potato']:
profile = get_agent_profile(agent_id)
agent = AgentPassport(
agent_name=profile['agent_name'],
initial_region=profile['initial_region'],
task_type=profile['task_type']
)
agent.self_status_info['spirit_score'] = profile.get('spirit_score', 3.0)
agent.update_rapport(profile.get('rapport_level', 0.5))
st.session_state['agents'][agent_id] = agent
if 'engine' not in st.session_state:
st.session_state['engine'] = NegotiationEngine()
if 'selected_item' not in st.session_state:
st.session_state['selected_item'] = None
# ํ—ค๋”
st.title("๐ŸŒพ Mulberry ์—์ด์ „ํŠธ ํ˜‘์ƒ ์—”์ง„")
st.caption("์ธ์ œ๊ตฐ ์„œํ™”๋ฉด ๊ณต๋™๊ตฌ๋งค ์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ - AI ์—์ด์ „ํŠธ๊ฐ€ ์ฃผ๋ฏผ์„ ์œ„ํ•ด ๊ฐ€๊ฒฉ์„ ํ˜‘์ƒํ•ฉ๋‹ˆ๋‹ค")
# ์‚ฌ์ด๋“œ๋ฐ”
with st.sidebar:
st.header("๐Ÿ“Š Dashboard")
# ์ „์ฒด ํ†ต๊ณ„
total_items = len(st.session_state['items'])
active_items = sum(1 for item in st.session_state['items'].values() if item['current'] > 0)
total_participants = sum(item['current'] for item in st.session_state['items'].values())
st.metric("์ด ํ’ˆ๋ชฉ", f"{total_items}๊ฐœ")
st.metric("์ง„ํ–‰ ์ค‘", f"{active_items}๊ฐœ")
st.metric("์ฐธ์—ฌ ๊ฐ€๊ตฌ", f"{total_participants}๊ฐ€๊ตฌ")
st.divider()
# ์ˆ˜์š” ์—์ด์ „ํŠธ ์ •๋ณด
demand_agent = st.session_state['agents']['demand']
st.subheader("๐ŸŒฒ ์ˆ˜์š” ์ง‘๊ณ„ ์—์ด์ „ํŠธ")
st.write(f"**{demand_agent.agent_name}**")
st.metric("Spirit Score", f"{demand_agent.self_status_info['spirit_score']:.1f}/5.0")
st.metric("ํ˜‘์ƒ๋ ฅ", f"{demand_agent.calculate_negotiation_power():.0%}")
st.metric("์‹ ๋ขฐ ๋“ฑ๊ธ‰", demand_agent.self_status_info['trust_level'])
# ๋ฉ”์ธ ํƒญ
tab1, tab2, tab3 = st.tabs([
"๐Ÿ“ฆ ๊ณต๋™๊ตฌ๋งค ํ˜„ํ™ฉ",
"๐Ÿค ์‹ค์‹œ๊ฐ„ ํ˜‘์ƒ",
"๐Ÿ“ˆ ๊ฒฝ์ œ์  ์ž„ํŒฉํŠธ"
])
with tab1:
st.subheader("๐Ÿ›’ ์„œํ™”๋ฉด ๊ณต๋™๊ตฌ๋งค ๋ฆฌ์ŠคํŠธ")
st.info("๐Ÿ’ก Mulberry ์—์ด์ „ํŠธ๋“ค์ด ์ฃผ๋ฏผ๋“ค์˜ ํ•„์š”๋ฅผ ๋ถ„์„ํ•ด ์ตœ์ ์˜ ๊ฑฐ๋ž˜๋ฅผ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค.")
# ๊ณต๋™๊ตฌ๋งค ์•„์ดํ…œ ํ‘œ์‹œ
for item_name, item_info in st.session_state['items'].items():
with st.expander(
f"๐Ÿ“ฆ {item_name} (ํ˜„์žฌ {item_info['current']}/{item_info['goal']}๋ช… ์ฐธ์—ฌ์ค‘)",
expanded=True
):
col1, col2, col3 = st.columns([2, 2, 1])
# ํ˜‘์ƒ๋œ ๊ฐ€๊ฒฉ ๊ณ„์‚ฐ
supply_agent = st.session_state['agents'][item_info['agent_id']]
negotiated_price, details = st.session_state['engine'].negotiate_price(
base_price=item_info['base_price'],
current_quantity=item_info['current'],
target_goal=item_info['goal'],
demand_agent=st.session_state['agents']['demand'],
supply_agent=supply_agent
)
# ์—…๋ฐ์ดํŠธ
item_info['negotiated_price'] = negotiated_price
with col1:
st.metric(
"ํ˜„์žฌ ํ˜‘์ƒ๊ฐ€",
f"โ‚ฉ{negotiated_price:,}",
f"-โ‚ฉ{details['savings']:,} ({details['total_discount_rate']}%)",
delta_color="inverse"
)
progress = details['progress'] / 100
st.progress(progress)
st.caption(f"์ง„ํ–‰๋ฅ : {details['progress']}% (๋ชฉํ‘œ: {item_info['goal']}{item_info['unit']})")
with col2:
st.write("**์—์ด์ „ํŠธ ํ˜‘์ƒ ๋กœ๊ทธ**")
if details['progress'] >= 70:
st.success(f"โœ… ์—์ด์ „ํŠธ๊ฐ€ '{item_info['supplier']}'์™€ ๋Œ€๋Ÿ‰ ๊ตฌ๋งค ์กฐ๊ฑด์œผ๋กœ {details['negotiation_bonus_rate']}% ์ถ”๊ฐ€ ํ• ์ธ์„ ํ˜‘์ƒํ–ˆ์Šต๋‹ˆ๋‹ค!")
elif details['progress'] >= 50:
st.info(f"๐Ÿ” ํ˜„์žฌ {details['base_discount_rate']}% ํ• ์ธ ์ค‘. ์ธ์›์ด ๋” ๋ชจ์ด๋ฉด ๊ฐ€๊ฒฉ์ด ํ•œ ๋‹จ๊ณ„ ๋” ๋‚ด๋ ค๊ฐ‘๋‹ˆ๋‹ค.")
else:
st.warning(f"โณ ํ˜„์žฌ {item_info['current']}๋ช… ์ฐธ์—ฌ. ๋ชฉํ‘œ๊นŒ์ง€ {item_info['goal'] - item_info['current']}๋ช… ๋” ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.")
st.caption(f"๊ณต๊ธ‰์‚ฌ: {item_info['supplier']} (์‹ ๋ขฐ๋„: {supply_agent.self_status_info['spirit_score']:.1f}/5.0)")
with col3:
if st.button("์ฐธ์—ฌํ•˜๊ธฐ", key=f"join_{item_name}"):
item_info['current'] += 1
st.success("์ฐธ์—ฌ ์™„๋ฃŒ!")
st.balloons()
st.rerun()
if st.button("ํ˜‘์ƒ ๋ณด๊ธฐ", key=f"view_{item_name}"):
st.session_state['selected_item'] = item_name
st.rerun()
with tab2:
st.subheader("๐Ÿค ์—์ด์ „ํŠธ ๊ฐ„ ์‹ค์‹œ๊ฐ„ ํ˜‘์ƒ ํ”„๋กœ์„ธ์Šค")
if st.session_state['selected_item']:
item_name = st.session_state['selected_item']
item_info = st.session_state['items'][item_name]
st.info(f"**{item_name}** ํ˜‘์ƒ ํ”„๋กœ์„ธ์Šค๋ฅผ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ํ•ฉ๋‹ˆ๋‹ค...")
# ํ˜‘์ƒ ์ •๋ณด
supply_agent = st.session_state['agents'][item_info['agent_id']]
negotiated_price = item_info.get('negotiated_price', item_info['base_price'])
# ํ˜‘์ƒ ๋Œ€ํ™” ์ƒ์„ฑ
dialogues = st.session_state['engine'].generate_negotiation_dialogue(
item_name=item_name,
base_price=item_info['base_price'],
current_quantity=item_info['current'],
target_goal=item_info['goal'],
negotiated_price=negotiated_price,
demand_agent=st.session_state['agents']['demand'],
supply_agent=supply_agent
)
# ๋Œ€ํ™” ํ‘œ์‹œ
chat_container = st.container(border=True)
with chat_container:
for i, dialogue in enumerate(dialogues):
if dialogue['speaker'] == 'demand':
with st.chat_message("assistant", avatar=dialogue['avatar']):
st.write(f"**{dialogue['agent_name']}**")
st.write(dialogue['message'])
elif dialogue['speaker'] == 'supply':
with st.chat_message("user", avatar=dialogue['avatar']):
st.write(f"**{dialogue['agent_name']}**")
st.write(dialogue['message'])
else: # system
with st.chat_message("ai", avatar=dialogue['avatar']):
st.write(dialogue['message'])
# ์• ๋‹ˆ๋ฉ”์ด์…˜ ํšจ๊ณผ
if i < len(dialogues) - 1:
time.sleep(0.3)
# ํ˜‘์ƒ ์ƒ์„ธ
st.divider()
col1, col2, col3 = st.columns(3)
details = st.session_state['engine'].negotiation_history[-1]
col1.metric("์›๊ฐ€", f"โ‚ฉ{details['base_price']:,}")
col2.metric("ํ˜‘์ƒ๊ฐ€", f"โ‚ฉ{negotiated_price:,}", f"-{details['total_discount_rate']}%")
col3.metric("์ ˆ๊ฐ์•ก", f"โ‚ฉ{details['savings']:,}")
else:
st.info("๐Ÿ‘ˆ ์™ผ์ชฝ '๊ณต๋™๊ตฌ๋งค ํ˜„ํ™ฉ' ํƒญ์—์„œ 'ํ˜‘์ƒ ๋ณด๊ธฐ' ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”!")
with tab3:
st.subheader("๐Ÿ“ˆ ์ธ์ œ๊ตฐ ์„œํ™”๋ฉด ๊ฒฝ์ œ ์ž„ํŒฉํŠธ (Real-time)")
# ๊ฒฝ์ œ์  ์ž„ํŒฉํŠธ ๊ณ„์‚ฐ
impact = st.session_state['engine'].calculate_economic_impact(
st.session_state['items'],
st.session_state['agents']
)
# ์ฃผ์š” ์ง€ํ‘œ
col1, col2, col3 = st.columns(3)
col1.metric(
"์ฃผ๋ฏผ ์ด ์ ˆ๊ฐ์•ก",
f"โ‚ฉ{impact['total_savings']:,}",
f"โ†‘ {impact['savings_rate']}%"
)
col2.metric(
"Mulberry ์šด์˜ ์ˆ˜์ต",
f"โ‚ฉ{impact['mulberry_revenue']:,}",
"์ •์‚ฐ ์˜ˆ์ •",
delta_color="normal"
)
col3.metric(
"AP2 ์ฆ๋ช… ์™„๋ฃŒ ๊ฑด์ˆ˜",
f"{impact['total_transactions']}๊ฑด",
"Verified",
delta_color="off"
)
st.divider()
# ์ƒ์„ธ ๋ถ„์„
col1, col2 = st.columns(2)
with col1:
st.subheader("๐Ÿ’ฐ ๋น„์šฉ ๋ถ„์„")
st.write("**์›๋ž˜ ์ด ๋น„์šฉ:**")
st.write(f"โ‚ฉ{impact['total_original']:,}")
st.write("**ํ˜‘์ƒ ํ›„ ์ด ๋น„์šฉ:**")
st.write(f"โ‚ฉ{impact['total_negotiated']:,}")
st.write("**์ด ์ ˆ๊ฐ์•ก:**")
st.write(f"โ‚ฉ{impact['total_savings']:,} ({impact['savings_rate']}%)")
st.divider()
st.write("**์ˆ˜์ต ๋ฐฐ๋ถ„:**")
st.write(f"- Mulberry ์šด์˜: โ‚ฉ{impact['mulberry_revenue']:,} (3%)")
st.write(f"- AP2 ์ˆ˜์ˆ˜๋ฃŒ: โ‚ฉ{impact['ap2_fee']:,} (0.5%)")
st.write(f"- ์ฃผ๋ฏผ ์ˆœํ˜œํƒ: โ‚ฉ{impact['net_community_benefit']:,}")
with col2:
st.subheader("๐ŸŽฏ ์‚ฌํšŒ์  ๊ฐ€์น˜")
households = get_households()
st.metric("์ฐธ์—ฌ ๊ฐ€๊ตฌ", f"{len(households)}๊ฐ€๊ตฌ")
st.metric("ํ‰๊ท  ์ ˆ๊ฐ์•ก/๊ฐ€๊ตฌ", f"โ‚ฉ{impact['avg_savings_per_transaction']:,}")
st.metric("์ด ๊ฑฐ๋ž˜ ๊ฑด์ˆ˜", f"{impact['total_transactions']}๊ฑด")
st.divider()
st.write("**Mulberry ๋ชจ๋ธ์˜ ์ฐจ๋ณ„์ :**")
st.success("โœ… ๊ด‘๊ณ ๋น„ ์ œ๋กœ - ๋งˆ์ผ€ํŒ… ๋น„์šฉ์ด ๊ฐ€๊ฒฉ ํ• ์ธ์œผ๋กœ ์ „ํ™˜")
st.success("โœ… AI ํ˜‘์ƒ - 24์‹œ๊ฐ„ ์ž๋™์œผ๋กœ ์ตœ์ ๊ฐ€ ํ˜‘์ƒ")
st.success("โœ… ํˆฌ๋ช…์„ฑ - AP2 ๋ธ”๋ก์ฒด์ธ์œผ๋กœ ๋ชจ๋“  ๊ฑฐ๋ž˜ ๊ฒ€์ฆ")
st.success("โœ… ์ƒ์ƒ - ์ง€์—ญ ๊ฒฝ์ œ ํ™œ์„ฑํ™” + ์ฃผ๋ฏผ ๋ณต์ง€ ํ–ฅ์ƒ")
# ํ‘ธํ„ฐ
st.divider()
col1, col2, col3 = st.columns([2, 1, 1])
with col1:
st.caption("๐ŸŒพ Mulberry Project - ์‹ํ’ˆ์‚ฌ๋ง‰ํ™” ์ œ๋กœ")
st.caption("Powered by Agent Engine + mHC + AP2")
with col2:
if st.button("๐Ÿ”„ ์ „์ฒด ์ดˆ๊ธฐํ™”"):
st.session_state['items'] = get_items()
st.session_state['engine'] = NegotiationEngine()
st.rerun()
with col3:
st.caption(f"๋ฒ„์ „: v1.0.0")
st.caption(f"์ง€์—ญ: ์ธ์ œ๊ตฐ ์„œํ™”๋ฉด")