Yt_Playlist_app / app.py
Thanush1's picture
Update app.py
335b99f verified
import os
import sys
import gradio as gr
import subprocess
import json
import os
import re
from typing import Tuple, Dict, Any
from playlist_details import get_youtube_playlist_videos
# Ensure Playwright & browsers are installed
try:
subprocess.run([sys.executable, "-m", "playwright", "install", "chromium"], check=True)
print("Playwright browsers installed")
except Exception as e:
print(f"Playwright install failed: {e}")
# Import custom modules (ensure they are in the working directory)
from scraper import scrape_playlists
from aws_llm import llm_response
def process_course(course_name: str, advanced_options: Dict[str, Any] = None) -> dict:
print(f"[process_course] Called with course_name: {course_name}")
try:
print("[process_course] Scraping playlists...")
data = scrape_playlists(course_name, headless=True)
print(f"[process_course] Scraped data: {json.dumps(data, indent=2)[:1000]}...")
playlist_infos = [
{
"title": pl["title"],
"url": pl["full_playlist_url"],
"views": pl["views"],
"year": pl["year"]
}
for pl in data["playlists"]
]
print(f"[process_course] playlist_infos: {playlist_infos}")
system_prompt = "You are an expert YouTube playlist selector."
user_prompt = (
f"Given the following YouTube playlists for {course_name}, "
"choose the best playlist that is in English, has the highest views, "
"and is the most recent. Only consider playlists where the title and metadata suggest the content is in English."
"Return ONLY the best playlist's URL as plain text, with no extra text, no JSON, no explanation.\n\n"
f"Playlists:\n{json.dumps(playlist_infos, indent=2)}"
)
print(f"[process_course] system_prompt: {system_prompt}")
print(f"[process_course] user_prompt: {user_prompt}")
best_playlist_url, _ = llm_response(system_prompt, user_prompt)
print(f"[process_course] LLM raw output: {best_playlist_url}")
# Extract playlist URL from LLM response (robustly)
url_match = re.search(r'(https?://[\w./?=&%-]+)', best_playlist_url)
if url_match:
playlist_url = url_match.group(1)
print(f"[process_course] Extracted playlist_url: {playlist_url}")
# Call your detailed playlist function
result = get_youtube_playlist_videos(playlist_url)
print(f"[process_course] get_youtube_playlist_videos result: {result}")
return result
print("[process_course] Could not extract playlist URL from LLM output.")
return {"error": "Could not extract playlist URL", "llm_output": best_playlist_url}
except Exception as e:
print(f"[process_course] Exception: {e}")
return {"error": str(e)}
def create_interface():
with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {max-width: 800px; margin: auto;} .output-markdown {white-space: pre-wrap;}") as app:
gr.Markdown("# YouTube Course Playlist Finder")
gr.Markdown("Search YouTube playlists Agent")
course = gr.Textbox(label="Course Name", placeholder="e.g., Python programming", value="Python programming")
search = gr.Button("Search", variant="primary")
with gr.Tabs():
with gr.TabItem("Best Playlist"):
best_json = gr.JSON(label="AI Selected Playlist")
search.click(fn=process_course, inputs=[course], outputs=[best_json])
gr.Examples(examples=["python programming", "machine learning", "react js tutorial"], inputs=course)
return app
if __name__ == "__main__":
app = create_interface()
app.launch(server_name="0.0.0.0", server_port=8080,share=True)