File size: 2,546 Bytes
76001c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3669d22
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
import gradio as gr
import whisper
import json
import re
import os


def transcribe_video(file_path):
    result = model.transcribe(file_path, verbose=False)
    return result['text'], result['segments']

def extract_highlights(transcript_segments, user_query, max_segments=100):
    transcript_text = "\n".join([
        f"[{int(seg['start'])} --> {int(seg['end'])}] {seg['text']}"
        for seg in transcript_segments[:max_segments]
    ])

    prompt = f"""
You are a JSON-only assistant.

Return only valid JSON in the following format:

{{
  "segments": [
    {{ "start": 52, "end": 58 }},
    {{ "start": 108, "end": 110 }}
  ]
}}

No markdown, no explanation, no wrapping. Just plain JSON.
User query: {user_query}
Transcript:
{transcript_text}
"""

    completion = client.chat.completions.create(
        model="llama3-70b-8192",
        messages=[{"role": "user", "content": prompt}],
        temperature=1,
        max_tokens=1024,
        top_p=1,
        stream=True,
        stop=None,
    )

    result = ""
    for chunk in completion:
        result += chunk.choices[0].delta.content or ""

    try:
        return json.loads(result.strip())
    except json.JSONDecodeError:
        raise ValueError("Failed to parse LLM output:\n" + result)

def process_video(video_file, user_query):
    steps = []
    steps.append("🎬 Uploaded video.")

    video_path = video_file
    _, transcript_segments = transcribe_video(video_path)

    steps.append("🧠 Extracting highlights...")
    results = extract_highlights(transcript_segments, user_query)

    segments = [(max(0, s["start"] - 5), s["end"] + 5) for s in results["segments"]]
    video = mp.VideoFileClip(video_path)
    clips = [video.subclip(start, end) for start, end in segments]
    final = mp.concatenate_videoclips(clips)

    output_path = "highlight_output.mp4"
    steps.append("🎞️ Rendering...")
    final.write_videofile(output_path, codec="libx264", audio_codec="aac", verbose=False, logger=None)

    steps.append("✅ Done!")
    return "\n".join(steps), output_path

with gr.Blocks() as demo:
    gr.Markdown("# ⚽ Momentify – Sports Highlight Generator")
    with gr.Row():
        video_input = gr.Video(label="Upload Match Video (.mp4)")
        query_input = gr.Textbox(label="Highlight Query", placeholder="e.g. Messi goals")
    output_text = gr.Markdown()
    output_video = gr.Video()
    btn = gr.Button("Generate Highlights")
    btn.click(process_video, [video_input, query_input], [output_text, output_video])

demo.launch(share=True)