adarshajay commited on
Commit
f774908
·
verified ·
1 Parent(s): 59ce9fb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ from dotenv import loadenv
4
+ import os
5
+ loadenv()
6
+
7
+
8
+ # API Setup
9
+ API_URL = os.getenv("Rapidapi")
10
+ HEADERS = {
11
+ "x-rapidapi-key": "3d2abfca23msh8639b0310323a0ep126a03jsn3335ff1fb1d9",
12
+ "x-rapidapi-host": "youtube138.p.rapidapi.com"
13
+ }
14
+
15
+ # Core function
16
+ def search_youtube(query: str) -> str:
17
+ params = {"q": query, "hl": "en", "gl": "US"}
18
+ try:
19
+ response = requests.get(API_URL, headers=HEADERS, params=params)
20
+ response.raise_for_status()
21
+ data = response.json()
22
+
23
+ # Extract top 5 video titles + links
24
+ videos = data.get("contents", [])
25
+ results = []
26
+ for video in videos:
27
+ if "video" in video:
28
+ v = video["video"]
29
+ title = v.get("title", "")
30
+ video_id = v.get("videoId", "")
31
+ link = f"https://www.youtube.com/watch?v={video_id}"
32
+ results.append(f"{title}\n{link}\n")
33
+ if len(results) >= 5:
34
+ break
35
+
36
+ return "\n".join(results) if results else "No videos found."
37
+ except Exception as e:
38
+ return f"Error: {str(e)}"
39
+
40
+ # Gradio Interface
41
+ demo = gr.Interface(
42
+ fn=search_youtube,
43
+ inputs=gr.Textbox(placeholder="Enter search term...", label="YouTube Query"),
44
+ outputs=gr.Textbox(label="Top 5 Results"),
45
+ title="YouTube Search Tool",
46
+ description="Search YouTube videos using the YouTube138 API (via RapidAPI)"
47
+ )
48
+
49
+ # MCP-compatible launch
50
+ if __name__ == "__main__":
51
+ demo.launch(mcp_server=True)