Spaces:
No application file
No application file
File size: 3,329 Bytes
6b65dee |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
from openai import OpenAI
import base64
from model_player import Player
import json
client = OpenAI()
def get_ai_response(prompt):
response = client.responses.create(
model="gpt-4o",
input=prompt
)
return response.output_text
def player_to_dict(player):
return {
"name": player.name,
"position": player.position,
"shirt_number": player.shirt_number,
"preferred_foot": player.preferred_foot,
"role": player.role,
"form": player.form,
}
prompt = """
You are simulating a fictional soccer match and creating a realistic event timeline from kickoff to final whistle.
Here is the match context:
- Match type: {match_type}
- Location: {location}
- Date: {date}
- Home team: {home_team}
- Away team: {away_team}
- Winner: {winner}
- Score: {score}
Here is the {home_team} roster:
{home_roster}
Here is the {away_team} roster:
{away_roster}
Rules:
- Spread out events across 90+ minutes
- Substitutions should use players not in the starting 11 if available
- Vary event types and timing
Only return a JSON array of chronological match events, formatted exactly like this example:
[
{{
"minute": // string, e.g. 45+2
"team": // home or away team name
"event": // e.g. Goal, Yellow Card, Substitution, etc.
"player": // name + number
"description": // 1-sentence summary
}}
]
"""
# # match 1
# match_type = "semifinal 1"
# location = "El Templo del Sol (fictional stadium in Mérida, Mexico)"
# date = "July 10, 2025"
# home_team = Player.teams[2]
# away_team = Player.teams[3]
# winner = Player.teams[2]
# score = "2–1"
# home_roster = None
# away_roster = None
# match 2
match_type = "semifinal 2"
location = "Everglade Arena (fictional stadium in Miami, Florida)"
date = "July 11, 2025"
home_team = Player.teams[1]
away_team = Player.teams[0]
winner = Player.teams[1]
score = "3-3 (4-2 pens)"
home_roster = None
away_roster = None
home_players = Player.get_players(team=home_team)
away_players = Player.get_players(team=away_team)
home_roster = [player_to_dict(player) for player in home_players]
away_roster = [player_to_dict(player) for player in away_players]
prompt = prompt.format(
match_type=match_type,
location=location,
date=date,
home_team=home_team,
away_team=away_team,
home_roster=home_roster,
away_roster=away_roster,
winner=winner,
score=score,
)
# print(prompt)
response = get_ai_response(prompt)
# Parse the AI response as JSON
try:
if response.startswith("```json") and response.endswith("```"):
response = response[7:-3]
events = json.loads(response)
except Exception as e:
print("Error parsing AI response as JSON:", e)
print("Raw response was:\n", response)
raise
# Pretty-print JSON to terminal
print(json.dumps(events, indent=4, sort_keys=True))
# Write pretty JSON to file
with open(f"/workspace/data/huge-league/games/{match_type.replace(' ', '_')}.json", "w") as f:
json.dump(events, f, indent=4, sort_keys=True)
# Optionally, if you want a more human-readable text format, uncomment below:
# def pretty_print_events(events):
# for event in events:
# print(f"[{event['minute']}] {event['team']} - {event['event']} - {event['player']}: {event['description']}")
# pretty_print_events(events)
|