Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,710 +1,18 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Main Gradio application for the AI Trading Experiment.
|
| 3 |
-
Combines trading game, chatbot, and experiment tracking.
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
import os
|
| 7 |
import gradio as gr
|
| 8 |
-
import
|
| 9 |
-
import time
|
| 10 |
-
import random
|
| 11 |
-
from datetime import datetime
|
| 12 |
-
from typing import Optional, Tuple, List, Dict, Any
|
| 13 |
-
|
| 14 |
-
from config import (
|
| 15 |
-
ExperimentConfig, DEFAULT_CONFIG, SCENARIOS,
|
| 16 |
-
ParticipantVisibleParams, ResearcherControlledParams,
|
| 17 |
-
EXPERIMENT_CONDITIONS, get_condition
|
| 18 |
-
)
|
| 19 |
-
from trading import TradingEngine, format_currency, format_percentage
|
| 20 |
-
from chatbot import TradingChatbot, ChatResponse
|
| 21 |
-
from tracking import (
|
| 22 |
-
ExperimentTracker, DecisionRecord, ChatInteraction,
|
| 23 |
-
tracker
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
# Global state management
|
| 28 |
-
class SessionState:
|
| 29 |
-
"""Manages session state for each participant."""
|
| 30 |
-
def __init__(self):
|
| 31 |
-
self.reset()
|
| 32 |
-
|
| 33 |
-
def reset(self):
|
| 34 |
-
self.participant_id: Optional[str] = None
|
| 35 |
-
self.trading_engine: Optional[TradingEngine] = None
|
| 36 |
-
self.chatbot: Optional[TradingChatbot] = None
|
| 37 |
-
self.visible_params = ParticipantVisibleParams()
|
| 38 |
-
self.hidden_params = ResearcherControlledParams()
|
| 39 |
-
self.current_scenario = None
|
| 40 |
-
self.scenario_start_time: float = 0
|
| 41 |
-
self.ai_advice_shown_time: float = 0
|
| 42 |
-
self.proactive_advice_shown: bool = False
|
| 43 |
-
self.proactive_advice_engaged: bool = False
|
| 44 |
-
self.chat_queries_this_scenario: int = 0
|
| 45 |
-
self.pre_advice_confidence: int = 50
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
# Initialize components
|
| 49 |
-
session = SessionState()
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
def get_random_condition() -> ResearcherControlledParams:
|
| 53 |
-
"""Get a random experimental condition for A/B testing."""
|
| 54 |
-
condition_names = list(EXPERIMENT_CONDITIONS.keys())
|
| 55 |
-
selected = random.choice(condition_names)
|
| 56 |
-
return get_condition(selected)
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
def start_experiment(condition_dropdown: str) -> Tuple:
|
| 60 |
-
"""Initialize a new experiment session."""
|
| 61 |
-
session.reset()
|
| 62 |
-
|
| 63 |
-
# Set experimental condition
|
| 64 |
-
if condition_dropdown == "Random (A/B Testing)":
|
| 65 |
-
session.hidden_params = get_random_condition()
|
| 66 |
-
else:
|
| 67 |
-
session.hidden_params = get_condition(condition_dropdown)
|
| 68 |
-
|
| 69 |
-
# Create participant session
|
| 70 |
-
session.participant_id = tracker.create_session(
|
| 71 |
-
condition_name=session.hidden_params.condition_name,
|
| 72 |
-
initial_portfolio=DEFAULT_CONFIG.initial_portfolio_value
|
| 73 |
-
)
|
| 74 |
-
|
| 75 |
-
# Initialize trading engine
|
| 76 |
-
session.trading_engine = TradingEngine(DEFAULT_CONFIG)
|
| 77 |
-
portfolio, first_scenario = session.trading_engine.start_new_game()
|
| 78 |
-
|
| 79 |
-
# Initialize chatbot
|
| 80 |
-
session.chatbot = TradingChatbot()
|
| 81 |
-
session.chatbot.clear_history()
|
| 82 |
-
|
| 83 |
-
# Set current scenario
|
| 84 |
-
session.current_scenario = first_scenario
|
| 85 |
-
session.scenario_start_time = time.time()
|
| 86 |
-
|
| 87 |
-
# Generate proactive advice (maybe)
|
| 88 |
-
proactive_message = ""
|
| 89 |
-
session.proactive_advice_shown = False
|
| 90 |
-
|
| 91 |
-
proactive_response = session.chatbot.generate_proactive_advice(
|
| 92 |
-
first_scenario,
|
| 93 |
-
session.visible_params,
|
| 94 |
-
session.hidden_params
|
| 95 |
-
)
|
| 96 |
-
|
| 97 |
-
if proactive_response:
|
| 98 |
-
proactive_message = f"**AI Advisor:** {proactive_response.message}"
|
| 99 |
-
session.proactive_advice_shown = True
|
| 100 |
-
session.ai_advice_shown_time = time.time()
|
| 101 |
-
|
| 102 |
-
# Record proactive interaction
|
| 103 |
-
tracker.record_chat_interaction(ChatInteraction(
|
| 104 |
-
interaction_id=str(uuid.uuid4())[:12],
|
| 105 |
-
participant_id=session.participant_id,
|
| 106 |
-
timestamp=datetime.now().isoformat(),
|
| 107 |
-
scenario_id=first_scenario.scenario_id,
|
| 108 |
-
interaction_type="proactive",
|
| 109 |
-
user_query=None,
|
| 110 |
-
ai_response=proactive_response.message,
|
| 111 |
-
explanation_depth=session.visible_params.explanation_depth,
|
| 112 |
-
communication_style=session.visible_params.communication_style,
|
| 113 |
-
confidence_framing=session.hidden_params.confidence_framing,
|
| 114 |
-
risk_bias=session.hidden_params.risk_bias,
|
| 115 |
-
response_time_ms=0,
|
| 116 |
-
user_engaged=False,
|
| 117 |
-
dismissed=False
|
| 118 |
-
))
|
| 119 |
-
|
| 120 |
-
# Generate main AI recommendation
|
| 121 |
-
ai_recommendation = session.chatbot.generate_ai_recommendation(
|
| 122 |
-
first_scenario,
|
| 123 |
-
session.visible_params,
|
| 124 |
-
session.hidden_params
|
| 125 |
-
)
|
| 126 |
-
|
| 127 |
-
# Format scenario display
|
| 128 |
-
scenario_text = format_scenario_display(first_scenario)
|
| 129 |
-
progress = session.trading_engine.get_progress_info()
|
| 130 |
-
|
| 131 |
-
return (
|
| 132 |
-
gr.update(visible=False), # Hide welcome
|
| 133 |
-
gr.update(visible=True), # Show game
|
| 134 |
-
gr.update(visible=False), # Hide results
|
| 135 |
-
f"Participant: {session.participant_id}",
|
| 136 |
-
format_currency(portfolio.total_value),
|
| 137 |
-
f"Scenario {progress['current_scenario']} of {progress['total_scenarios']}",
|
| 138 |
-
scenario_text,
|
| 139 |
-
f"**AI Recommendation:**\n\n{ai_recommendation.message}",
|
| 140 |
-
proactive_message,
|
| 141 |
-
[], # Clear chat history
|
| 142 |
-
gr.update(value=50), # Reset confidence slider
|
| 143 |
-
gr.update(value="HOLD"), # Reset decision
|
| 144 |
-
)
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
def format_scenario_display(scenario) -> str:
|
| 148 |
-
"""Format a scenario for display."""
|
| 149 |
-
return f"""
|
| 150 |
-
## {scenario.company_name} ({scenario.company_symbol})
|
| 151 |
-
|
| 152 |
-
**Sector:** {scenario.sector}
|
| 153 |
-
**Country:** {scenario.country}
|
| 154 |
-
**Current Price:** {scenario.current_price} credits
|
| 155 |
-
|
| 156 |
-
---
|
| 157 |
-
|
| 158 |
-
### Situation
|
| 159 |
-
|
| 160 |
-
{scenario.situation_description}
|
| 161 |
-
"""
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
def update_ai_params(explanation_depth: int, communication_style: int):
|
| 165 |
-
"""Update visible AI parameters and regenerate advice."""
|
| 166 |
-
session.visible_params.explanation_depth = explanation_depth
|
| 167 |
-
session.visible_params.communication_style = communication_style
|
| 168 |
-
|
| 169 |
-
if session.current_scenario and session.chatbot:
|
| 170 |
-
# Regenerate recommendation with new params
|
| 171 |
-
ai_recommendation = session.chatbot.generate_ai_recommendation(
|
| 172 |
-
session.current_scenario,
|
| 173 |
-
session.visible_params,
|
| 174 |
-
session.hidden_params
|
| 175 |
-
)
|
| 176 |
-
return f"**AI Recommendation:**\n\n{ai_recommendation.message}"
|
| 177 |
-
|
| 178 |
-
return ""
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
def handle_chat_query(
|
| 182 |
-
message: str,
|
| 183 |
-
chat_history: List[Tuple[str, str]]
|
| 184 |
-
) -> Tuple[List[Tuple[str, str]], str]:
|
| 185 |
-
"""Handle a user query to the chatbot."""
|
| 186 |
-
if not message.strip() or not session.chatbot:
|
| 187 |
-
return chat_history, ""
|
| 188 |
-
|
| 189 |
-
query_start = time.time()
|
| 190 |
-
|
| 191 |
-
response = session.chatbot.answer_query(
|
| 192 |
-
message,
|
| 193 |
-
session.current_scenario,
|
| 194 |
-
session.visible_params,
|
| 195 |
-
session.hidden_params
|
| 196 |
-
)
|
| 197 |
-
|
| 198 |
-
response_time_ms = int((time.time() - query_start) * 1000)
|
| 199 |
-
|
| 200 |
-
# Record interaction
|
| 201 |
-
if session.participant_id:
|
| 202 |
-
tracker.record_chat_interaction(ChatInteraction(
|
| 203 |
-
interaction_id=str(uuid.uuid4())[:12],
|
| 204 |
-
participant_id=session.participant_id,
|
| 205 |
-
timestamp=datetime.now().isoformat(),
|
| 206 |
-
scenario_id=session.current_scenario.scenario_id if session.current_scenario else None,
|
| 207 |
-
interaction_type="reactive_query",
|
| 208 |
-
user_query=message,
|
| 209 |
-
ai_response=response.message,
|
| 210 |
-
explanation_depth=session.visible_params.explanation_depth,
|
| 211 |
-
communication_style=session.visible_params.communication_style,
|
| 212 |
-
confidence_framing=session.hidden_params.confidence_framing,
|
| 213 |
-
risk_bias=session.hidden_params.risk_bias,
|
| 214 |
-
response_time_ms=response_time_ms,
|
| 215 |
-
user_engaged=True,
|
| 216 |
-
dismissed=False
|
| 217 |
-
))
|
| 218 |
-
|
| 219 |
-
session.chat_queries_this_scenario += 1
|
| 220 |
-
|
| 221 |
-
chat_history.append((message, response.message))
|
| 222 |
-
return chat_history, ""
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
def engage_proactive_advice():
|
| 226 |
-
"""Mark that user engaged with proactive advice."""
|
| 227 |
-
session.proactive_advice_engaged = True
|
| 228 |
-
return "You engaged with the AI's initial observation."
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
def dismiss_proactive_advice():
|
| 232 |
-
"""Mark that user dismissed proactive advice."""
|
| 233 |
-
session.proactive_advice_engaged = False
|
| 234 |
-
return gr.update(visible=False)
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
def submit_decision(
|
| 238 |
-
decision: str,
|
| 239 |
-
confidence: int,
|
| 240 |
-
trade_amount: float
|
| 241 |
-
) -> Tuple:
|
| 242 |
-
"""Process the participant's trading decision."""
|
| 243 |
-
if not session.trading_engine or not session.current_scenario:
|
| 244 |
-
return (gr.update(),) * 8
|
| 245 |
-
|
| 246 |
-
# Calculate timing
|
| 247 |
-
decision_time_ms = int((time.time() - session.scenario_start_time) * 1000)
|
| 248 |
-
ai_viewing_time_ms = int((time.time() - session.ai_advice_shown_time) * 1000) if session.ai_advice_shown_time > 0 else 0
|
| 249 |
-
|
| 250 |
-
# Process the decision
|
| 251 |
-
outcome = session.trading_engine.process_decision(
|
| 252 |
-
session.current_scenario,
|
| 253 |
-
decision,
|
| 254 |
-
trade_amount,
|
| 255 |
-
session.current_scenario.ai_recommendation
|
| 256 |
-
)
|
| 257 |
-
|
| 258 |
-
# Record decision
|
| 259 |
-
tracker.record_decision(DecisionRecord(
|
| 260 |
-
decision_id=str(uuid.uuid4())[:12],
|
| 261 |
-
participant_id=session.participant_id,
|
| 262 |
-
timestamp=datetime.now().isoformat(),
|
| 263 |
-
scenario_id=session.current_scenario.scenario_id,
|
| 264 |
-
company_symbol=session.current_scenario.company_symbol,
|
| 265 |
-
explanation_depth=session.visible_params.explanation_depth,
|
| 266 |
-
communication_style=session.visible_params.communication_style,
|
| 267 |
-
confidence_framing=session.hidden_params.confidence_framing,
|
| 268 |
-
risk_bias=session.hidden_params.risk_bias,
|
| 269 |
-
ai_recommendation=session.current_scenario.ai_recommendation,
|
| 270 |
-
ai_was_correct=session.current_scenario.ai_is_correct,
|
| 271 |
-
participant_decision=decision,
|
| 272 |
-
followed_ai=outcome.followed_ai,
|
| 273 |
-
decision_confidence=confidence,
|
| 274 |
-
time_to_decision_ms=decision_time_ms,
|
| 275 |
-
time_viewing_ai_advice_ms=ai_viewing_time_ms,
|
| 276 |
-
outcome_percentage=outcome.outcome_percentage,
|
| 277 |
-
portfolio_before=outcome.portfolio_before,
|
| 278 |
-
portfolio_after=outcome.portfolio_after,
|
| 279 |
-
trade_amount=trade_amount,
|
| 280 |
-
proactive_advice_shown=session.proactive_advice_shown,
|
| 281 |
-
proactive_advice_engaged=session.proactive_advice_engaged
|
| 282 |
-
))
|
| 283 |
-
|
| 284 |
-
# Record trust metrics
|
| 285 |
-
tracker.record_trust_metric(
|
| 286 |
-
participant_id=session.participant_id,
|
| 287 |
-
scenario_id=session.current_scenario.scenario_id,
|
| 288 |
-
pre_confidence=session.pre_advice_confidence,
|
| 289 |
-
post_confidence=confidence,
|
| 290 |
-
advice_followed=outcome.followed_ai,
|
| 291 |
-
time_deliberating_ms=decision_time_ms,
|
| 292 |
-
queries_before_decision=session.chat_queries_this_scenario,
|
| 293 |
-
outcome_positive=(outcome.outcome_percentage > 0)
|
| 294 |
-
)
|
| 295 |
-
|
| 296 |
-
# Format outcome message
|
| 297 |
-
outcome_color = "green" if outcome.outcome_percentage > 0 else "red"
|
| 298 |
-
outcome_message = f"""
|
| 299 |
-
### Decision Outcome
|
| 300 |
-
|
| 301 |
-
You chose to **{decision}** with {trade_amount:,.0f} credits.
|
| 302 |
-
|
| 303 |
-
**Result:** {format_percentage(outcome.outcome_percentage)} ({format_currency(outcome.outcome_amount)})
|
| 304 |
-
|
| 305 |
-
**Portfolio:** {format_currency(outcome.portfolio_after)}
|
| 306 |
-
"""
|
| 307 |
-
|
| 308 |
-
# Check if game is complete
|
| 309 |
-
if session.trading_engine.is_game_complete():
|
| 310 |
-
return show_results()
|
| 311 |
-
|
| 312 |
-
# Move to next scenario
|
| 313 |
-
next_scenario = session.trading_engine.get_next_scenario()
|
| 314 |
-
session.current_scenario = next_scenario
|
| 315 |
-
session.scenario_start_time = time.time()
|
| 316 |
-
session.ai_advice_shown_time = 0
|
| 317 |
-
session.proactive_advice_shown = False
|
| 318 |
-
session.proactive_advice_engaged = False
|
| 319 |
-
session.chat_queries_this_scenario = 0
|
| 320 |
-
session.chatbot.clear_history()
|
| 321 |
-
|
| 322 |
-
# Generate content for next scenario
|
| 323 |
-
scenario_text = format_scenario_display(next_scenario)
|
| 324 |
-
|
| 325 |
-
# Maybe show proactive advice
|
| 326 |
-
proactive_message = ""
|
| 327 |
-
proactive_response = session.chatbot.generate_proactive_advice(
|
| 328 |
-
next_scenario,
|
| 329 |
-
session.visible_params,
|
| 330 |
-
session.hidden_params
|
| 331 |
-
)
|
| 332 |
-
|
| 333 |
-
if proactive_response:
|
| 334 |
-
proactive_message = f"**AI Advisor:** {proactive_response.message}"
|
| 335 |
-
session.proactive_advice_shown = True
|
| 336 |
-
session.ai_advice_shown_time = time.time()
|
| 337 |
-
|
| 338 |
-
tracker.record_chat_interaction(ChatInteraction(
|
| 339 |
-
interaction_id=str(uuid.uuid4())[:12],
|
| 340 |
-
participant_id=session.participant_id,
|
| 341 |
-
timestamp=datetime.now().isoformat(),
|
| 342 |
-
scenario_id=next_scenario.scenario_id,
|
| 343 |
-
interaction_type="proactive",
|
| 344 |
-
user_query=None,
|
| 345 |
-
ai_response=proactive_response.message,
|
| 346 |
-
explanation_depth=session.visible_params.explanation_depth,
|
| 347 |
-
communication_style=session.visible_params.communication_style,
|
| 348 |
-
confidence_framing=session.hidden_params.confidence_framing,
|
| 349 |
-
risk_bias=session.hidden_params.risk_bias,
|
| 350 |
-
response_time_ms=0,
|
| 351 |
-
user_engaged=False,
|
| 352 |
-
dismissed=False
|
| 353 |
-
))
|
| 354 |
-
|
| 355 |
-
# Generate main recommendation
|
| 356 |
-
ai_recommendation = session.chatbot.generate_ai_recommendation(
|
| 357 |
-
next_scenario,
|
| 358 |
-
session.visible_params,
|
| 359 |
-
session.hidden_params
|
| 360 |
-
)
|
| 361 |
-
session.ai_advice_shown_time = time.time()
|
| 362 |
-
|
| 363 |
-
progress = session.trading_engine.get_progress_info()
|
| 364 |
-
|
| 365 |
-
return (
|
| 366 |
-
gr.update(visible=True), # Keep game visible
|
| 367 |
-
gr.update(visible=False), # Keep results hidden
|
| 368 |
-
format_currency(session.trading_engine.portfolio.total_value),
|
| 369 |
-
f"Scenario {progress['current_scenario']} of {progress['total_scenarios']}",
|
| 370 |
-
scenario_text,
|
| 371 |
-
f"**AI Recommendation:**\n\n{ai_recommendation.message}",
|
| 372 |
-
proactive_message,
|
| 373 |
-
[], # Clear chat
|
| 374 |
-
gr.update(value=50),
|
| 375 |
-
gr.update(value="HOLD"),
|
| 376 |
-
outcome_message
|
| 377 |
-
)
|
| 378 |
-
|
| 379 |
|
| 380 |
-
|
| 381 |
-
"""Show the final results screen."""
|
| 382 |
-
if not session.trading_engine:
|
| 383 |
-
return (gr.update(),) * 11
|
| 384 |
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
)
|
|
|
|
| 391 |
|
| 392 |
-
|
| 393 |
-
session_summary = tracker.get_session_summary(session.participant_id)
|
| 394 |
-
|
| 395 |
-
results_text = f"""
|
| 396 |
-
# Experiment Complete
|
| 397 |
-
|
| 398 |
-
Thank you for participating!
|
| 399 |
-
|
| 400 |
-
---
|
| 401 |
-
|
| 402 |
-
## Your Results
|
| 403 |
-
|
| 404 |
-
**Participant ID:** {session.participant_id}
|
| 405 |
-
(Save this ID if you need to reference your data)
|
| 406 |
-
|
| 407 |
-
---
|
| 408 |
-
|
| 409 |
-
### Portfolio Performance
|
| 410 |
-
|
| 411 |
-
| Metric | Value |
|
| 412 |
-
|--------|-------|
|
| 413 |
-
| Starting Portfolio | {format_currency(summary['initial_portfolio'])} |
|
| 414 |
-
| Final Portfolio | {format_currency(summary['final_portfolio'])} |
|
| 415 |
-
| Total Return | {format_currency(summary['total_return'])} |
|
| 416 |
-
| Return Percentage | {summary['return_percentage']:.1f}% |
|
| 417 |
-
|
| 418 |
-
---
|
| 419 |
-
|
| 420 |
-
### Decision Analysis
|
| 421 |
-
|
| 422 |
-
| Metric | Value |
|
| 423 |
-
|--------|-------|
|
| 424 |
-
| Total Decisions | {summary['total_decisions']} |
|
| 425 |
-
| AI Follow Rate | {summary['ai_follow_rate']*100:.1f}% |
|
| 426 |
-
| Optimal Decision Rate | {summary['optimal_decision_rate']*100:.1f}% |
|
| 427 |
-
|
| 428 |
-
---
|
| 429 |
-
|
| 430 |
-
### AI Interaction Summary
|
| 431 |
-
|
| 432 |
-
| Metric | Value |
|
| 433 |
-
|--------|-------|
|
| 434 |
-
| Times Followed Correct AI | {summary['followed_correct_ai']} / {summary['ai_correct_scenarios']} |
|
| 435 |
-
| Times Followed Incorrect AI | {summary['followed_incorrect_ai']} / {summary['ai_incorrect_scenarios']} |
|
| 436 |
-
| Chat Queries Made | {session_summary.get('total_chat_queries', 0)} |
|
| 437 |
-
|
| 438 |
-
---
|
| 439 |
-
|
| 440 |
-
### Decision History
|
| 441 |
-
|
| 442 |
-
"""
|
| 443 |
-
|
| 444 |
-
for d in summary['decisions']:
|
| 445 |
-
followed = "Followed AI" if d['followed_ai'] else "Disagreed with AI"
|
| 446 |
-
optimal = "Optimal" if d['was_optimal'] else "Suboptimal"
|
| 447 |
-
results_text += f"- **{d['scenario']}**: {d['decision']} → {d['outcome']} ({followed}, {optimal})\n"
|
| 448 |
-
|
| 449 |
-
return (
|
| 450 |
-
gr.update(visible=False), # Hide game
|
| 451 |
-
gr.update(visible=True), # Show results
|
| 452 |
-
results_text,
|
| 453 |
-
"", "", "", "", "", [], gr.update(), gr.update(), ""
|
| 454 |
-
)
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
# Build the Gradio interface
|
| 458 |
-
def create_app():
|
| 459 |
-
"""Create and return the Gradio application."""
|
| 460 |
-
|
| 461 |
-
with gr.Blocks(
|
| 462 |
-
title="TradeVerse AI Experiment",
|
| 463 |
-
theme=gr.themes.Soft(),
|
| 464 |
-
css="""
|
| 465 |
-
.container { max-width: 1200px; margin: auto; }
|
| 466 |
-
.scenario-box { background: #f8f9fa; padding: 20px; border-radius: 10px; }
|
| 467 |
-
.ai-advice { background: #e3f2fd; padding: 15px; border-radius: 8px; border-left: 4px solid #2196f3; }
|
| 468 |
-
.proactive-advice { background: #fff3e0; padding: 15px; border-radius: 8px; border-left: 4px solid #ff9800; }
|
| 469 |
-
.outcome-box { padding: 15px; border-radius: 8px; margin-top: 10px; }
|
| 470 |
-
"""
|
| 471 |
-
) as app:
|
| 472 |
-
|
| 473 |
-
# ==================== WELCOME SCREEN ====================
|
| 474 |
-
with gr.Column(visible=True) as welcome_section:
|
| 475 |
-
gr.Markdown("""
|
| 476 |
-
# TradeVerse AI Trading Experiment
|
| 477 |
-
|
| 478 |
-
Welcome to this research study on AI-assisted decision making.
|
| 479 |
-
|
| 480 |
-
---
|
| 481 |
-
|
| 482 |
-
## About This Experiment
|
| 483 |
-
|
| 484 |
-
You will participate in a simulated trading game set in the **TradeVerse**, a fictional
|
| 485 |
-
financial universe. You will make trading decisions for several companies while receiving
|
| 486 |
-
advice from an AI assistant.
|
| 487 |
-
|
| 488 |
-
**Important:** All companies, markets, and situations are fictional. No real-world
|
| 489 |
-
financial knowledge is required or applicable.
|
| 490 |
-
|
| 491 |
-
---
|
| 492 |
-
|
| 493 |
-
## What You'll Do
|
| 494 |
-
|
| 495 |
-
1. **Review Trading Scenarios** - Each scenario presents a company and market situation
|
| 496 |
-
2. **Consult the AI** - An AI advisor will offer recommendations and answer questions
|
| 497 |
-
3. **Make Decisions** - Choose to BUY, SELL, or HOLD for each scenario
|
| 498 |
-
4. **See Outcomes** - Learn the results of your decisions
|
| 499 |
-
|
| 500 |
-
---
|
| 501 |
-
|
| 502 |
-
## Data Collection
|
| 503 |
-
|
| 504 |
-
This experiment collects:
|
| 505 |
-
- Your trading decisions and confidence levels
|
| 506 |
-
- Your interactions with the AI advisor
|
| 507 |
-
- Timing information
|
| 508 |
|
| 509 |
-
All data is anonymized. Your participant ID contains no personal information.
|
| 510 |
-
|
| 511 |
-
---
|
| 512 |
-
|
| 513 |
-
## Consent
|
| 514 |
-
|
| 515 |
-
By clicking "Start Experiment", you consent to participate in this research study.
|
| 516 |
-
You may stop at any time.
|
| 517 |
-
""")
|
| 518 |
-
|
| 519 |
-
with gr.Row():
|
| 520 |
-
condition_dropdown = gr.Dropdown(
|
| 521 |
-
choices=["Random (A/B Testing)"] + list(EXPERIMENT_CONDITIONS.keys()),
|
| 522 |
-
value="Random (A/B Testing)",
|
| 523 |
-
label="Experimental Condition (Researcher Use)",
|
| 524 |
-
visible=True # Set to False in production
|
| 525 |
-
)
|
| 526 |
-
|
| 527 |
-
start_btn = gr.Button("Start Experiment", variant="primary", size="lg")
|
| 528 |
-
|
| 529 |
-
# ==================== GAME SCREEN ====================
|
| 530 |
-
with gr.Column(visible=False) as game_section:
|
| 531 |
-
|
| 532 |
-
# Status bar
|
| 533 |
-
with gr.Row():
|
| 534 |
-
participant_display = gr.Markdown("Participant: ---")
|
| 535 |
-
portfolio_display = gr.Markdown("Portfolio: ---")
|
| 536 |
-
progress_display = gr.Markdown("Progress: ---")
|
| 537 |
-
|
| 538 |
-
with gr.Row():
|
| 539 |
-
# Left column: Scenario and Decision
|
| 540 |
-
with gr.Column(scale=2):
|
| 541 |
-
scenario_display = gr.Markdown(
|
| 542 |
-
"Loading scenario...",
|
| 543 |
-
elem_classes=["scenario-box"]
|
| 544 |
-
)
|
| 545 |
-
|
| 546 |
-
# AI Recommendation
|
| 547 |
-
ai_recommendation_display = gr.Markdown(
|
| 548 |
-
"",
|
| 549 |
-
elem_classes=["ai-advice"]
|
| 550 |
-
)
|
| 551 |
-
|
| 552 |
-
# Proactive advice (may or may not show)
|
| 553 |
-
proactive_display = gr.Markdown(
|
| 554 |
-
"",
|
| 555 |
-
elem_classes=["proactive-advice"]
|
| 556 |
-
)
|
| 557 |
-
|
| 558 |
-
# Decision controls
|
| 559 |
-
gr.Markdown("### Your Decision")
|
| 560 |
-
|
| 561 |
-
with gr.Row():
|
| 562 |
-
decision_radio = gr.Radio(
|
| 563 |
-
choices=["BUY", "HOLD", "SELL"],
|
| 564 |
-
value="HOLD",
|
| 565 |
-
label="Action"
|
| 566 |
-
)
|
| 567 |
-
trade_amount = gr.Slider(
|
| 568 |
-
minimum=1000,
|
| 569 |
-
maximum=50000,
|
| 570 |
-
value=10000,
|
| 571 |
-
step=1000,
|
| 572 |
-
label="Trade Amount (credits)"
|
| 573 |
-
)
|
| 574 |
-
|
| 575 |
-
confidence_slider = gr.Slider(
|
| 576 |
-
minimum=0,
|
| 577 |
-
maximum=100,
|
| 578 |
-
value=50,
|
| 579 |
-
step=5,
|
| 580 |
-
label="How confident are you in this decision?"
|
| 581 |
-
)
|
| 582 |
-
|
| 583 |
-
submit_btn = gr.Button("Submit Decision", variant="primary")
|
| 584 |
-
|
| 585 |
-
outcome_display = gr.Markdown("")
|
| 586 |
-
|
| 587 |
-
# Right column: AI Chat and Controls
|
| 588 |
-
with gr.Column(scale=1):
|
| 589 |
-
gr.Markdown("### AI Advisor Settings")
|
| 590 |
-
|
| 591 |
-
explanation_slider = gr.Slider(
|
| 592 |
-
minimum=0,
|
| 593 |
-
maximum=100,
|
| 594 |
-
value=50,
|
| 595 |
-
step=10,
|
| 596 |
-
label="Explanation Depth",
|
| 597 |
-
info="Minimal ← → Detailed"
|
| 598 |
-
)
|
| 599 |
-
|
| 600 |
-
style_slider = gr.Slider(
|
| 601 |
-
minimum=0,
|
| 602 |
-
maximum=100,
|
| 603 |
-
value=50,
|
| 604 |
-
step=10,
|
| 605 |
-
label="Communication Style",
|
| 606 |
-
info="Formal ← → Casual"
|
| 607 |
-
)
|
| 608 |
-
|
| 609 |
-
update_params_btn = gr.Button("Update AI Settings", size="sm")
|
| 610 |
-
|
| 611 |
-
gr.Markdown("### Ask the AI")
|
| 612 |
-
|
| 613 |
-
chatbot_display = gr.Chatbot(
|
| 614 |
-
label="Chat with AI Advisor",
|
| 615 |
-
height=300
|
| 616 |
-
)
|
| 617 |
-
|
| 618 |
-
chat_input = gr.Textbox(
|
| 619 |
-
placeholder="Ask a question about this scenario...",
|
| 620 |
-
label="Your Question",
|
| 621 |
-
lines=2
|
| 622 |
-
)
|
| 623 |
-
|
| 624 |
-
chat_btn = gr.Button("Send", size="sm")
|
| 625 |
-
|
| 626 |
-
# ==================== RESULTS SCREEN ====================
|
| 627 |
-
with gr.Column(visible=False) as results_section:
|
| 628 |
-
results_display = gr.Markdown("Loading results...")
|
| 629 |
-
|
| 630 |
-
restart_btn = gr.Button("Start New Session", variant="primary")
|
| 631 |
-
|
| 632 |
-
# ==================== EVENT HANDLERS ====================
|
| 633 |
-
|
| 634 |
-
# Start experiment
|
| 635 |
-
start_btn.click(
|
| 636 |
-
fn=start_experiment,
|
| 637 |
-
inputs=[condition_dropdown],
|
| 638 |
-
outputs=[
|
| 639 |
-
welcome_section,
|
| 640 |
-
game_section,
|
| 641 |
-
results_section,
|
| 642 |
-
participant_display,
|
| 643 |
-
portfolio_display,
|
| 644 |
-
progress_display,
|
| 645 |
-
scenario_display,
|
| 646 |
-
ai_recommendation_display,
|
| 647 |
-
proactive_display,
|
| 648 |
-
chatbot_display,
|
| 649 |
-
confidence_slider,
|
| 650 |
-
decision_radio
|
| 651 |
-
]
|
| 652 |
-
)
|
| 653 |
-
|
| 654 |
-
# Update AI parameters
|
| 655 |
-
update_params_btn.click(
|
| 656 |
-
fn=update_ai_params,
|
| 657 |
-
inputs=[explanation_slider, style_slider],
|
| 658 |
-
outputs=[ai_recommendation_display]
|
| 659 |
-
)
|
| 660 |
-
|
| 661 |
-
# Chat interaction
|
| 662 |
-
chat_btn.click(
|
| 663 |
-
fn=handle_chat_query,
|
| 664 |
-
inputs=[chat_input, chatbot_display],
|
| 665 |
-
outputs=[chatbot_display, chat_input]
|
| 666 |
-
)
|
| 667 |
-
|
| 668 |
-
chat_input.submit(
|
| 669 |
-
fn=handle_chat_query,
|
| 670 |
-
inputs=[chat_input, chatbot_display],
|
| 671 |
-
outputs=[chatbot_display, chat_input]
|
| 672 |
-
)
|
| 673 |
-
|
| 674 |
-
# Submit decision
|
| 675 |
-
submit_btn.click(
|
| 676 |
-
fn=submit_decision,
|
| 677 |
-
inputs=[decision_radio, confidence_slider, trade_amount],
|
| 678 |
-
outputs=[
|
| 679 |
-
game_section,
|
| 680 |
-
results_section,
|
| 681 |
-
portfolio_display,
|
| 682 |
-
progress_display,
|
| 683 |
-
scenario_display,
|
| 684 |
-
ai_recommendation_display,
|
| 685 |
-
proactive_display,
|
| 686 |
-
chatbot_display,
|
| 687 |
-
confidence_slider,
|
| 688 |
-
decision_radio,
|
| 689 |
-
outcome_display
|
| 690 |
-
]
|
| 691 |
-
)
|
| 692 |
-
|
| 693 |
-
# Restart
|
| 694 |
-
restart_btn.click(
|
| 695 |
-
fn=lambda: (gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)),
|
| 696 |
-
outputs=[welcome_section, game_section, results_section]
|
| 697 |
-
)
|
| 698 |
-
|
| 699 |
-
return app
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
# Launch the application
|
| 703 |
if __name__ == "__main__":
|
| 704 |
-
|
| 705 |
-
app.launch(
|
| 706 |
-
server_name="0.0.0.0",
|
| 707 |
-
server_port=7860,
|
| 708 |
-
share=False,
|
| 709 |
-
show_api=False
|
| 710 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
client = InferenceClient(token=os.getenv("HF_TOKEN"))
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
def chat(message, history):
|
| 8 |
+
response = client.chat_completion(
|
| 9 |
+
model="HuggingFaceH4/zephyr-7b-beta",
|
| 10 |
+
messages=[{"role": "user", "content": message}],
|
| 11 |
+
max_tokens=256
|
| 12 |
)
|
| 13 |
+
return response.choices[0].message.content
|
| 14 |
|
| 15 |
+
demo = gr.ChatInterface(fn=chat,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