RacingDemo / src /models /catchphrase_extractor.py
Vlad Bastina
merge
10877f8
import os
import google.generativeai as genai
import time
def extract_catchphrase(video_path: str):
# Set your API key
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
# Load the model (Gemini 1.5 Flash is currently accessible via the multimodal endpoint)
model = genai.GenerativeModel(model_name="models/gemini-2.5-flash")
# Upload video as a part of a multi-turn prompt
upload_response = genai.upload_file(path=video_path, mime_type="video/mp4")
file_id = upload_response.name
while True:
status = genai.get_file(file_id)
if status.state == 2:
break
print(f"Waiting for file to become ACTIVE... Current state: {status.state}")
time.sleep(1)
prompt = '''
**Role:** You are an expert sports commentary analyst with a keen ear for identifying impactful and memorable short phrases ("catchphrases") that describe a race's outcome or a competitor's performance, particularly at the moment of or immediately following a win.
**Task:**
Analyze the provided race video's audio commentary (or a provided transcript) and extract a list of significant 2-3 word catchphrases that highlight key aspects of the win or the winner's achievement.
**Inputs:**
1. **Race Video/Audio:** The video will be provided in the prompt and the audio track will be present in the video.
*OR*
2. **Commentary Transcript:** [User will provide a text transcript of the race commentary.]
3. **(Optional) Approximate Timestamp of Win:** [User may provide an approximate time for when the win occurs to help you focus your search, e.g., "around the 2:15 mark"].
**Process & Reasoning (Think Step-by-Step):**
1. **Transcribe (if necessary):** If audio is provided, accurately transcribe the commentary.
2. **Identify Key Moments:** Listen for/read through the commentary, paying close attention to:
* The period leading up to the finish line.
* The moment the winner crosses the finish line.
* The immediate aftermath of the win.
3. **Filter for "Winning" Context:** Focus on segments of commentary that directly describe the act of winning, the winner, or the nature of their victory.
4. **Define "Significant Catchphrase":** Look for phrases that are:
* **Concise:** Ideally 2-3 words in length.
* **Impactful/Descriptive:** Use strong adjectives or highlight a notable achievement (e.g., "debut," "first," "commanding," "dominant," "incredible," "historic").
* **Memorable/Quotable:** Sound like a good "soundbite" from the race.
* **Unique or Emphatic:** Stand out from more generic commentary.
5. **Examples of Desired Catchphrases:**
* "Debut winner!"
* "First win for him!"
* "Commanding win!"
* "Dominant performance!"
* "Simply unstoppable!"
* "Picture perfect!"
* "Record breaking!"
6. **Examples of Phrases to AVOID (too generic or not catchphrases):**
* "Crosses the line"
* "Good race"
* "He is the winner" (unless part of a more emphatic phrase like "And he is the winner, by a mile!")
* "And the crowd goes wild"
* "Impressive"
* "Away he goes"
* Anything related to the way the race was won, about the distance to the second place
* Simple name mentions without descriptive context.
7. **Extract and Timestamp:** For each identified catchphrase, provide the phrase itself and the approximate timestamp (HH:MM:SS or MM:SS) of when it was spoken in the commentary.
8. **Prioritize & Limit:** If many potential phrases are found, prioritize the most impactful and descriptive ones. Aim to provide the top 3-5 distinct catchphrases unless otherwise specified.
**Output Requirements:**
* A numbered list of the extracted catchphrases.
* Each item in the list should be in the format: `"Catchphrase" - Timestamp (e.g., MM:SS)`
**Example Output:**
"Debut winner!" - 02:31
"Commanding performance!" - 02:33
"Absolutely dominant!" - 02:35
**Important Considerations for the AI:**
* **Commentary Style:** Some commentators are more prone to catchphrases than others. If the commentary is very straightforward, fewer distinct catchphrases might be available.
* **Accuracy:** Strive for accurate transcription and phrase extraction.
* **Context is Key:** Ensure the phrase is genuinely related to the significance of the win and not just a general comment.
'''
# Use the uploaded video in a prompt
prompt_parts = [
prompt,
upload_response # This is a File object
]
response = model.generate_content(prompt_parts)
return response.text