File size: 56,318 Bytes
15b5142 cf686ab 15b5142 cf686ab 15b5142 cf686ab 15b5142 cf686ab 15b5142 32a19cc 15b5142 | 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 | import gradio as gr
import sqlite3
import json
import requests
from datetime import datetime, timedelta, timezone
from typing import List, Dict, Any, Optional
import google.generativeai as genai
from googleapiclient.discovery import build
import pandas as pd
import re
from collections import defaultdict
import base64
# Do NOT auto-load API keys from environment; keys should be provided by the user via the UI.
# Keep variables here so other functions can reference them after the user provides keys.
YOUTUBE_API_KEY: Optional[str] = None
GEMINI_API_KEY: Optional[str] = None
model = None
youtube = None
def set_api_keys(youtube_key: Optional[str], gemini_key: Optional[str]) -> tuple[str, str, str]:
"""Apply API keys provided by the user at runtime.
This will configure the Gemini client and the YouTube Data API client
so the rest of the app uses the provided keys instead of environment vars.
"""
global YOUTUBE_API_KEY, GEMINI_API_KEY, model, youtube
messages = []
# Configure Gemini (Generative AI)
if gemini_key:
try:
genai.configure(api_key=gemini_key)
model = genai.GenerativeModel('gemini-2.5-flash')
GEMINI_API_KEY = gemini_key
messages.append("Gemini API key applied successfully.")
except Exception as e:
messages.append(f"Failed to apply Gemini API key: {e}")
# Configure YouTube Data API
if youtube_key:
try:
youtube = build('youtube', 'v3', developerKey=youtube_key)
YOUTUBE_API_KEY = youtube_key
messages.append("YouTube API key applied successfully.")
except Exception as e:
messages.append(f"Failed to apply YouTube API key: {e}")
if not messages:
# Return status and empty keys
return "No API keys provided.", "", ""
# Return status plus the applied keys so the UI can store them in state
return "\n".join(messages), YOUTUBE_API_KEY or "", GEMINI_API_KEY or ""
class YouTubeCompetitorAnalyzer:
def __init__(self):
self.init_database()
def init_database(self):
"""Initialize the database"""
conn = sqlite3.connect('competitor_data.db')
cursor = conn.cursor()
# Channel table (added last_updated_at column)
cursor.execute('''
CREATE TABLE IF NOT EXISTS channels (
id INTEGER PRIMARY KEY AUTOINCREMENT,
channel_id TEXT UNIQUE,
channel_name TEXT,
channel_icon_url TEXT,
subscriber_count INTEGER,
added_date TEXT,
last_updated_at TEXT
)
''')
# Video data table (added description and tags)
cursor.execute('''
CREATE TABLE IF NOT EXISTS videos (
id INTEGER PRIMARY KEY AUTOINCREMENT,
video_id TEXT UNIQUE,
channel_id TEXT,
title TEXT,
description TEXT,
tags TEXT,
published_at TEXT,
view_count INTEGER,
thumbnail_url TEXT,
detected_person TEXT,
detection_source TEXT,
importance_level TEXT,
created_at TEXT
)
''')
# Trend clusters table
cursor.execute('''
CREATE TABLE IF NOT EXISTS trends (
id INTEGER PRIMARY KEY AUTOINCREMENT,
person_name TEXT,
video_ids TEXT,
trend_date TEXT,
is_active BOOLEAN
)
''')
conn.commit()
conn.close()
def extract_person_from_title_with_gemini(self, title: str) -> Optional[str]:
"""Extract a person's name from the title using Gemini (global, highest priority)"""
if not model:
return None
try:
prompt = f"""
Please extract a single famous person's name (historical or contemporary) from this YouTube title.
Title: "{title}"
Target:
Globally well-known individuals (no restriction on nationality, era, or field)
- People from any country or region worldwide
- From ancient to modern times
- Any field: politics, business, philosophy, literature, science, arts, religion, sports, etc.
- Real historical or contemporary figures
Criteria:
- Widely known at a general-knowledge level
- Frequently mentioned in books, education, or media
- Identifiable as a specific real person by proper name
Response format:
- If a matching person exists: return the person's name only (in Japanese)
- If none: return "ใชใ"
- If multiple apply: return the single most relevant person
Note: Do not restrict by nationality, era, or field. Consider notable people worldwide.
Examples:
"The secret of innovation by Steve Jobs" -> Steve Jobs
"Learning leadership from Confucius" -> Confucius
"Introduction to Einstein's theory of relativity" -> Einstein
"Konosuke Matsushita on business philosophy" -> Konosuke Matsushita
"General success tips" -> none
"""
response = model.generate_content(prompt)
result = response.text.strip()
# If result is "ใชใ" or empty, return None
if not result or result.lower() in ['ใชใ', 'none', '่ฉฒๅฝใชใ', 'ไธๆ']:
return None
# Remove line breaks and extra characters to isolate the person's name
clean_result = re.sub(r'[ใใใใใใ๏ฝ\n\r\t]', '', result).strip()
# Check global name pattern (2-15 chars: supports Japanese, English, Chinese, etc.)
global_name_pattern = r'^[\u4E00-\u9FAF\u3040-\u309F\u30A0-\u30FF\uAC00-\uD7AF\u0041-\u005A\u0061-\u007A\u00C0-\u017F\u0100-\u024F\s\u30FB\u00B7\u2022]{2,15}$'
if re.match(global_name_pattern, clean_result):
return clean_result
else:
return None
except Exception as e:
print(f"Gemini global title parsing error: {e}")
return None
def extract_person_from_description_with_gemini(self, description: str) -> Optional[str]:
if not description or len(description.strip()) < 10 or not model:
return None
try:
# If the description is too long, limit to the first 500 characters
desc_excerpt = description[:500] if len(description) > 500 else description
prompt = f"""
Please extract a single famous person's name (historical or contemporary) from this YouTube video's description.
Description excerpt: "{desc_excerpt}"
Target:
Globally well-known individuals (no restriction on nationality, era, or field)
Criteria:
- Widely known at a general-knowledge level
- Frequently mentioned in books, education, or media
- Identifiable as a specific real person by proper name
Response format:
- If a matching person exists: return the person's name only (in Japanese)
- If none: return "ใชใ"
- If multiple apply: return the single most relevant person
- Hashtags (e.g., #SteveJobs, #Confucius) should also be considered
Note: Do not restrict by nationality, era, or field. Consider notable people worldwide.
"""
response = model.generate_content(prompt)
result = response.text.strip()
if not result or result.lower() in ['ใชใ', 'none', '่ฉฒๅฝใชใ', 'ไธๆ']:
return None
clean_result = re.sub(r'[ใใใใใใ๏ฝ#\n\r\t]', '', result).strip()
# Check global name pattern
global_name_pattern = r'^[\u4E00-\u9FAF\u3040-\u309F\u30A0-\u30FF\uAC00-\uD7AF\u0041-\u005A\u0061-\u007A\u00C0-\u017F\u0100-\u024F\s\u30FB\u00B7\u2022]{2,15}$'
if re.match(global_name_pattern, clean_result):
return clean_result
else:
return None
except Exception as e:
print(f"Gemini global description parsing error: {e}")
return None
def extract_person_from_tags(self, tags: List[str]) -> Optional[str]:
"""Extract a person's name from tags (global, priority 3)"""
if not tags:
return None
# Define global name pattern (supports Japanese, English, Chinese, Korean, etc.)
global_name_pattern = r'^[\u4E00-\u9FAF\u3040-\u309F\u30A0-\u30FF\uAC00-\uD7AF\u0041-\u005A\u0061-\u007A\u00C0-\u017F\u0100-\u024F\s\u30FB\u00B7\u2022]{2,15}$'
# ใฟใฐใใไบบ็ฉๅใใใใใฎใๆขใ
for tag in tags:
if re.match(global_name_pattern, tag):
# Exclude overly generic words (global support)
exclude_words = [
'ๅ็ป', 'ๆ็จฟ', 'ๆดๆฐ', '้
ไฟก', 'ไบบ็', '็ตๅถ', 'ไปไบ', 'ๆๅ', 'ๅคฑๆ',
'video', 'life', 'business', 'success', 'leadership', 'philosophy',
'motivation', 'inspiration', 'education', 'training', 'coach'
]
if tag not in exclude_words and tag.lower() not in [word.lower() for word in exclude_words]:
return tag
return None
def analyze_thumbnail_ocr(self, thumbnail_url: str) -> Optional[str]:
"""Thumbnail OCR analysis (priority 4)"""
if not model:
return None
try:
response = requests.get(thumbnail_url, timeout=10)
image_data = base64.b64encode(response.content).decode()
prompt = """
Extract text from this YouTube thumbnail image.
Pay special attention to names of famous individuals (worldwide, historical or modern).
Reply in JSON using the following format:
{
"detected_text": "All text read by OCR",
"person_names": ["List of extracted person names"]
}
"""
image_part = {
"mime_type": "image/jpeg",
"data": image_data
}
response = model.generate_content([prompt, image_part])
result_text = response.text
# Extract JSON
json_match = re.search(r'```json\n(.*?)\n```', result_text, re.DOTALL)
if json_match:
result_text = json_match.group(1)
try:
result = json.loads(result_text)
person_names = result.get('person_names', [])
return person_names[0] if person_names else None
except json.JSONDecodeError:
return None
except Exception as e:
print(f"Thumbnail OCR analysis error: {e}")
return None
def analyze_thumbnail_face_recognition(self, thumbnail_url: str) -> Optional[str]:
"""Thumbnail face recognition (priority 5)"""
if not model:
return None
try:
response = requests.get(thumbnail_url, timeout=10)
image_data = base64.b64encode(response.content).decode()
prompt = """
Identify the person shown in this image.
Consider famous people worldwide, including historical figures, philosophers, business leaders, writers, and scientists.
Only return a person's name if you can identify them with confidence.
If unknown, return null.
Respond in JSON:
{
"person_name": "Identified person name or null"
}
"""
image_part = {
"mime_type": "image/jpeg",
"data": image_data
}
response = model.generate_content([prompt, image_part])
result_text = response.text
json_match = re.search(r'```json\n(.*?)\n```', result_text, re.DOTALL)
if json_match:
result_text = json_match.group(1)
try:
result = json.loads(result_text)
return result.get('person_name')
except json.JSONDecodeError:
return None
except Exception as e:
print(f"Face recognition analysis error: {e}")
return None
def extract_person_comprehensive(self, video_data: Dict) -> tuple[Optional[str], str]:
"""Comprehensive person extraction (Gemini prioritized, global system)"""
title = video_data.get('title', '')
description = video_data.get('description', '')
tags = video_data.get('tags', [])
thumbnail_url = video_data.get('thumbnail_url', '')
# Priority 1: Gemini title analysis (global, highest priority)
person = self.extract_person_from_title_with_gemini(title)
if person:
return person, "Gemini-GlobalTitle"
# Priority 2: Gemini description analysis (global)
person = self.extract_person_from_description_with_gemini(description)
if person:
return person, "Gemini-GlobalDescription"
# Priority 3: Tag analysis (global)
person = self.extract_person_from_tags(tags)
if person:
return person, "GlobalTag"
# Priority 4: Thumbnail OCR
person = self.analyze_thumbnail_ocr(thumbnail_url)
if person:
return person, "ThumbnailOCR"
# Priority 5: Face recognition
person = self.analyze_thumbnail_face_recognition(thumbnail_url)
if person:
return person, "FaceRecognition"
return None, "Not detected"
def add_channel(self, channel_id: str) -> str:
"""Add a channel"""
if not youtube:
return "YouTube API key is not set."
try:
# Retrieve channel info
response = youtube.channels().list(
part='snippet,statistics',
id=channel_id
).execute()
if not response['items']:
return f"ID: {channel_id} - Channel not found"
channel_info = response['items'][0]
channel_name = channel_info['snippet']['title']
channel_icon = channel_info['snippet']['thumbnails']['default']['url']
subscriber_count = int(channel_info['statistics'].get('subscriberCount', 0))
conn = sqlite3.connect('competitor_data.db')
cursor = conn.cursor()
cursor.execute('''
INSERT OR REPLACE INTO channels
(channel_id, channel_name, channel_icon_url, subscriber_count, added_date)
VALUES (?, ?, ?, ?, ?)
''', (channel_id, channel_name, channel_icon, subscriber_count, datetime.now().isoformat()))
conn.commit()
conn.close()
return f"Channel '{channel_name}' added"
except Exception as e:
return f"ID: {channel_id} - Error: {str(e)}"
def delete_channel(self, channel_id: str) -> str:
"""Delete a channel"""
try:
conn = sqlite3.connect('competitor_data.db')
cursor = conn.cursor()
# Retrieve channel name
cursor.execute('SELECT channel_name FROM channels WHERE channel_id = ?', (channel_id,))
result = cursor.fetchone()
if not result:
conn.close()
return "Channel not found"
channel_name = result[0]
# Delete channel and related video data
cursor.execute('DELETE FROM videos WHERE channel_id = ?', (channel_id,))
cursor.execute('DELETE FROM channels WHERE channel_id = ?', (channel_id,))
conn.commit()
conn.close()
return f"Channel '{channel_name}' deleted"
except Exception as e:
return f"Deletion error: {str(e)}"
def update_channel_name(self, channel_id: str, new_name: str) -> str:
"""Update channel name"""
try:
conn = sqlite3.connect('competitor_data.db')
cursor = conn.cursor()
cursor.execute('''
UPDATE channels
SET channel_name = ?
WHERE channel_id = ?
''', (new_name, channel_id))
if cursor.rowcount == 0:
conn.close()
return "Channel not found"
conn.commit()
conn.close()
return f"Channel name updated to '{new_name}'"
except Exception as e:
return f"Update error: {str(e)}"
def get_channels(self) -> List[Dict]:
"""Get list of registered channels"""
conn = sqlite3.connect('competitor_data.db')
cursor = conn.cursor()
cursor.execute('''
SELECT channel_id, channel_name, channel_icon_url, subscriber_count, added_date, last_updated_at
FROM channels
ORDER BY added_date DESC
''')
channels = []
for row in cursor.fetchall():
channels.append({
'id': row[0],
'name': row[1],
'icon_url': row[2],
'subscriber_count': row[3],
'added_date': row[4],
'last_updated_at': row[5]
})
conn.close()
return channels
def fetch_videos_from_channel(self, channel_id: str, since_date: Optional[str] = None) -> List[Dict]:
"""Fetch videos from a channel since the specified date"""
if not youtube:
return []
try:
# since_dateใใชใใใฐใ้ๅป7ๆฅ้ใซ่จญๅฎ
if not since_date:
since_date_dt = datetime.now(timezone.utc) - timedelta(days=7)
else:
since_date_dt = datetime.fromisoformat(since_date)
# YouTube APIใฎใใฉใผใใใใซๅคๆ
published_after = since_date_dt.isoformat().replace('+00:00', 'Z')
response = youtube.search().list(
part='snippet',
channelId=channel_id,
maxResults=50,
order='date',
publishedAfter=published_after,
type='video'
).execute()
videos = []
video_ids = [item['id']['videoId'] for item in response['items']]
# ๅ็ปใฎ่ฉณ็ดฐๆ
ๅ ฑ๏ผๅ็ๅๆฐใๆฆ่ฆๆฌใใฟใฐใชใฉ๏ผใๅๅพ
if video_ids:
video_details = youtube.videos().list(
part='statistics,snippet',
id=','.join(video_ids)
).execute()
for item in video_details['items']:
videos.append({
'video_id': item['id'],
'title': item['snippet']['title'],
'description': item['snippet'].get('description', ''),
'tags': item['snippet'].get('tags', []),
'published_at': item['snippet']['publishedAt'],
'view_count': int(item['statistics'].get('viewCount', 0)),
'thumbnail_url': item['snippet']['thumbnails']['high']['url']
})
return videos
except Exception as e:
print(f"Video fetch error: {e}")
return []
def determine_importance(self, video_data: Dict) -> str:
"""Determine importance level"""
published_at_str = video_data['published_at']
# Add 'Z' when timezone information is missing
if 'Z' not in published_at_str and '+' not in published_at_str:
published_at_str += 'Z'
published_at = datetime.fromisoformat(published_at_str.replace('Z', '+00:00'))
now_utc = datetime.now(published_at.tzinfo)
hours_since_published = (now_utc - published_at).total_seconds() / 3600
view_count = video_data['view_count']
if hours_since_published <= 24 and view_count >= 10000:
return "Critical"
elif hours_since_published <= 48 and view_count >= 10000:
return "Important"
else:
return "Normal"
def detect_trends(self) -> List[Dict]:
"""Detect trending clusters"""
conn = sqlite3.connect('competitor_data.db')
cursor = conn.cursor()
# ้ๅป2ๆฅไปฅๅ
ใฎๅ็ปใงไบบ็ฉใ็นๅฎใใใใใฎใๅๅพ
two_days_ago = (datetime.now() - timedelta(days=2)).isoformat()
cursor.execute('''
SELECT detected_person, COUNT(*) as video_count,
GROUP_CONCAT(video_id) as video_ids,
GROUP_CONCAT(DISTINCT channel_id) as channels,
GROUP_CONCAT(detection_source) as sources
FROM videos
WHERE detected_person IS NOT NULL
AND detected_person != ''
AND published_at > ?
GROUP BY detected_person
HAVING COUNT(*) >= 2
AND COUNT(DISTINCT channel_id) >= 2
ORDER BY video_count DESC
''', (two_days_ago,))
trends = []
for row in cursor.fetchall():
person_name, count, video_ids, channels, sources = row
unique_channels = len(set(channels.split(',')))
trends.append({
'person_name': person_name,
'video_count': count,
'unique_channels': unique_channels,
'video_ids': video_ids.split(','),
'detection_sources': sources.split(',')
})
conn.close()
return trends
def update_all_data(self) -> str:
"""Update data for all channels"""
channels = self.get_channels()
total_new_videos = 0
conn = sqlite3.connect('competitor_data.db')
cursor = conn.cursor()
for channel in channels:
channel_id = channel['id']
last_update = channel['last_updated_at']
# Only fetch videos published after the channel's last_updated_at
videos = self.fetch_videos_from_channel(channel_id, since_date=last_update)
for video in videos:
# ๅ
ๆฌ็ใชไบบ็ฉๅๆฝๅบ
detected_person, detection_source = self.extract_person_comprehensive(video)
# ้่ฆๅบฆใๅคๅฎ
importance = self.determine_importance(video)
# ใใผใฟใใผในใซไฟๅญ
cursor.execute('''
INSERT OR IGNORE INTO videos
(video_id, channel_id, title, description, tags, published_at, view_count,
thumbnail_url, detected_person, detection_source, importance_level, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
video['video_id'],
channel_id,
video['title'],
video['description'],
','.join(video['tags']) if video['tags'] else '',
video['published_at'],
video['view_count'],
video['thumbnail_url'],
detected_person,
detection_source,
importance,
datetime.now(timezone.utc).isoformat()
))
if cursor.rowcount > 0:
total_new_videos += 1
# Update this channel's last_updated_at to now
cursor.execute('''
UPDATE channels
SET last_updated_at = ?
WHERE channel_id = ?
''', (datetime.now(timezone.utc).isoformat(), channel_id))
conn.commit()
conn.close()
return f"Update complete: added {total_new_videos} new videos"
def get_recent_videos_by_timerange(self, hours: int, limit: int = 50) -> List[Dict]:
"""Get videos within the specified time range sorted by view count (JST-based)"""
conn = sqlite3.connect('competitor_data.db')
cursor = conn.cursor()
# ๆฅๆฌๆ้๏ผJST = UTC+9๏ผใงๆๅฎๆ้ๅใฎๆฅๆใ่จ็ฎ
jst = timezone(timedelta(hours=9))
cutoff_time_jst = datetime.now(jst) - timedelta(hours=hours)
cutoff_time_utc = cutoff_time_jst.astimezone(timezone.utc)
cursor.execute('''
SELECT v.video_id, v.title, v.published_at, v.view_count, v.thumbnail_url,
v.detected_person, v.detection_source, v.importance_level,
c.channel_name, c.channel_icon_url, v.channel_id
FROM videos v
JOIN channels c ON v.channel_id = c.channel_id
WHERE v.published_at > ?
ORDER BY v.view_count DESC
LIMIT ?
''', (cutoff_time_utc.isoformat(), limit))
videos = []
for row in cursor.fetchall():
video_id, title, published_at, view_count, thumbnail_url, detected_person, \
detection_source, importance_level, channel_name, channel_icon_url, channel_id = row
# UTCๆ้ใJSTใซๅคๆ
published_at_utc = datetime.fromisoformat(published_at.replace('Z', '+00:00'))
published_at_jst = published_at_utc.astimezone(jst)
videos.append({
'video_id': video_id,
'title': title,
'published_at': published_at,
'published_at_jst': published_at_jst,
'view_count': view_count,
'thumbnail_url': thumbnail_url,
'detected_person': detected_person or 'Not detected',
'detection_source': detection_source or '-',
'importance_level': importance_level or 'Normal',
'channel_name': channel_name,
'channel_icon_url': channel_icon_url,
'channel_id': channel_id
})
conn.close()
return videos
def generate_recent_videos_html(self, hours: int, limit: int = 50) -> str:
"""Generate HTML for the recent videos list"""
videos = self.get_recent_videos_by_timerange(hours, limit)
# Build a description for the time range
time_range_text = f"Past {hours} hours"
html = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Recent Videos - {time_range_text}</title>
<style>
body {{ font-family: 'Helvetica Neue', Arial, sans-serif; margin: 0; padding: 20px; background-color: #000000 !important; color: #ffffff !important; }}
.container {{ max-width: 1200px; margin: 0 auto; background-color: transparent !important; }}
h1 {{ color: #ffffff !important; text-align: center; margin-bottom: 30px; }}
.stats-info {{ background: rgba(255,255,255,0.03) !important; padding: 15px !important; border-radius: 8px !important; margin-bottom: 20px !important; text-align: center !important; font-size: 16px !important; color: #bcdffb !important; }}
.video-item {{ display: flex !important; align-items: flex-start !important; background: #0b0b0b !important; margin-bottom: 15px !important; padding: 15px !important; border-radius: 8px !important; box-shadow: 0 2px 6px rgba(0,0,0,0.6) !important; border-left: 4px solid #222 !important; }}
.video-item.Critical {{ border-left-color: #b71c1c !important; background-color: #120202 !important; }}
.video-item.Important {{ border-left-color: #bf360c !important; background-color: #241100 !important; }}
.thumbnail {{ width: 160px !important; height: 90px !important; object-fit: cover !important; margin-right: 15px !important; border-radius: 4px !important; flex-shrink: 0 !important; }}
.video-info {{ flex: 1 !important; }}
.video-title {{ font-weight: bold !important; margin-bottom: 8px !important; font-size: 16px !important; line-height: 1.4 !important; color: #ffffff !important; }}
.video-meta {{ color: #cfcfcf !important; font-size: 14px !important; line-height: 1.6 !important; margin-bottom: 5px !important; }}
.channel-info {{ display: flex !important; align-items: center !important; margin-bottom: 8px !important; }}
.channel-icon {{ width: 24px !important; height: 24px !important; border-radius: 50% !important; margin-right: 8px !important; }}
.stats {{ color: #64b5f6 !important; font-weight: bold !important; }}
.importance-badge {{ display: inline-block !important; padding: 2px 8px !important; border-radius: 12px !important; font-size: 12px !important; font-weight: bold !important; margin-left: 10px !important; }}
.importance-badge.Critical {{ background: #b71c1c !important; color: white !important; }}
.importance-badge.Important {{ background: #bf360c !important; color: white !important; }}
.importance-badge.Normal {{ background: #2e7d32 !important; color: white !important; }}
.detection-badge {{ display: inline-block !important; padding: 2px 6px !important; border-radius: 8px !important; font-size: 11px !important; background: #1976d2 !important; color: white !important; margin-left: 5px !important; }}
.video-links {{ margin-top: 8px !important; }}
.video-links a {{ display: inline-block !important; margin-right: 15px !important; color: #90caf9 !important; text-decoration: none !important; font-size: 14px !important; padding: 4px 8px !important; border: 1px solid #263238 !important; border-radius: 4px !important; transition: background-color 0.3s !important; }}
.video-links a:hover {{ background-color: rgba(227,242,253,0.04) !important; }}
.no-videos {{ text-align: center !important; padding: 40px !important; color: #bdbdbd !important; font-size: 16px !important; }}
</style>
</head>
<body>
<div class="container">
<h1>๐บ Recent Videos ({time_range_text} ยท Sorted by views)</h1>
<div class="stats-info">
๐ Showing {len(videos)} videos (up to {limit}) | ๐ Times shown in JST | ๐ Sorted by views
</div>
"""
if videos:
for video in videos:
# ๆฅๆฌๆ้ใงใฎๆ็จฟๆฅๆใใใฉใผใใใ
published_jst = video['published_at_jst'].strftime('%m/%d %H:%M')
# Construct video and channel URLs
video_url = f"https://www.youtube.com/watch?v={video['video_id']}"
channel_url = f"https://www.youtube.com/channel/{video['channel_id']}"
html += f"""
<div class="video-item {video['importance_level']}">
<img src="{video['thumbnail_url']}" alt="thumbnail" class="thumbnail">
<div class="video-info">
<div class="video-title">{video['title']}</div>
<div class="channel-info">
<img src="{video['channel_icon_url']}" alt="channel" class="channel-icon">
<span>{video['channel_name']}</span>
</div>
<div class="video-meta">
๐
{published_jst} (JST) |
<span class="stats">๐ {video['view_count']:,} views</span> |
๐ค {video['detected_person']}
<span class="detection-badge">{video['detection_source']}</span>
<span class="importance-badge {video['importance_level']}">{video['importance_level']}</span>
</div>
<div class="video-links">
<a href="{video_url}" target="_blank">๐ฌ Watch video</a>
<a href="{channel_url}" target="_blank">๐บ Channel</a>
</div>
</div>
</div>
"""
else:
html += f"""
<div class="no-videos">
๐ญ No videos were posted in the {time_range_text}.<br>
Try updating data or expanding the time range.
</div>
"""
html += """
</div>
</body>
</html>
"""
return html
def generate_dashboard(self) -> str:
"""Generate the HTML dashboard"""
trends = self.detect_trends()
conn = sqlite3.connect('competitor_data.db')
cursor = conn.cursor()
# ้่ฆๅบฆๅฅใฎๅ็ปใๅๅพ
cursor.execute('''
SELECT v.title, v.published_at, v.view_count, v.detected_person,
v.importance_level, c.channel_name, v.thumbnail_url, v.video_id,
v.channel_id, v.detection_source
FROM videos v
JOIN channels c ON v.channel_id = c.channel_id
WHERE v.published_at > ?
ORDER BY
CASE v.importance_level
WHEN 'Critical' THEN 1
WHEN 'Important' THEN 2
ELSE 3
END,
v.view_count DESC
''', ((datetime.now() - timedelta(days=7)).isoformat(),))
videos = cursor.fetchall()
conn.close()
# Generate HTML
html = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>YouTube Competitor Analysis Dashboard (Global)</title>
<style>
body {{ font-family: 'Helvetica Neue', Arial, sans-serif; margin: 0; padding: 20px; background-color: #000000 !important; color: #ffffff !important; }}
.container {{ max-width: 1200px; margin: 0 auto; background-color: transparent !important; }}
h1 {{ color: #ffffff !important; text-align: center; margin-bottom: 30px; }}
h2 {{ border-bottom: 2px solid rgba(255,255,255,0.06) !important; padding-bottom: 10px !important; }}
.section {{ background: #070707 !important; margin-bottom: 30px !important; padding: 20px !important; border-radius: 8px !important; box-shadow: 0 2px 6px rgba(0,0,0,0.6) !important; }}
.trend-item {{ border: 1px solid #1f1f1f !important; margin-bottom: 15px !important; padding: 15px !important; border-radius: 5px !important; background: #0b0b0b !important; }}
.trend-title {{ font-size: 18px !important; font-weight: bold !important; color: #ff8a80 !important; margin-bottom: 10px !important; }}
.trend-meta {{ font-size: 14px !important; color: #bdbdbd !important; margin-bottom: 5px !important; }}
.video-item {{ display: flex !important; align-items: flex-start !important; margin-bottom: 15px !important; padding: 10px !important; border-left: 4px solid #222 !important; }}
.video-item.Critical {{ border-left-color: #b71c1c !important; background-color: #120202 !important; }}
.video-item.Important {{ border-left-color: #bf360c !important; background-color: #241100 !important; }}
.thumbnail {{ width: 160px !important; height: 90px !important; object-fit: cover !important; margin-right: 15px !important; border-radius: 4px !important; flex-shrink: 0 !important; }}
.video-info {{ flex: 1 !important; }}
.video-title {{ font-weight: bold !important; margin-bottom: 5px !important; font-size: 16px !important; color: #ffffff !important; }}
.video-meta {{ color: #cfcfcf !important; font-size: 14px !important; line-height: 1.6 !important; }}
.importance-badge {{ display: inline-block !important; padding: 2px 8px !important; border-radius: 12px !important; font-size: 12px !important; font-weight: bold !important; margin-left: 10px !important; }}
.importance-badge.Critical {{ background: #b71c1c !important; color: white !important; }}
.importance-badge.Important {{ background: #bf360c !important; color: white !important; }}
.importance-badge.Normal {{ background: #2e7d32 !important; color: white !important; }}
.detection-badge {{ display: inline-block !important; padding: 2px 6px !important; border-radius: 8px !important; font-size: 11px !important; background: #1976d2 !important; color: white !important; margin-left: 5px !important; }}
.stats {{ color: #64b5f6 !important; font-weight: bold !important; }}
.video-links {{ margin-top: 8px !important; }}
.video-links a {{ display: inline-block !important; margin-right: 15px !important; color: #90caf9 !important; text-decoration: none !important; font-size: 14px !important; padding: 4px 8px !important; border: 1px solid #263238 !important; border-radius: 4px !important; transition: background-color 0.3s !important; }}
.video-links a:hover {{ background-color: rgba(227,242,253,0.04) !important; }}
</style>
</head>
<body>
<div class="container">
<h1>๐ YouTube Competitor Analysis Dashboard (Global)</h1>
<div class="section">
<h2>๐ฅ Currently Trending Clusters</h2>
"""
if trends:
for trend in trends:
detection_sources = list(set(trend['detection_sources']))
sources_text = ', '.join(detection_sources)
html += f"""
<div class="trend-item">
<div class="trend-title">๐ค {trend['person_name']}</div>
<div class="trend-meta">๐บ <strong>{trend['video_count']}</strong> videos posted across <strong>{trend['unique_channels']}</strong> channels</div>
<div class="trend-meta">๐ Detection methods: {sources_text}</div>
</div>
"""
else:
html += "<p>There are currently no trending clusters.</p>"
html += """
</div>
<div class="section">
<h2>๐บ Recent Videos (Global person detection)</h2>
"""
for video in videos[:20]: # ไธไฝ20ๆฌใ่กจ็คบ
(title, published_at, view_count, detected_person, importance,
channel_name, thumbnail_url, video_id, channel_id, detection_source) = video
published_date = datetime.fromisoformat(published_at.replace('Z', '+00:00')).strftime('%m/%d %H:%M')
person_display = detected_person if detected_person else "Unknown"
# URLใ็ๆ
video_url = f"https://www.youtube.com/watch?v={video_id}"
channel_url = f"https://www.youtube.com/channel/{channel_id}"
html += f"""
<div class="video-item {importance}">
<img src="{thumbnail_url}" alt="thumbnail" class="thumbnail">
<div class="video-info">
<div class="video-title">{title}</div>
<div class="video-meta">
๐บ {channel_name} | ๐
{published_date} |
<span class="stats">๐ {view_count:,} views</span> |
๐ค {person_display}
<span class="detection-badge">{detection_source}</span>
<span class="importance-badge {importance}">{importance}</span>
</div>
<div class="video-links">
<a href="{video_url}" target="_blank">๐ฌ Watch video</a>
<a href="{channel_url}" target="_blank">๐บ Channel</a>
</div>
</div>
</div>
"""
html += """
</div>
</div>
</body>
</html>
"""
return html
def generate_channel_management_html(self) -> str:
"""Generate HTML for channel management"""
channels = self.get_channels()
html = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Channel Management (Global)</title>
<style>
body {{ font-family: 'Helvetica Neue', Arial, sans-serif; margin: 0; padding: 20px; background-color: #000000 !important; color: #ffffff !important; }}
.container {{ max-width: 800px; margin: 0 auto; background-color: transparent !important; }}
h2 {{ border-bottom: 2px solid rgba(255,255,255,0.06) !important; padding-bottom: 10px !important; }}
.channel-item {{ display: flex !important; align-items: center !important; background: #0b0b0b !important; margin-bottom: 15px !important; padding: 15px !important; border-radius: 8px !important; box-shadow: 0 2px 6px rgba(0,0,0,0.6) !important; }}
.channel-icon {{ width: 48px !important; height: 48px !important; border-radius: 50% !important; margin-right: 15px !important; object-fit: cover !important; }}
.channel-info {{ flex: 1 !important; }}
.channel-name {{ font-weight: bold !important; font-size: 16px !important; margin-bottom: 5px !important; color: #ffffff !important; }}
.channel-meta {{ color: #bdbdbd !important; font-size: 14px !important; }}
.channel-actions {{ display: flex !important; gap: 10px !important; }}
.btn {{ padding: 6px 12px !important; border: none !important; border-radius: 4px !important; cursor: pointer !important; font-size: 12px !important; text-decoration: none !important; display: inline-block !important; transition: opacity 0.3s !important; }}
.btn-edit {{ background: #1976d2 !important; color: white !important; border: 1px solid #263238 !important; }}
.btn-delete {{ background: #b71c1c !important; color: white !important; border: 1px solid #331111 !important; }}
.btn:hover {{ opacity: 0.9 !important; }}
</style>
</head>
<body>
<div class="container">
<h2>๐ Registered Channels (Global person detection)</h2>
"""
if channels:
for channel in channels:
added_date = datetime.fromisoformat(channel['added_date']).strftime('%Y/%m/%d')
subscriber_text = f"{channel['subscriber_count']:,} subscribers" if channel['subscriber_count'] else "Private"
html += f"""
<div class="channel-item">
<img src="{channel['icon_url']}" alt="icon" class="channel-icon">
<div class="channel-info">
<div class="channel-name">{channel['name']}</div>
<div class="channel-meta">
๐ฅ Subscribers: {subscriber_text} | ๐
Added: {added_date}
</div>
<div class="channel-meta">
๐ {channel['id']}
</div>
</div>
<div class="channel-actions">
<button class="btn btn-edit" onclick="editChannel('{channel['id']}', '{channel['name']}')">โ๏ธ Edit</button>
<button class="btn btn-delete" onclick="deleteChannel('{channel['id']}')">๐๏ธ Delete</button>
</div>
</div>
"""
else:
html += "<p>No channels registered.</p>"
html += """
<script>
function editChannel(channelId, currentName) {
const newName = prompt('Enter new channel name:', currentName);
if (newName && newName !== currentName) {
alert('Channel name updates must be performed from the Gradio interface.');
}
}
function deleteChannel(channelId) {
if (confirm('Are you sure you want to delete this channel?\\nRelated video data will also be removed.')) {
alert('Channel deletion must be performed from the Gradio interface.\\nChannel ID: ' + channelId);
}
}
</script>
</div>
</body>
</html>
"""
return html
# ใขใใชใฎใคใณในใฟใณในใไฝๆ
analyzer = YouTubeCompetitorAnalyzer()
# Strong global dark CSS to force dark mode even if hosting injects light styles.
# Uses high-specificity selectors and !important to override Hugging Face Spaces' theme.
DARK_THEME_CSS = """
:root, html, body, .gradio-container {
background-color: #000000 !important;
color: #ffffff !important;
color-scheme: dark !important;
}
.gradio-container, .gradio-container * {
background-color: transparent !important;
color: #ffffff !important;
border-color: #333333 !important;
}
/* Inputs, buttons and textareas */
button, .gr-button, input, textarea, select, .gradio-textbox, .gradio-file, .gradio-dropdown, .gradio-button {
background-color: #0b0b0b !important;
color: #ffffff !important;
border: 1px solid #333333 !important;
}
input::placeholder, textarea::placeholder {
color: #bfbfbf !important;
}
.gradio-markdown, .gradio-html, .gradio-label, .gradio-textbox, .gradio-output {
color: #ffffff !important;
}
/* Ensure components that Gradio or Spaces might wrap still show dark backgrounds */
.gradio-container .container, .gradio-container .section, .gradio-container .card {
background-color: #000000 !important;
color: #ffffff !important;
}
/* Give high contrast to borders and badges */
.importance-badge, .detection-badge {
color: #ffffff !important;
}
"""
# Gradio ใคใณใฟใผface
def add_channel_interface(channel_ids_text):
"""Interface function that supports adding multiple channel IDs"""
if not channel_ids_text:
return "Please enter channel ID"
# ๆน่กใงๅๅฒใใๅๅพใฎ็ฉบ็ฝใ้คๅปใใ็ฉบ่กใ็ก่ฆใใ
channel_ids = [cid.strip() for cid in channel_ids_text.split('\n') if cid.strip()]
if not channel_ids:
return "No valid channel IDs provided."
results = []
# Process each channel ID in order
for channel_id in channel_ids:
result = analyzer.add_channel(channel_id)
results.append(result)
# ็ตๆใๆน่กใง้ฃ็ตใใฆ่ฟใ
return "\n".join(results)
def delete_channel_interface(channel_id):
if not channel_id:
return "Please enter a channel ID to delete"
return analyzer.delete_channel(channel_id.strip())
def update_channel_name_interface(channel_id, new_name):
if not channel_id or not new_name:
return "Please enter channel ID and new name"
return analyzer.update_channel_name(channel_id.strip(), new_name.strip())
def update_data_interface():
return analyzer.update_all_data()
def show_dashboard():
return analyzer.generate_dashboard()
def show_channel_management():
return analyzer.generate_channel_management_html()
def show_recent_videos_interface(hours_selection, limit_selection):
"""Interface function for recent videos list"""
hours_map = {
"6 hours": 6,
"12 hours": 12,
"24 hours": 24,
"48 hours": 48
}
limit_map = {
"20 items": 20,
"50 items": 50,
"100 items": 100,
"200 items": 200
}
hours = hours_map.get(hours_selection, 24)
limit = limit_map.get(limit_selection, 50)
return analyzer.generate_recent_videos_html(hours, limit)
# Gradioใขใใชใฎๆง็ฏ
with gr.Blocks(title="YouTube Competitor Analysis (Global)", theme=gr.themes.Monochrome(), css=DARK_THEME_CSS) as app:
gr.Markdown("# ๐ YouTube Competitor Analysis App (Global)")
gr.Markdown("Analyze competitor channel uploads and detect global clustered trends using **Gemini 2.5 Flash**.")
with gr.Tab("๐ Dashboard"):
gr.Markdown("## Global Analysis Dashboard")
refresh_btn = gr.Button("๐ Refresh Dashboard", variant="secondary")
dashboard_html = gr.HTML()
refresh_btn.click(show_dashboard, inputs=[], outputs=[dashboard_html])
# initial load
app.load(show_dashboard, inputs=[], outputs=[dashboard_html])
with gr.Tab("๐บ Recent Videos"):
gr.Markdown("## ๐บ Recent Videos (select time range and max items)")
gr.Markdown("""
### Features
- โฐ Time range selection: choose between 6 to 48 hours
- ๐ Sorted by view count: show the highest-view videos first
- ๐ Times displayed in JST (UTC+9)
- ๐ข Max items: limit display between 20 and 200
- ๐ Global detection: detect notable people worldwide
""")
with gr.Row():
hours_dropdown = gr.Dropdown(
choices=["6 hours", "12 hours", "24 hours", "48 hours"],
value="24 hours",
label="โฐ Time Range"
)
limit_dropdown = gr.Dropdown(
choices=["20 items", "50 items", "100 items", "200 items"],
value="50 items",
label="๐ข Max items"
)
update_recent_btn = gr.Button("๐ Update Recent Videos", variant="primary", size="lg")
recent_videos_html = gr.HTML()
update_recent_btn.click(
show_recent_videos_interface,
inputs=[hours_dropdown, limit_dropdown],
outputs=[recent_videos_html]
)
# initial load (24 hours, 50 items)
app.load(
show_recent_videos_interface,
inputs=[gr.State("24 hours"), gr.State("50 items")],
outputs=[recent_videos_html]
)
with gr.Tab("๐ Data Update"):
gr.Markdown("## ๐ Global AI High-Precision Data Update System")
gr.Markdown("""
### ๐ฏ Person extraction using Gemini 2.5 Flash (priority order)
1. **๐ค Gemini title analysis** - high-precision extraction of notable people from titles
2. **๐ค Gemini description analysis** - detect people via hashtags and description
3. **๐ท๏ธ Tag analysis** - identify multilingual person names from tags
4. **๐ผ๏ธ Thumbnail OCR** - read text from thumbnails
5. **๐ค Face recognition** - identify persons via face recognition
**๐ Global support**: detects people regardless of nationality, era, or field
""")
# API key inputs (allow user to provide keys at runtime instead of env vars)
with gr.Row():
youtube_key_input = gr.Textbox(label="YouTube API Key", placeholder="Enter YouTube API key", type="password")
gemini_key_input = gr.Textbox(label="Gemini API Key", placeholder="Enter Gemini API key", type="password")
apply_keys_btn = gr.Button("Apply API Keys", variant="secondary")
api_keys_result = gr.Textbox(label="API Key Status", interactive=False)
# Keep applied keys in hidden Gradio state so other callbacks can reference them if needed
youtube_key_state = gr.State("")
gemini_key_state = gr.State("")
# set_api_keys now returns (status, youtube_key, gemini_key)
apply_keys_btn.click(
set_api_keys,
inputs=[youtube_key_input, gemini_key_input],
outputs=[api_keys_result, youtube_key_state, gemini_key_state]
)
update_btn = gr.Button("๐ Start Global Data Update", variant="primary", size="lg")
update_result = gr.Textbox(label="Update Result", interactive=False)
update_btn.click(update_data_interface, inputs=[], outputs=[update_result])
with gr.Tab("๐บ Channel Management"):
with gr.Row():
with gr.Column(scale=2):
gr.Markdown("## Add Channels")
# Textbox switched to multiline to support multiple lines
channel_input = gr.TextArea(
label="Channel ID (one per line)",
placeholder="UCxxxxxxxxxxxxxxxxxxxxxxxx\nUCyyyyyyyyyyyyyyyyyyyyyyyy",
info="Enter multiple YouTube channel IDs separated by newlines"
)
add_btn = gr.Button("Add Channels", variant="primary")
add_result = gr.Textbox(label="Result", interactive=False)
add_btn.click(add_channel_interface, inputs=[channel_input], outputs=[add_result])
with gr.Column(scale=2):
gr.Markdown("## Delete Channel")
delete_channel_input = gr.Textbox(
label="Channel ID to delete",
placeholder="UCxxxxxxxxxxxxxxxxxxxxxxxx"
)
delete_btn = gr.Button("Delete Channel", variant="stop")
delete_result = gr.Textbox(label="Delete Result", interactive=False)
delete_btn.click(delete_channel_interface, inputs=[delete_channel_input], outputs=[delete_result])
with gr.Row():
with gr.Column():
gr.Markdown("## Edit Channel Name")
edit_channel_id = gr.Textbox(label="Channel ID to edit", placeholder="UCxxxxxxxxxxxxxxxxxxxxxxxx")
new_channel_name = gr.Textbox(label="New channel name", placeholder="Enter new name")
update_name_btn = gr.Button("Update Name", variant="secondary")
update_name_result = gr.Textbox(label="Update Result", interactive=False)
update_name_btn.click(
update_channel_name_interface,
inputs=[edit_channel_id, new_channel_name],
outputs=[update_name_result]
)
gr.Markdown("## Registered Channels")
channel_list_html = gr.HTML()
refresh_channels_btn = gr.Button("Refresh list", variant="secondary")
refresh_channels_btn.click(show_channel_management, inputs=[], outputs=[channel_list_html])
# initial load
app.load(show_channel_management, inputs=[], outputs=[channel_list_html])
if __name__ == "__main__":
app.launch() |