added video cost to executions
This commit is contained in:
parent
35f213ced9
commit
5be810d606
3 changed files with 54 additions and 6 deletions
7
app.py
7
app.py
|
|
@ -181,6 +181,7 @@ async def run_job(job_id, job_data):
|
|||
|
||||
base_name = os.path.basename(target_json).replace('_metadata.json', '')
|
||||
clips = data.get('shorts', [])
|
||||
cost_analysis = data.get('cost_analysis')
|
||||
|
||||
# Check which clips actually exist on disk
|
||||
ready_clips = []
|
||||
|
|
@ -194,7 +195,7 @@ async def run_job(job_id, job_data):
|
|||
ready_clips.append(clip)
|
||||
|
||||
if ready_clips:
|
||||
jobs[job_id]['result'] = {'clips': ready_clips}
|
||||
jobs[job_id]['result'] = {'clips': ready_clips, 'cost_analysis': cost_analysis}
|
||||
except Exception as e:
|
||||
# Ignore read errors during processing
|
||||
pass
|
||||
|
|
@ -215,11 +216,13 @@ async def run_job(job_id, job_data):
|
|||
# Enhance result with video URLs
|
||||
base_name = os.path.basename(target_json).replace('_metadata.json', '')
|
||||
clips = data.get('shorts', [])
|
||||
cost_analysis = data.get('cost_analysis')
|
||||
|
||||
for i, clip in enumerate(clips):
|
||||
clip_filename = f"{base_name}_clip_{i+1}.mp4"
|
||||
clip['video_url'] = f"/videos/{job_id}/{clip_filename}"
|
||||
|
||||
jobs[job_id]['result'] = {'clips': clips}
|
||||
jobs[job_id]['result'] = {'clips': clips, 'cost_analysis': cost_analysis}
|
||||
else:
|
||||
jobs[job_id]['status'] = 'failed'
|
||||
jobs[job_id]['logs'].append("No metadata file generated.")
|
||||
|
|
|
|||
|
|
@ -499,6 +499,11 @@ function App() {
|
|||
{results.clips.length} Clips
|
||||
</span>
|
||||
)}
|
||||
{results?.cost_analysis && (
|
||||
<span className="text-xs bg-green-500/10 border border-green-500/20 text-green-400 px-2 py-0.5 rounded-full ml-2" title={`Input: ${results.cost_analysis.input_tokens} | Output: ${results.cost_analysis.output_tokens}`}>
|
||||
${results.cost_analysis.total_cost.toFixed(5)}
|
||||
</span>
|
||||
)}
|
||||
</h2>
|
||||
|
||||
<div className="flex-1 overflow-y-auto custom-scrollbar p-1">
|
||||
|
|
|
|||
48
main.py
48
main.py
|
|
@ -454,9 +454,7 @@ def get_viral_clips(transcript_result, video_duration):
|
|||
|
||||
client = genai.Client(api_key=api_key)
|
||||
|
||||
# We use gemini-1.5-flash which is the standard current fast model.
|
||||
# 'gemini-2.5-flash' does not exist in public API as of my knowledge cutoff or might be typo.
|
||||
# Reverting to 'gemini-1.5-flash' to be safe, or use 'gemini-1.5-pro' for better quality.
|
||||
# We use gemini-2.5-flash as requested.
|
||||
model_name = 'gemini-2.5-flash'
|
||||
|
||||
print(f"🤖 Initializing Gemini with model: {model_name}")
|
||||
|
|
@ -482,6 +480,44 @@ def get_viral_clips(transcript_result, video_duration):
|
|||
model=model_name,
|
||||
contents=prompt
|
||||
)
|
||||
|
||||
# --- Cost Calculation ---
|
||||
try:
|
||||
usage = response.usage_metadata
|
||||
if usage:
|
||||
# Gemini 2.5 Flash Pricing (Dec 2025)
|
||||
# Input: $0.10 per 1M tokens
|
||||
# Output: $0.40 per 1M tokens
|
||||
|
||||
input_price_per_million = 0.10
|
||||
output_price_per_million = 0.40
|
||||
|
||||
prompt_tokens = usage.prompt_token_count
|
||||
output_tokens = usage.candidates_token_count
|
||||
|
||||
input_cost = (prompt_tokens / 1_000_000) * input_price_per_million
|
||||
output_cost = (output_tokens / 1_000_000) * output_price_per_million
|
||||
total_cost = input_cost + output_cost
|
||||
|
||||
cost_analysis = {
|
||||
"input_tokens": prompt_tokens,
|
||||
"output_tokens": output_tokens,
|
||||
"input_cost": input_cost,
|
||||
"output_cost": output_cost,
|
||||
"total_cost": total_cost,
|
||||
"model": model_name
|
||||
}
|
||||
|
||||
print(f"💰 Token Usage ({model_name}):")
|
||||
print(f" - Input Tokens: {prompt_tokens} (${input_cost:.6f})")
|
||||
print(f" - Output Tokens: {output_tokens} (${output_cost:.6f})")
|
||||
print(f" - Total Estimated Cost: ${total_cost:.6f}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"⚠️ Could not calculate cost: {e}")
|
||||
cost_analysis = None
|
||||
# ------------------------
|
||||
|
||||
# Clean response if it contains markdown code blocks
|
||||
text = response.text
|
||||
if text.startswith("```json"):
|
||||
|
|
@ -490,7 +526,11 @@ def get_viral_clips(transcript_result, video_duration):
|
|||
text = text[:-3]
|
||||
text = text.strip()
|
||||
|
||||
return json.loads(text)
|
||||
result_json = json.loads(text)
|
||||
if cost_analysis:
|
||||
result_json['cost_analysis'] = cost_analysis
|
||||
|
||||
return result_json
|
||||
except Exception as e:
|
||||
print(f"❌ Gemini Error: {e}")
|
||||
return None
|
||||
|
|
|
|||
Loading…
Reference in a new issue