Skip to content

Commit

Permalink
Enhances upload process
Browse files Browse the repository at this point in the history
  • Loading branch information
asantacreu committed Feb 5, 2025
1 parent 50d3aff commit a9c4341
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 43 deletions.
51 changes: 17 additions & 34 deletions application.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ def upload_hub():
"thumbnail": thumbnail,
"zone_code": zone_code,
"sector_code": sector_code,
"schedule_info": schedule_info
"schedule_info": schedule_info,
"is_short": is_short
}
return render_template('upload-hub.html', **context)

Expand Down Expand Up @@ -423,35 +424,17 @@ def compute_upload_metadata():
return jsonify({'error': str(e)}), 500


@app.route('/test-schedule')
@app.route('/test-schedule', methods=['GET'])
def test_schedule():
"""Test endpoint to check scheduling recommendation without uploading"""
try:
file_id = request.args.get('file_id')
if file_id:
session['file_id'] = file_id # Save file_id in session for redirection
else:
file_id = session.get('file_id') # Use stored file_id if available

if not file_id:
return "Error: No file ID provided.", 400

isAuthenticated = utils.channel.is_authenticated()
if not isAuthenticated:
return render_template('upload-hub.html', authenticated=False, file_id=file_id)

metadata = utils.drive.getFileMetadata(file_id)
if not metadata:
return "Error fetching metadata from Google Drive.", 500
# Get parameters from the request
is_short = request.args.get('is_short', type=bool)
name = request.args.get('name', '')
climber = request.args.get('climber', 'Unknown Climber')
grade = request.args.get('grade', '')
zone = request.args.get('zone', 'Unknown Zone')

properties = metadata.get('properties', {})
climber = properties.get('climber', 'Unknown Climber')
name = properties.get('name', 'Unknown Problem')
grade = properties.get('grade', '')
zone = properties.get('zone', 'Unknown Zone')

is_short = is_video_short(file_id)

# Get AI recommendation
schedule_info = suggest_upload_time(is_short, name, climber, grade, zone)
if schedule_info and schedule_info.get('success'):
Expand Down Expand Up @@ -534,15 +517,15 @@ def process_upload_youtube_from_local():
if error:
return error

# Move the file to uploaded videos folder on success
uploaded_path = Path(app.config['UPLOADED_VIDEOS_FOLDER']) / video_filename
try:
uploaded_path.parent.mkdir(parents=True, exist_ok=True)
video_path.rename(uploaded_path)
except Exception as move_error:
print(f"Warning: Could not move file to done folder: {move_error}")
# Move the file to uploaded videos folder on success
uploaded_path = Path(app.config['UPLOADED_VIDEOS_FOLDER']) / video_filename
try:
uploaded_path.parent.mkdir(parents=True, exist_ok=True)
video_path.rename(uploaded_path)
except Exception as move_error:
print(f"Warning: Could not move file to done folder: {move_error}")

return channel_uploader.generate_success_page(response['id'], title, publish_time)
return channel_uploader.generate_success_page(response['id'], title, publish_time)

except Exception as e:
print(f"Error in process_upload_youtube_from_local: {e}")
Expand Down
28 changes: 23 additions & 5 deletions templates/local-upload-hub.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ <h2>Upload Information</h2>

<label for="scheduled_time">Scheduled Time:</label>
<input type="text" id="scheduled_time" name="scheduled_time"><br><br>

<!-- Move the button and schedule info here -->
<button id="checkSchedule" class="btn btn-secondary" type="button">Refresh Schedule Recommendation</button>
<div id="scheduleInfo" class="schedule-info" style="display: block;">
<div id="scheduleContent"></div>
</div>
</div>

<button type="submit" id="uploadVideoButton" style="display: none;">Upload Video</button>
Expand Down Expand Up @@ -111,8 +117,14 @@ <h2>Enter Video Details</h2>
scheduleInfo.style.display = 'none';
scheduleInfo.className = 'schedule-info';

const isShort = window.isShortVideo || false; // Assuming this variable is set elsewhere
const name = document.getElementById('modalProblem').value; // Get the problem name
const climber = document.getElementById('modalClimber').value; // Get the climber name
const grade = document.getElementById('modalGrade').value; // Get the grade
const zone = document.getElementById('modalZone').value; // Get the zone

try {
const response = await fetch('/test-schedule');
const response = await fetch(`/test-schedule?is_short=${isShort}&name=${encodeURIComponent(name)}&climber=${encodeURIComponent(climber)}&grade=${encodeURIComponent(grade)}&zone=${encodeURIComponent(zone)}`);
const result = await response.json();

if (result.success) {
Expand Down Expand Up @@ -258,31 +270,37 @@ <h2>Enter Video Details</h2>
if(data.error){
alert("Error computing metadata: " + data.error);
} else {
// Set fields with computed values
document.getElementById('description').value = data.description;
document.getElementById('tags').value = data.tags;
if(data.schedule_info && data.schedule_info.success){
document.getElementById('scheduled_time').value = data.schedule_info.scheduled_time;

const scheduleContent = document.getElementById('scheduleContent');
const scheduledTime = data.schedule_info.scheduled_time;
const reasoning = data.schedule_info.reasoning;

document.getElementById('scheduled_time').value = scheduledTime;
scheduleContent.innerHTML = formatScheduleInfo(scheduledTime, reasoning);
} else {
document.getElementById('scheduled_time').value = "";
}

// Show the upload information container now that data is ready
document.getElementById('uploadInformation').style.display = 'block';
// Show the upload video button now that the video is loaded and metadata computed
document.getElementById('uploadVideoButton').style.display = 'block';
}
})
.catch(err => { console.error(err); });

// Hide the modal popup
document.getElementById('metadataModal').style.display = 'none';
});

// Allow closing the modal with the "x" button.
document.getElementById('closeModal').addEventListener('click', function(){
document.getElementById('metadataModal').style.display = 'none';
});

// Add event listener for the refresh button
document.getElementById('checkSchedule').addEventListener('click', checkSchedule);
</script>
</body>
</html>
8 changes: 6 additions & 2 deletions templates/upload-hub.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,17 @@ <h1>MadBoulder Upload Hub</h1>
scheduleInfo.style.display = 'none';
scheduleInfo.className = 'schedule-info';

const name = document.getElementById('title').value; // Get the title
const climber = document.getElementById('climber').value; // Get the climber name
const grade = document.getElementById('grade').value; // Get the grade
const zone = document.getElementById('zone').value; // Get the zone

try {
const response = await fetch('/test-schedule');
const response = await fetch(`/test-schedule?is_short=${is_short}&name=${encodeURIComponent(name)}&climber=${encodeURIComponent(climber)}&grade=${encodeURIComponent(grade)}&zone=${encodeURIComponent(zone)}`);
const result = await response.json();

if (result.success) {
document.getElementById('scheduled_time').value = result.scheduled_time;

scheduleInfo.className = 'schedule-info schedule-success';
scheduleInfo.innerHTML = formatScheduleInfo(
result.scheduled_time,
Expand Down
4 changes: 2 additions & 2 deletions utils/ai_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ class GenerativeAI:
def __init__(self):
# Configure Gemini
genai.configure(api_key=os.environ['GEMINI_API_KEY'])
self.model = genai.GenerativeModel('gemini-pro')
self.model = genai.GenerativeModel('gemini-1.5-flash')

def get_schedule_recommendation(self, video_data, scheduled_videos):
"""Get scheduling recommendation for a video"""
try:
# Get the complete prompt from SchedulingPrompts
prompt = SchedulingPrompts.get_scheduling_prompt(video_data, scheduled_videos)

print(prompt)
# Get response from AI
response = self.model.generate_content(prompt)

Expand Down
3 changes: 3 additions & 0 deletions utils/channel_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ def generate_success_page(video_id, title, publish_time=None):
success_html += f"""
<p>Continue the edition in Youtube Studio:</p>
<a href="{youtube_url}">{title}</a>
<br><br>
<p>Or add another one:</p>
<a href="/local-upload-hub" class="btn btn-primary">Return to Local Upload Hub</a>
"""

return success_html
Expand Down

0 comments on commit a9c4341

Please sign in to comment.