File size: 15,519 Bytes
9bec329
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41ca786
227cbd0
9bec329
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
"""
World Cup Storyteller β€” Hugging Face Spaces app.py
NLP Homework 4 β€” ARI 525

Three approaches, equal treatment:
- Zero-Shot : prompt only β†’ LLM knowledge
- Few-Shot  : prompt + 2 style examples β†’ LLM knowledge
- RAG       : prompt β†’ retrieve from WC dataset β†’ grounded generation
"""

import os
import time
import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import numpy as np
import gradio as gr
from groq import Groq
from sentence_transformers import SentenceTransformer
import faiss

# -----------------------------------------------
# CONFIG
# -----------------------------------------------

GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "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")
MODEL = "llama-3.1-8b-instant"
client = Groq(api_key=GROQ_API_KEY)

# -----------------------------------------------
# LOAD DATA (used only by RAG)
# -----------------------------------------------

try:
    cups    = pd.read_csv("WorldCups.csv")
    matches = pd.read_csv("WorldCupMatches.csv")
    print("βœ… Dataset loaded.")
except FileNotFoundError as e:
    raise RuntimeError(
        "CSV files not found. Upload WorldCups.csv and WorldCupMatches.csv "
        "to the Space root folder."
    ) from e


# -----------------------------------------------
# DATA CLEANING & CONTEXT BUILDERS
# -----------------------------------------------

def clean_data(cups, matches):
    matches_clean = matches.dropna(
        subset=["Home Team Name", "Away Team Name",
                "Home Team Goals", "Away Team Goals"]
    ).copy()
    matches_clean["Home Team Name"] = matches_clean["Home Team Name"].str.strip()
    matches_clean["Away Team Name"]  = matches_clean["Away Team Name"].str.strip()
    matches_clean["Home Team Goals"] = matches_clean["Home Team Goals"].astype(int)
    matches_clean["Away Team Goals"] = matches_clean["Away Team Goals"].astype(int)
    cups_clean = cups.dropna(subset=["Year", "Winner"]).copy()
    cups_clean["Year"] = cups_clean["Year"].astype(int)
    return cups_clean, matches_clean


def build_team_context(team, year, cups, matches):
    cup_info = cups[cups["Year"] == year]
    if cup_info.empty:
        return None
    cup = cup_info.iloc[0]
    team_matches = matches[
        (matches["Year"] == year) &
        ((matches["Home Team Name"] == team) | (matches["Away Team Name"] == team))
    ].sort_values("Stage")
    if team_matches.empty:
        return None

    match_lines = []
    for _, row in team_matches.iterrows():
        home, away = row["Home Team Name"], row["Away Team Name"]
        hg, ag = int(row["Home Team Goals"]), int(row["Away Team Goals"])
        if home == team:
            result = "WIN" if hg > ag else ("DRAW" if hg == ag else "LOSS")
            line = f"[{row['Stage']}] {team} vs {away}: {hg}-{ag} ({result})"
        else:
            result = "WIN" if ag > hg else ("DRAW" if hg == ag else "LOSS")
            line = f"[{row['Stage']}] {team} vs {home}: {ag}-{hg} ({result})"
        match_lines.append(line)

    final_note = ""
    if cup.get("Winner") == team:
        final_note = f"{team} WON the {year} World Cup! πŸ†"
    elif cup.get("Runners-Up") == team:
        final_note = f"{team} were runners-up in {year}."
    elif cup.get("Third") == team:
        final_note = f"{team} finished third in {year}."

    return (
        f"{team}'s Journey β€” World Cup {year}\n"
        f"Host: {cup.get('Country', 'Unknown')}\n"
        f"{final_note}\n\nMatch-by-match results:\n" +
        "\n".join(match_lines)
    )


def build_edition_context(year, cups, matches):
    cup_info = cups[cups["Year"] == year]
    if cup_info.empty:
        return None
    cup = cup_info.iloc[0]
    edition_matches = matches[matches["Year"] == year].sort_values("Stage")
    match_lines = [
        f"[{row['Stage']}] {row['Home Team Name']} {int(row['Home Team Goals'])} "
        f"- {int(row['Away Team Goals'])} {row['Away Team Name']}"
        for _, row in edition_matches.iterrows()
    ]
    return (
        f"World Cup {year} β€” Host: {cup.get('Country', 'Unknown')}\n"
        f"Winner: {cup.get('Winner', 'Unknown')}\n"
        f"Runners-up: {cup.get('Runners-Up', 'Unknown')}\n"
        f"Third Place: {cup.get('Third', 'Unknown')}\n"
        f"Goals Scored: {cup.get('GoalsScored', 'Unknown')}\n"
        f"Attendance: {cup.get('Attendance', 'Unknown')}\n\nAll Matches:\n" +
        "\n".join(match_lines)
    )


cups_clean, matches_clean = clean_data(cups, matches)


# -----------------------------------------------
# RAG KNOWLEDGE BASE
# -----------------------------------------------

def build_knowledge_base(cups, matches):
    chunks, metadata = [], []
    for _, cup_row in cups.iterrows():
        year = int(cup_row["Year"])
        year_matches = matches[matches["Year"] == year]
        teams = set(
            year_matches["Home Team Name"].tolist() +
            year_matches["Away Team Name"].tolist()
        )
        for team in teams:
            ctx = build_team_context(team, year, cups, matches)
            if ctx:
                chunks.append(ctx)
                metadata.append({"team": team, "year": year})
        ctx = build_edition_context(year, cups, matches)
        if ctx:
            chunks.append(ctx)
            metadata.append({"team": "ALL", "year": year})
    return chunks, metadata


print("⏳ Loading embedding model...")
embedder = SentenceTransformer("all-MiniLM-L6-v2")

print("⏳ Building & embedding knowledge base...")
kb_chunks, kb_metadata = build_knowledge_base(cups_clean, matches_clean)
kb_embeddings = embedder.encode(kb_chunks, show_progress_bar=False, convert_to_numpy=True)

dim = kb_embeddings.shape[1]
faiss_index = faiss.IndexFlatL2(dim)
faiss_index.add(kb_embeddings)
print(f"βœ… RAG index ready β€” {faiss_index.ntotal} vectors")


def retrieve_context(query, top_k=3):
    query_vec = embedder.encode([query], convert_to_numpy=True)
    distances, indices = faiss_index.search(query_vec, top_k)
    retrieved = []
    for idx in indices[0]:
        if idx < len(kb_chunks):
            meta = kb_metadata[idx]
            retrieved.append(f"[Retrieved: {meta['team']} β€” {meta['year']}]\n{kb_chunks[idx]}")
    return "\n\n---\n\n".join(retrieved)


# -----------------------------------------------
# PROMPTS & FEW-SHOT EXAMPLES
# -----------------------------------------------

BASE_SYSTEM_PROMPT = """
You are a passionate, knowledgeable sports journalist specializing in FIFA World Cup history.
Generate vivid, engaging, narrative-driven stories about World Cup tournaments and team journeys.

Guidelines:
- Write in flowing narrative prose β€” no bullet points
- Build tension and highlight dramatic moments
- Be factually accurate based on your knowledge
- Adapt tone to the user's request (documentary, dramatic, casual, etc.)
- Keep stories between 250-400 words unless asked otherwise
""".strip()

RAG_SYSTEM_PROMPT = """
You are a passionate sports journalist specializing in FIFA World Cup history.
You will be given retrieved historical match data. Use it to write an accurate,
vivid, narrative-driven story. Only use facts present in the retrieved data.

Guidelines:
- Write in flowing narrative prose β€” no bullet points
- Build tension and highlight dramatic moments
- Stick strictly to the facts in the retrieved data β€” do not invent scores
- Adapt tone to the user's request
- Keep stories between 250-400 words unless asked otherwise
""".strip()

FEW_SHOT_EXAMPLES = [
    {
        "prompt": "Tell the story of France's 1998 World Cup victory.",
        "story": (
            "It was the summer that France found its destiny on home soil. Les Bleus entered "
            "the 1998 World Cup as hosts carrying immense pressure, but from the very first "
            "whistle they played with a quiet, relentless authority.\n\n"
            "The group stage was a statement β€” South Africa swept aside 3-0, Saudi Arabia "
            "dismantled 4-0, Denmark edged out 2-1. By the knockout rounds, France carried "
            "the weight of a nation's expectations into every match.\n\n"
            "Paraguay made them suffer β€” a lone golden goal was all that separated the sides. "
            "Italy pushed them to penalties, a nerve-shredding duel France survived with "
            "ice-cold nerves. Then came Croatia. France trailed before turning the game on "
            "its head to win 2-1.\n\n"
            "The final against defending champions Brazil became a coronation. Three goals, "
            "zero reply. The Stade de France erupted, and a generation of French children "
            "discovered what it felt like to be champions of the world."
        )
    },
    {
        "prompt": "Tell the story of West Germany's 1954 World Cup win.",
        "story": (
            "They called it the Miracle of Bern, and for good reason. No one believed West "
            "Germany could win the 1954 World Cup β€” least of all after Hungary handed them "
            "an 8-3 humiliation in the group stage.\n\n"
            "But West Germany, crafty and resilient, had rested key players for that match "
            "and quietly plotted their path forward. They dispatched Turkey twice, squeezed "
            "past Yugoslavia, then demolished Austria 6-1 in a dazzling semi-final.\n\n"
            "The final was a rematch nobody expected. Hungary β€” the Mighty Magyars, unbeaten "
            "for four years β€” led 2-0 within eight minutes. The world assumed it was over.\n\n"
            "It was not. West Germany clawed back to 2-2, and then six minutes from the end "
            "Helmut Rahn struck. 3-2. A country still rebuilding from the rubble of war had "
            "become world champions."
        )
    }
]


# -----------------------------------------------
# GENERATION FUNCTIONS
# -----------------------------------------------

def generate_zeroshot(user_prompt):
    start = time.time()
    response = client.chat.completions.create(
        model=MODEL,
        messages=[
            {"role": "system", "content": BASE_SYSTEM_PROMPT},
            {"role": "user",   "content": user_prompt}
        ],
        temperature=0.8,
        max_tokens=600
    )
    elapsed = round(time.time() - start, 2)
    return response.choices[0].message.content.strip(), elapsed


def generate_fewshot(user_prompt):
    messages = [{"role": "system", "content": BASE_SYSTEM_PROMPT}]
    for ex in FEW_SHOT_EXAMPLES:
        messages.append({"role": "user",      "content": ex["prompt"]})
        messages.append({"role": "assistant", "content": ex["story"]})
    messages.append({"role": "user", "content": user_prompt})
    start = time.time()
    response = client.chat.completions.create(
        model=MODEL, messages=messages, temperature=0.8, max_tokens=600
    )
    elapsed = round(time.time() - start, 2)
    return response.choices[0].message.content.strip(), elapsed


def generate_rag(user_prompt, top_k=3):
    retrieved = retrieve_context(user_prompt, top_k=top_k)
    full_message = (
        f"Retrieved World Cup data:\n---\n{retrieved}\n---\n\n"
        f"User request: {user_prompt}"
    )
    start = time.time()
    response = client.chat.completions.create(
        model=MODEL,
        messages=[
            {"role": "system", "content": RAG_SYSTEM_PROMPT},
            {"role": "user",   "content": full_message}
        ],
        temperature=0.8,
        max_tokens=600
    )
    elapsed = round(time.time() - start, 2)
    return response.choices[0].message.content.strip(), elapsed


# -----------------------------------------------
# GRADIO UI
# -----------------------------------------------

def generate_story_ui(user_prompt, approach):
    if not user_prompt.strip():
        return "⚠️ Please enter a prompt.", ""
    try:
        if approach == "Zero-Shot":
            story, elapsed = generate_zeroshot(user_prompt)
            info = f"⚑ Zero-Shot (LLM knowledge only) | ⏱️ {elapsed}s | πŸ“ {len(story.split())} words"
        elif approach == "Few-Shot":
            story, elapsed = generate_fewshot(user_prompt)
            info = f"πŸ“– Few-Shot (LLM knowledge + 2 style examples) | ⏱️ {elapsed}s | πŸ“ {len(story.split())} words"
        else:
            story, elapsed = generate_rag(user_prompt)
            info = f"πŸ” RAG (retrieved from WC dataset) | ⏱️ {elapsed}s | πŸ“ {len(story.split())} words"
        return story, info
    except Exception as e:
        return f"❌ Error: {str(e)}", ""


EXAMPLES = [
    "Tell me the story of Brazil's 2002 World Cup campaign like a sports documentary.",
    "What happened in the 1966 World Cup? Who won and how?",
    "Describe Argentina's 1986 World Cup triumph β€” focus on the drama.",
    "Tell Italy's 2006 World Cup story.",
    "What was the most dramatic World Cup final ever?",
]

with gr.Blocks(
    title="⚽ World Cup Storyteller",
    theme=gr.themes.Base(),
    css="""
        #header { text-align: center; padding: 1.5em 0 0.5em; }
        #header h1 { font-size: 2.2em; }
        #header p  { color: #777; font-size: 1.05em; }
        #story-box textarea { font-size: 1.05em; line-height: 1.8; }
        .note { font-size: 0.85em; color: #777; margin-top: 0.4em; line-height: 1.6; }
    """
) as demo:

    with gr.Column(elem_id="header"):
        gr.Markdown("# ⚽ World Cup Storyteller")
        gr.Markdown(
            "Ask anything about World Cup history β€” get a vivid narrative story. "
            "Compare how different NLP approaches handle the same prompt."
        )

    with gr.Row():

        with gr.Column(scale=1, min_width=260):
            gr.Markdown("### βš™οΈ NLP Approach")
            approach = gr.Radio(
                choices=["Zero-Shot", "Few-Shot", "RAG"],
                value="Few-Shot",
                label=""
            )
            gr.Markdown(
                "**Zero-Shot** β€” Prompt only. No examples, no data. "
                "The LLM relies entirely on its own knowledge.\n\n"
                "**Few-Shot** β€” 2 hand-crafted story examples guide the writing style. "
                "Still uses LLM knowledge, no dataset.\n\n"
                "**RAG** β€” Retrieves real match data from the WC dataset (1930–2014) "
                "before generating. Most factually grounded.",
                elem_classes="note"
            )

        with gr.Column(scale=2):
            gr.Markdown("### ✍️ Your Prompt")
            user_prompt = gr.Textbox(
                placeholder="e.g. Tell me the story of Brazil's 2002 World Cup campaign...",
                label="Ask anything about World Cup history",
                lines=3
            )
            gr.Examples(
                examples=[[p] for p in EXAMPLES],
                inputs=user_prompt,
                label="Try an example"
            )
            btn = gr.Button("πŸŽ™οΈ Generate Story", variant="primary", size="lg")

            gr.Markdown("### πŸ“– Story")
            story_out = gr.Textbox(
                label="", lines=16, interactive=False, elem_id="story-box"
            )
            info_out = gr.Markdown("")

    btn.click(
        fn=generate_story_ui,
        inputs=[user_prompt, approach],
        outputs=[story_out, info_out]
    )

    gr.Markdown(
        "---\n"
        "*Data: FIFA World Cup dataset (1930–2014) Β· "
        "LLM: Llama 3 8B via Groq Β· "
        "Embeddings: all-MiniLM-L6-v2 Β· "
        "Built for NLP HW4 β€” ARI 525*"
    )

demo.launch()