Spaces:
Sleeping
Sleeping
Download app.py from daudcheema1/briefTask-AI: direct link, hf CLI and curl.
- Browser
- Download file 4.38 kB
-
https://huggingface.co/spaces/daudcheema1/briefTask-AI/resolve/main/app.py
- Command line
-
hf download hf://spaces/daudcheema1/briefTask-AI/app.py
-
curl -L -o app.py https://huggingface.co/spaces/daudcheema1/briefTask-AI/resolve/main/app.py
4.38 kB
| import os | |
| import re | |
| from urllib.parse import urlparse, parse_qs | |
| import requests | |
| import spaces | |
| import gradio as gr | |
| from groq import Groq | |
| # Pull key from Hugging Face Settings -> Secret | |
| client = Groq(api_key=os.environ.get("GROQ_API_KEY")) | |
| def extract_video_id(url_or_topic: str): | |
| parsed = urlparse(url_or_topic) | |
| if parsed.hostname in ("youtu.be", "www.youtu.be"): | |
| return parsed.path.lstrip("/") | |
| if parsed.hostname in ("youtube.com", "www.youtube.com"): | |
| if parsed.path == "/watch": | |
| return parse_qs(parsed.query).get("v", [None])[0] | |
| if parsed.path.startswith("/shorts/"): | |
| parts = parsed.path.split("/") | |
| return parts[2] if len(parts) > 2 else None | |
| match = re.search(r"(?:v=|\/|youtu\.be\/)([0-9A-Za-z_-]{11})", url_or_topic) | |
| return match.group(1) if match else None | |
| def get_clean_transcript(video_id: str) -> str: | |
| """Fetches transcript via edge proxy to bypass cloud datacenter IP blocks.""" | |
| # Attempt 1: Free Edge-cached proxy | |
| try: | |
| url = f"https://youtube-transcript.ai/transcript/{video_id}.txt" | |
| resp = requests.get(url, timeout=10, headers={"User-Agent": "Mozilla/5.0"}) | |
| if resp.status_code == 200 and len(resp.text) > 100 and "Error" not in resp.text[:50]: | |
| # Clean markdown headers if present | |
| lines = [l for l in resp.text.split("\n") if not l.startswith("#")] | |
| return "\n".join(lines).strip() | |
| except Exception: | |
| pass | |
| # Attempt 2: Public Invidious mirror | |
| instances = ["https://inv.tux.pizza", "https://invidious.nerdvpn.de"] | |
| for inst in instances: | |
| try: | |
| res = requests.get(f"{inst}/api/v1/captions/{video_id}", timeout=6) | |
| if res.status_code == 200: | |
| captions = res.json().get("captions", []) | |
| if captions: | |
| cap_url = inst + captions[0].get("url") | |
| cap_res = requests.get(cap_url, timeout=6) | |
| clean = re.sub(r'<[^>]+>', ' ', cap_res.text) | |
| clean = re.sub(r'\s+', ' ', clean).strip() | |
| if len(clean) > 100: | |
| return clean | |
| except Exception: | |
| continue | |
| raise ValueError( | |
| "Could not retrieve video captions. Please test with a public video that has captions enabled." | |
| ) | |
| def summarize(video_url: str): | |
| if not video_url.strip(): | |
| return "Please provide a valid YouTube URL." | |
| video_id = extract_video_id(video_url) | |
| if not video_id: | |
| return "Invalid YouTube URL format. Please provide a standard watch link or shorts URL." | |
| try: | |
| transcript_text = get_clean_transcript(video_id) | |
| except Exception as e: | |
| return f"Extraction Notice: {str(e)}" | |
| try: | |
| completion = client.chat.completions.create( | |
| model="llama-3.3-70b-versatile", | |
| messages=[ | |
| { | |
| "role": "system", | |
| "content": ( | |
| "You are BriefTube AI, an enterprise-grade video intelligence engine. " | |
| "Synthesize the provided transcript strictly and objectively from the source text. " | |
| "Format cleanly into:\n" | |
| "### 1. Executive Summary\n" | |
| "### 2. Key Highlights & Actionable Takeaways\n" | |
| "### 3. Critical Facts, Entities & Mentions" | |
| ) | |
| }, | |
| { | |
| "role": "user", | |
| "content": f"Transcript:\n{transcript_text[:14000]}" | |
| } | |
| ], | |
| temperature=0.2 | |
| ) | |
| return completion.choices[0].message.content | |
| except Exception as e: | |
| return f"Groq LLM Synthesis Error: {str(e)}" | |
| demo = gr.Interface( | |
| fn=summarize, | |
| inputs=gr.Textbox( | |
| label="YouTube Video URL", | |
| placeholder="https://www.youtube.com/watch?v=aircAruvnKk", | |
| lines=1 | |
| ), | |
| outputs=gr.Markdown(label="BriefTube Synthesis & Intelligence"), | |
| title="BriefTube AI — Video Intelligence Platform", | |
| description="Asynchronous video summarization pipeline powered by Meta Llama 3.3 70B via Groq LPUs.", | |
| examples=[ | |
| ["https://www.youtube.com/watch?v=aircAruvnKk"] | |
| ] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |