Spaces:
Sleeping
Sleeping
Download app.py from re-eul/mulberry-negotiation: direct link, hf CLI and curl.
- Browser
- Download file 11.3 kB
-
https://huggingface.co/spaces/re-eul/mulberry-negotiation/resolve/main/app.py
- Command line
-
hf download hf://spaces/re-eul/mulberry-negotiation/app.py
-
curl -L -o app.py https://huggingface.co/spaces/re-eul/mulberry-negotiation/resolve/main/app.py
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"์ง์ญ: ์ธ์ ๊ตฐ ์ํ๋ฉด") | |