Refactor notebook to script with modularized code.
Converted the football game narration notebook into a Python script with clear modular sections. Improved readability by adding cell markers and organizing imports, while maintaining functionality for parsing game data and generating narrations.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "31d3c4a4-5442-4074-b812-42d60e0a0c04",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
@@ -9,15 +10,15 @@
|
||||
"start_time": "2025-04-26T11:54:29.192394Z"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# In this example we read a footbal (soccer) game stat and we create a narration about the game as we are running a podcast\n",
|
||||
"# use this website as an example: https://understat.com/match/27683"
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": 3
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "cf45e9d5-4913-416c-9880-5be60a96c0e6",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
@@ -25,6 +26,7 @@
|
||||
"start_time": "2025-04-26T11:54:30.215752Z"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"import requests\n",
|
||||
@@ -32,12 +34,11 @@
|
||||
"from IPython.display import Markdown, display\n",
|
||||
"from bs4 import BeautifulSoup\n",
|
||||
"from openai import OpenAI"
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": 4
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "af8fea69-60aa-430c-a16c-8757b487e07a",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
@@ -45,6 +46,7 @@
|
||||
"start_time": "2025-04-26T11:54:31.214154Z"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"load_dotenv(override=True)\n",
|
||||
"api_key = os.getenv('OPENAI_API_KEY')\n",
|
||||
@@ -61,20 +63,11 @@
|
||||
" \"An API key was found, but it looks like it might have space or tab characters at the start or end - please remove them - see troubleshooting notebook\")\n",
|
||||
"else:\n",
|
||||
" print(\"API key found and looks good so far!\")"
|
||||
],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"API key found and looks good so far!\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"execution_count": 5
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "daee94d2-f82b-43f0-95d1-15370eda1bc7",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
@@ -82,18 +75,18 @@
|
||||
"start_time": "2025-04-26T11:54:32.183600Z"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"openai = OpenAI()\n",
|
||||
"url = \"https://understat.com/match/27683\"\n",
|
||||
"\n",
|
||||
"# If this doesn't work, try Kernel menu >> Restart Kernel and Clear Outputs Of All Cells, then run the cells from the top of this notebook down.\n",
|
||||
"# If it STILL doesn't work (horrors!) then please see the Troubleshooting notebook in this folder for full instructions"
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": 6
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "0712dd1d-b6bc-41c6-84ec-d965f696f7aa",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
@@ -101,18 +94,18 @@
|
||||
"start_time": "2025-04-26T11:54:33.023289Z"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"system_prompt = (\"You are a football (soccer) analyst. Yuo are used to read stats of football \\\n",
|
||||
" games and extract relevant information. You are asked to be a podcast host and \\\n",
|
||||
" you need to create a narration of the game based on the stats you read and based \\\n",
|
||||
" on the play by play moves (the one with minutes upfront). You're talking to the \\\n",
|
||||
" general audience so try to use a easy language and do not be too much telegraphic\")"
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": 7
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "70c972a6-8af6-4ff2-a338-6d7ba90e2045",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
@@ -120,6 +113,7 @@
|
||||
"start_time": "2025-04-26T11:54:33.725360Z"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Some websites need you to use proper headers when fetching them:\n",
|
||||
"headers = {\n",
|
||||
@@ -138,18 +132,19 @@
|
||||
" for irrelevant in soup.body([\"script\", \"style\", \"img\", \"input\"]):\n",
|
||||
" irrelevant.decompose()\n",
|
||||
" self.text = soup.body.get_text(separator=\"\\n\", strip=True)\n"
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": 8
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "4ccc1ba81c76ffb9",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-04-26T11:54:40.042357Z",
|
||||
"start_time": "2025-04-26T11:54:40.040384Z"
|
||||
}
|
||||
},
|
||||
"cell_type": "code",
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def create_user_prompt(game):\n",
|
||||
" user_prompt = f\"You are looking at {game.title} football game\"\n",
|
||||
@@ -158,26 +153,24 @@
|
||||
" Focus only on what happened on the game and the stats and ignore all the standings and anything else.\\n\\n\"\n",
|
||||
" user_prompt += game.text\n",
|
||||
" return user_prompt\n"
|
||||
],
|
||||
"id": "4ccc1ba81c76ffb9",
|
||||
"outputs": [],
|
||||
"execution_count": 9
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e729956758b4d7b5",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-04-26T11:54:40.699042Z",
|
||||
"start_time": "2025-04-26T11:54:40.696698Z"
|
||||
}
|
||||
},
|
||||
"cell_type": "code",
|
||||
"source": "",
|
||||
"id": "e729956758b4d7b5",
|
||||
"outputs": [],
|
||||
"execution_count": null
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "82b71c1a-895a-48e7-a945-13e615bb0096",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
@@ -185,6 +178,7 @@
|
||||
"start_time": "2025-04-26T11:54:41.314110Z"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Define messages with system_prompt and user_prompt\n",
|
||||
"def messages_for(system_prompt_input, user_prompt_input):\n",
|
||||
@@ -192,12 +186,11 @@
|
||||
" {\"role\": \"system\", \"content\": system_prompt_input},\n",
|
||||
" {\"role\": \"user\", \"content\": user_prompt_input}\n",
|
||||
" ]"
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": 10
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "854dc42e-2bbd-493b-958f-c20484908300",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
@@ -205,6 +198,7 @@
|
||||
"start_time": "2025-04-26T11:54:41.987168Z"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# And now: call the OpenAI API.\n",
|
||||
"game = Website(url)\n",
|
||||
@@ -216,20 +210,7 @@
|
||||
"\n",
|
||||
"# Response is provided in Markdown and displayed accordingly\n",
|
||||
"display(Markdown(response.choices[0].message.content))"
|
||||
],
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
],
|
||||
"text/markdown": "# Cagliari vs. Fiorentina - Match Recap (April 23, 2025)\n\nIn an exciting Serie A match, Cagliari faced off against Fiorentina at home, with the game concluding in a 2-1 victory for the visitors. This match was filled with drama and crucial moments that kept fans on the edge of their seats.\n\n### First Half Highlights\n\nThe game kicked off with both teams striving for early dominance. It didn’t take long for Cagliari to make their mark, as they opened the scoring in just the 6th minute. **Roberto Piccoli** found the back of the net, sending the home crowd into a frenzy; Cagliari led 1-0 early.\n\nHowever, Fiorentina responded with great intensity. After some exchanges, they found an equalizer in the 35th minute through **Robin Gosens**, who struck to level the game at 1-1. The first half concluded with both teams at a stalemate, but Fiorentina carried a slight advantage in possession and shots on target.\n\n### Second Half Thrills\n\nAs the second half commenced, Fiorentina took control, creating more opportunities. Their efforts paid off just two minutes after the break when **Lucas Beltrán** scored in the 47th minute, putting Fiorentina ahead 2-1. This goal shifted the momentum firmly in favor of the visitors.\n\nCagliari attempted to regroup, making substitutions and pushing forward to find an equalizer. They had some chances but struggled to fully penetrate Fiorentina's defense. The home side managed to compile a total of 8 shots but could only register 2 on target, while Fiorentina matched their shots but managed a more clinical performance with 4 on target.\n\n### Key Statistics\n\n- **Goals:** \n - Cagliari: 1 \n - Fiorentina: 2 \n\n- **Expected Goals (xG):** \n - Cagliari: 0.37 \n - Fiorentina: 0.70 \n\n- **Shots:** \n - Cagliari: 8 \n - Fiorentina: 8 \n\n- **Shots on Target:** \n - Cagliari: 2 \n - Fiorentina: 4 \n\n- **Possession and Pressure:** \n - Cagliari had a PPDA (Passes Per Defensive Action) of 12.41, while Fiorentina managed a tighter 11.89. This suggests that Fiorentina was more effective in breaking down Cagliari's defensive structure.\n\n### Conclusion\n\nUltimately, the match concluded with Fiorentina taking all three points with a determined display. Despite a valiant effort from Cagliari, their inability to capitalize on chances proved costly. With xPTS (expected points) showing a reflective scoreline, Cagliari's final total fell short of their expectations at 0.93, while Fiorentina exceeded their xPTS with a solid 1.63.\n\nIt was a match marked by tactical battles, and while Cagliari may feel disappointed, Fiorentina celebrated a crucial win that could boost their ambitions for the remainder of the season."
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"execution_count": 11
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -256,7 +237,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.11"
|
||||
"version": "3.11.12"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
Reference in New Issue
Block a user