Spaces:
Sleeping
Sleeping
Update core/analyze.py
Browse files- core/analyze.py +8 -19
core/analyze.py
CHANGED
|
@@ -2,7 +2,7 @@ import os
|
|
| 2 |
import time
|
| 3 |
import json
|
| 4 |
import logging
|
| 5 |
-
from
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
|
| 8 |
load_dotenv()
|
|
@@ -11,19 +11,15 @@ load_dotenv()
|
|
| 11 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
| 14 |
-
# Configure
|
| 15 |
-
api_key = os.getenv("
|
| 16 |
-
MODEL_NAME = os.getenv("
|
| 17 |
|
| 18 |
-
|
| 19 |
-
client = OpenAI(
|
| 20 |
-
base_url="https://openrouter.ai/api/v1",
|
| 21 |
-
api_key=api_key
|
| 22 |
-
)
|
| 23 |
|
| 24 |
|
| 25 |
def analyze_transcript(transcript):
|
| 26 |
-
"""Analyze transcript using
|
| 27 |
|
| 28 |
prompt = f"""
|
| 29 |
You are an expert video editor and viral content strategist.
|
|
@@ -68,7 +64,7 @@ def analyze_transcript(transcript):
|
|
| 68 |
|
| 69 |
max_retries = 3
|
| 70 |
base_delay = 5
|
| 71 |
-
content = None
|
| 72 |
|
| 73 |
for attempt in range(max_retries):
|
| 74 |
try:
|
|
@@ -78,10 +74,6 @@ def analyze_transcript(transcript):
|
|
| 78 |
{"role": "system", "content": "You are a helpful assistant that outputs only valid JSON."},
|
| 79 |
{"role": "user", "content": prompt}
|
| 80 |
],
|
| 81 |
-
extra_headers={
|
| 82 |
-
"HTTP-Referer": "https://github.com/Start-To-End-AI",
|
| 83 |
-
"X-Title": "Video Clipper AI",
|
| 84 |
-
},
|
| 85 |
temperature=0.7,
|
| 86 |
)
|
| 87 |
|
|
@@ -102,7 +94,7 @@ def analyze_transcript(transcript):
|
|
| 102 |
return {"content": content}
|
| 103 |
|
| 104 |
except Exception as e:
|
| 105 |
-
print(f"❌ Error in
|
| 106 |
if attempt < max_retries - 1:
|
| 107 |
wait_time = base_delay * (2 ** attempt)
|
| 108 |
print(f"⚠️ Retrying task in {wait_time}s...")
|
|
@@ -114,14 +106,11 @@ def analyze_transcript(transcript):
|
|
| 114 |
return {"content": '{"segments": []}'}
|
| 115 |
|
| 116 |
|
| 117 |
-
# Smart chunking system for long transcripts
|
| 118 |
def smart_chunk_transcript(transcript, max_tokens=4000):
|
| 119 |
"""
|
| 120 |
Split transcript into coherent chunks at sentence boundaries
|
| 121 |
while preserving context and meaning.
|
| 122 |
"""
|
| 123 |
-
import json
|
| 124 |
-
# Simple sentence-based chunking
|
| 125 |
sentences = transcript.replace('\n', ' ').split('. ')
|
| 126 |
chunks = []
|
| 127 |
current_chunk = []
|
|
|
|
| 2 |
import time
|
| 3 |
import json
|
| 4 |
import logging
|
| 5 |
+
from groq import Groq
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
|
| 8 |
load_dotenv()
|
|
|
|
| 11 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
| 14 |
+
# Configure Groq Client
|
| 15 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 16 |
+
MODEL_NAME = os.getenv("GROQ_MODEL", "llama-3.1-8b-instant")
|
| 17 |
|
| 18 |
+
client = Groq(api_key=api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
def analyze_transcript(transcript):
|
| 22 |
+
"""Analyze transcript using Groq API."""
|
| 23 |
|
| 24 |
prompt = f"""
|
| 25 |
You are an expert video editor and viral content strategist.
|
|
|
|
| 64 |
|
| 65 |
max_retries = 3
|
| 66 |
base_delay = 5
|
| 67 |
+
content = None
|
| 68 |
|
| 69 |
for attempt in range(max_retries):
|
| 70 |
try:
|
|
|
|
| 74 |
{"role": "system", "content": "You are a helpful assistant that outputs only valid JSON."},
|
| 75 |
{"role": "user", "content": prompt}
|
| 76 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
temperature=0.7,
|
| 78 |
)
|
| 79 |
|
|
|
|
| 94 |
return {"content": content}
|
| 95 |
|
| 96 |
except Exception as e:
|
| 97 |
+
print(f"❌ Error in Groq analysis: {e}")
|
| 98 |
if attempt < max_retries - 1:
|
| 99 |
wait_time = base_delay * (2 ** attempt)
|
| 100 |
print(f"⚠️ Retrying task in {wait_time}s...")
|
|
|
|
| 106 |
return {"content": '{"segments": []}'}
|
| 107 |
|
| 108 |
|
|
|
|
| 109 |
def smart_chunk_transcript(transcript, max_tokens=4000):
|
| 110 |
"""
|
| 111 |
Split transcript into coherent chunks at sentence boundaries
|
| 112 |
while preserving context and meaning.
|
| 113 |
"""
|
|
|
|
|
|
|
| 114 |
sentences = transcript.replace('\n', ' ').split('. ')
|
| 115 |
chunks = []
|
| 116 |
current_chunk = []
|