File size: 3,839 Bytes
a030e94
 
 
 
 
 
 
 
 
 
335b99f
a030e94
 
 
 
 
 
 
 
 
 
 
97d69f2
a030e94
97d69f2
a030e94
97d69f2
a030e94
 
 
 
 
 
 
 
 
97d69f2
a030e94
 
 
 
0c670bd
97d69f2
a030e94
 
97d69f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a030e94
97d69f2
a030e94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83d9da4
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
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)