File size: 4,809 Bytes
c0da02c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from groq import Groq
from dotenv import load_dotenv

load_dotenv()

client = Groq(api_key=os.getenv("GROQ_API_KEY"))

def call_groq(prompt):
    """Helper function to call Groq API safely"""
    try:
        response = client.chat.completions.create(
            model="llama-3.1-8b-instant",
            messages=[{"role": "user", "content": prompt}],
            temperature=0.3,
            max_tokens=1500
        )
        
        if response and response.choices and len(response.choices) > 0:
            content = response.choices[0].message.content
            if content:
                return content
        
        return "Could not generate content. Please try again."
    
    except Exception as e:
        print(f"Groq API error: {str(e)}")
        return f"Error generating content: {str(e)}"


def generate_notes(full_text):
    """Takes transcribed text and generates organized notes"""
    
    print("πŸ“ Generating notes...")
    
    if len(full_text) > 3000:
        full_text = full_text[:3000] + "..."
    
    prompt = f"""
    You are an expert note taker. Read the following transcription and create:
    
    1. A brief SUMMARY (3-5 sentences)
    2. ORGANIZED NOTES in bullet points
    3. KEY TAKEAWAYS (max 5 points)
    
    Format EXACTLY like this:
    
    ## πŸ“‹ SUMMARY
    (write summary here)
    
    ## πŸ“ ORGANIZED NOTES
    (write bullet points here)
    
    ## πŸ’‘ KEY TAKEAWAYS
    (write takeaways here)
    
    Transcription:
    {full_text}
    """
    
    result = call_groq(prompt)
    print(f"πŸ“ Notes generated: {len(result)} characters")
    return result


def generate_timestamps(segments):
    """Creates important timestamps from segments"""
    
    print("⏱️ Generating timestamps...")
    
    if not segments or len(segments) == 0:
        return "## ⏱️ IMPORTANT TIMESTAMPS\n- No timestamp data available for this video."
    
    segments_text = ""
    for seg in segments[:30]:
        start_time = int(seg['start'])
        minutes = start_time // 60
        seconds = start_time % 60
        time_label = f"{minutes:02d}:{seconds:02d}"
        segments_text += f"[{time_label}] {seg['text']}\n"
    
    if not segments_text.strip():
        return "## ⏱️ IMPORTANT TIMESTAMPS\n- No timestamp data available."
    
    prompt = f"""
    You are given a video transcription with timestamps.
    Identify the TOP 8 most important moments.
    
    Format EXACTLY like this:
    ## ⏱️ IMPORTANT TIMESTAMPS
    - [MM:SS] - Description
    - [MM:SS] - Description
    
    Timestamped transcription:
    {segments_text}
    """
    
    result = call_groq(prompt)
    print(f"⏱️ Timestamps generated: {len(result)} characters")
    return result


def generate_action_items(full_text):
    """Extracts action items from transcription"""
    
    print("βœ… Generating action items...")
    
    if len(full_text) > 3000:
        full_text = full_text[:3000] + "..."
    
    prompt = f"""
    Read this transcription and extract ACTION ITEMS or TASKS.
    If none mentioned, suggest relevant actions for the viewer.
    
    Format EXACTLY like this:
    ## βœ… ACTION ITEMS
    - [ ] Action item 1
    - [ ] Action item 2
    - [ ] Action item 3
    
    Transcription:
    {full_text}
    """
    
    result = call_groq(prompt)
    print(f"βœ… Action items generated: {len(result)} characters")
    return result


def process_transcription(transcription):
    """Main function - takes transcription dict and returns all notes"""
    
    print(f"πŸ” Received transcription type: {type(transcription)}")
    print(f"πŸ” Transcription keys: {transcription.keys() if transcription else 'NONE'}")
    print(f"πŸ” Full text length: {len(transcription.get('full_text', '')) if transcription else 0}")
    
    full_text = transcription.get("full_text", "") if transcription else ""
    segments = transcription.get("segments", []) if transcription else []
    
    if not full_text:
        raise Exception("Transcription is empty! Please try another video.")
    
    notes = generate_notes(full_text)
    timestamps = generate_timestamps(segments)
    action_items = generate_action_items(full_text)
    
    # Make sure nothing is None
    final_notes = notes if notes and len(notes) > 5 else "## πŸ“ Notes\nCould not generate notes. Please try again."
    final_timestamps = timestamps if timestamps and len(timestamps) > 5 else "## ⏱️ Timestamps\nNo timestamps available."
    final_actions = action_items if action_items and len(action_items) > 5 else "## βœ… Action Items\nNo action items found."
    
    print("πŸŽ‰ All content generated successfully!")
    
    return {
        "notes": final_notes,
        "timestamps": final_timestamps,
        "action_items": final_actions
    }