Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,26 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
|
|
|
|
|
|
|
|
3 |
from dotenv import load_dotenv
|
4 |
-
import os
|
5 |
-
load_dotenv()
|
6 |
|
|
|
7 |
|
8 |
-
#
|
9 |
API_URL = "https://youtube138.p.rapidapi.com/search/"
|
10 |
HEADERS = {
|
11 |
"x-rapidapi-key": os.getenv("Rapidapi"),
|
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:
|
@@ -32,20 +32,62 @@ def search_youtube(query: str) -> str:
|
|
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 |
-
#
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
)
|
48 |
|
49 |
-
# MCP-compatible launch
|
50 |
if __name__ == "__main__":
|
51 |
demo.launch(mcp_server=True)
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
+
import numpy as np
|
4 |
+
import os
|
5 |
+
from pathlib import Path
|
6 |
+
from PIL import Image
|
7 |
from dotenv import load_dotenv
|
|
|
|
|
8 |
|
9 |
+
load_dotenv()
|
10 |
|
11 |
+
# YouTube Tool
|
12 |
API_URL = "https://youtube138.p.rapidapi.com/search/"
|
13 |
HEADERS = {
|
14 |
"x-rapidapi-key": os.getenv("Rapidapi"),
|
15 |
"x-rapidapi-host": "youtube138.p.rapidapi.com"
|
16 |
}
|
17 |
|
|
|
18 |
def search_youtube(query: str) -> str:
|
19 |
params = {"q": query, "hl": "en", "gl": "US"}
|
20 |
try:
|
21 |
response = requests.get(API_URL, headers=HEADERS, params=params)
|
22 |
response.raise_for_status()
|
23 |
data = response.json()
|
|
|
|
|
24 |
videos = data.get("contents", [])
|
25 |
results = []
|
26 |
for video in videos:
|
|
|
32 |
results.append(f"{title}\n{link}\n")
|
33 |
if len(results) >= 5:
|
34 |
break
|
|
|
35 |
return "\n".join(results) if results else "No videos found."
|
36 |
except Exception as e:
|
37 |
return f"Error: {str(e)}"
|
38 |
|
39 |
+
# Other Tools
|
40 |
+
def prime_factors(n):
|
41 |
+
n = int(n)
|
42 |
+
if n <= 1:
|
43 |
+
raise ValueError("Input must be an integer greater than 1.")
|
44 |
+
factors = []
|
45 |
+
while n % 2 == 0:
|
46 |
+
factors.append(2)
|
47 |
+
n //= 2
|
48 |
+
divisor = 3
|
49 |
+
while divisor * divisor <= n:
|
50 |
+
while n % divisor == 0:
|
51 |
+
factors.append(divisor)
|
52 |
+
n //= divisor
|
53 |
+
divisor += 2
|
54 |
+
if n > 1:
|
55 |
+
factors.append(n)
|
56 |
+
return factors
|
57 |
+
|
58 |
+
def generate_cheetah_image():
|
59 |
+
return Path(os.path.dirname(__file__)) / "cheetah.jpg"
|
60 |
+
|
61 |
+
def image_orientation(image: Image.Image) -> str:
|
62 |
+
return "Portrait" if image.height > image.width else "Landscape"
|
63 |
+
|
64 |
+
def sepia(input_img):
|
65 |
+
sepia_filter = np.array([
|
66 |
+
[0.393, 0.769, 0.189],
|
67 |
+
[0.349, 0.686, 0.168],
|
68 |
+
[0.272, 0.534, 0.131]
|
69 |
+
])
|
70 |
+
sepia_img = np.array(input_img).dot(sepia_filter.T)
|
71 |
+
sepia_img /= sepia_img.max()
|
72 |
+
return sepia_img
|
73 |
+
|
74 |
+
# Tabbed Tool Interface
|
75 |
+
demo = gr.TabbedInterface(
|
76 |
+
[
|
77 |
+
gr.Interface(search_youtube, gr.Textbox(label="YouTube Query"), gr.Textbox(label="Top 5 Results"), api_name="youtube_search"),
|
78 |
+
gr.Interface(prime_factors, gr.Textbox(), gr.Textbox(), api_name="prime_factors"),
|
79 |
+
gr.Interface(generate_cheetah_image, None, gr.Image(), api_name="generate_cheetah_image"),
|
80 |
+
gr.Interface(image_orientation, gr.Image(type="pil"), gr.Textbox(), api_name="image_orientation"),
|
81 |
+
gr.Interface(sepia, gr.Image(), gr.Image(), api_name="sepia"),
|
82 |
+
],
|
83 |
+
[
|
84 |
+
"YouTube Search",
|
85 |
+
"Prime Factors",
|
86 |
+
"Cheetah Image",
|
87 |
+
"Image Orientation Checker",
|
88 |
+
"Sepia Filter",
|
89 |
+
]
|
90 |
)
|
91 |
|
|
|
92 |
if __name__ == "__main__":
|
93 |
demo.launch(mcp_server=True)
|