Spaces:
Sleeping
Sleeping
rsnarsna commited on
Commit Β·
dda4ec3
0
Parent(s):
first commit
Browse filesFiles changed (6)
- .gitignoreAdded+220
- client_secret.jsonAdded+12
- fastapi_app.pyAdded+1138
- gemini_transcript.pyAdded+445
- index.htmlAdded+378
- requirements.txtAdded+7
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220# Byte-compiled / optimized / DLL files__pycache__/*.py[codz]*$py.class
# C extensions*.so
# Distribution / packaging.Pythonbuild/develop-eggs/dist/downloads/eggs/.eggs/lib/lib64/parts/sdist/var/wheels/share/python-wheels/*.egg-info/.installed.cfg*.eggMANIFEST
# PyInstaller# Usually these files are written by a python script from a template# before PyInstaller builds the exe, so as to inject date/other infos into it.*.manifest*.spec
# Installer logspip-log.txtpip-delete-this-directory.txt
# Unit test / coverage reportshtmlcov/.tox/.nox/.coverage.coverage.*.cachenosetests.xmlcoverage.xml*.cover*.py.cover*.lcov.hypothesis/.pytest_cache/cover/
# Translations*.mo*.pot
# Django stuff:*.loglocal_settings.pydb.sqlite3db.sqlite3-journal
# Flask stuff:instance/.webassets-cache
# Scrapy stuff:.scrapy
# Sphinx documentationdocs/_build/
# PyBuilder.pybuilder/target/
# Jupyter Notebook.ipynb_checkpoints
# IPythonprofile_default/ipython_config.py
# pyenv# For a library or package, you might want to ignore these files since the code is# intended to run in multiple environments; otherwise, check them in:# .python-version
# pipenv# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.# However, in case of collaboration, if having platform-specific dependencies or dependencies# having no cross-platform support, pipenv may install dependencies that don't work, or not# install all needed dependencies.# Pipfile.lock
# UV# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.# This is especially recommended for binary packages to ensure reproducibility, and is more# commonly ignored for libraries.# uv.lock
# poetry# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.# This is especially recommended for binary packages to ensure reproducibility, and is more# commonly ignored for libraries.# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control# poetry.lock# poetry.toml
# pdm# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.# https://pdm-project.org/en/latest/usage/project/#working-with-version-control# pdm.lock# pdm.toml.pdm-python.pdm-build/
# pixi# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.# pixi.lock# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one# in the .venv directory. It is recommended not to include this directory in version control..pixi/*!.pixi/config.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm__pypackages__/
# Celery stuffcelerybeat-schedule*celerybeat.pid
# Redis*.rdb*.aof*.pid
# RabbitMQmnesia/rabbitmq/rabbitmq-data/
# ActiveMQactivemq-data/
# SageMath parsed files*.sage.py
# Environments.env.envrc.venvenv/venv/ENV/env.bak/venv.bak/
# Spyder project settings.spyderproject.spyproject
# Rope project settings.ropeproject
# mkdocs documentation/site
# mypy.mypy_cache/.dmypy.jsondmypy.json
# Pyre type checker.pyre/
# pytype static type analyzer.pytype/
# Cython debug symbolscython_debug/
# PyCharm# JetBrains specific template is maintained in a separate JetBrains.gitignore that can# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore# and can be added to the global gitignore or merged into this file. For a more nuclear# option (not recommended) you can uncomment the following to ignore the entire idea folder.# .idea/
# Abstra# Abstra is an AI-powered process automation framework.# Ignore directories containing user credentials, local state, and settings.# Learn more at https://abstra.io/docs.abstra/
# Visual Studio Code# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore# and can be added to the global gitignore or merged into this file. However, if you prefer, # you could uncomment the following to ignore the entire vscode folder# .vscode/# Temporary file for partial code executiontempCodeRunnerFile.py
# Ruff stuff:.ruff_cache/
# PyPI configuration file.pypirc
# Marimomarimo/_static/marimo/_lsp/__marimo__/
# Streamlit.streamlit/secrets.toml123456789101112{ "web": { "client_id": "769133159215-9gbq0l5v49kmclfcq7vbq7tutck0aphd.apps.googleusercontent.com", "project_id": "root-isotope-497908-u0", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_secret": "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", "redirect_uris": ["http://localhost:8000/auth/callback"], "javascript_origins": ["http://localhost:8000"] }}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138import osimport jsonimport base64import hashlibimport secretsimport shutilimport tempfileimport threading
from pathlib import Pathfrom email.mime.text import MIMETextfrom datetime import datetime, timezone
from fastapi.responses import FileResponsefrom fastapi import FastAPI, Request, HTTPExceptionfrom fastapi.responses import RedirectResponse, HTMLResponsefrom pydantic import BaseModel
from google_auth_oauthlib.flow import Flowfrom google.auth.transport.requests import Request as GoogleRequestfrom google.oauth2.credentials import Credentialsfrom googleapiclient.discovery import buildfrom googleapiclient.http import MediaFileUpload
from gemini_transcript import TranscriptSummaryPipeline
# ============================================================================# CONFIG# ============================================================================
os.environ.setdefault("OAUTHLIB_INSECURE_TRANSPORT", "1")
BASE_DIR = Path(__file__).resolve().parentCLIENT_SECRETS = os.getenv("CLIENT_SECRETS", str(BASE_DIR / "client_secret.json"))TOKEN_PATH = os.getenv("GOOGLE_OAUTH_TOKEN_PATH", str(BASE_DIR / "Google_oauth_token.json"))REDIRECT_URI = os.getenv("REDIRECT_URI", "http://localhost:8000/auth/callback")STATE_FILE = BASE_DIR / "oauth_states.json"DEFAULT_SPREADSHEET_ID = os.getenv("DEFAULT_SPREADSHEET_ID", "1XA3vW_guHBT-ktkYvhktmUqcquECBe8exGZAoSQS3Ag")DEFAULT_DRIVE_FOLDER_ID = os.getenv("DEFAULT_DRIVE_FOLDER_ID", "1hI6dNXysR_2p9gHkDpsI-iwMExmy2hhR")
SCOPES = [ "https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/gmail.send", "https://www.googleapis.com/auth/drive.file",]
OUTPUT_DIR = BASE_DIR / "output"OUTPUT_DIR.mkdir(exist_ok=True)
SUMMARY_FILE = OUTPUT_DIR / "summary.txt"QA_FILE = OUTPUT_DIR / "qa.txt"TRANSCRIPT_FILE = OUTPUT_DIR / "transcript.txt"
SHEETS_HEADERS = [ "Timestamp", # A "Job ID", # B "Video Title", # C "YouTube URL", # D "Model Used", # E "Status", # F "Summary Drive Link", # G "Q&A Drive Link", # H "Transcript Drive Link", # I "Email Sent To", # J "Email Status", # K "Email Message ID", # L "Completed At", # M "Error", # N]
# ============================================================================# IN-MEMORY JOB STORE# ============================================================================
_jobs: dict[str, dict] = {}_jobs_lock = threading.Lock()
STEPS = [ "fetch_transcript", "summarize", "create_drive_folder", "upload_summary", "upload_qa", "upload_transcript", "send_email", "log_sheet",]
def _new_job(job_id: str, youtube_url: str, email_to: str) -> dict: job = { "job_id": job_id, "status": "initiated", "youtube_url": youtube_url, "email_to": email_to, "started_at": datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC"), "completed_at": None, "steps": {s: "pending" for s in STEPS}, "result": None, "error": None, } with _jobs_lock: _jobs[job_id] = job return job
def _update_job(job_id: str, **kwargs): with _jobs_lock: if job_id in _jobs: _jobs[job_id].update(kwargs)
def _set_step(job_id: str, step: str, state: str): with _jobs_lock: if job_id in _jobs: _jobs[job_id]["steps"][step] = state
# ============================================================================# APP# ============================================================================
app = FastAPI(title="Google Integration API", version="7.0.0")
# ============================================================================# MODELS# ============================================================================
class GenerateRequest(BaseModel): youtube_url: str email_to: str
class EmailRequest(BaseModel): to: str subject: str body: str
class CreateFileRequest(BaseModel): filename: str content: str mimetype: str = "text/plain" folder_id: str | None = None make_public: bool = True
class SheetWriteRequest(BaseModel): spreadsheet_id: str = DEFAULT_SPREADSHEET_ID range_name: str = "Sheet1!A1" values: list[list]
class SheetClearRequest(BaseModel): spreadsheet_id: str = DEFAULT_SPREADSHEET_ID range_name: str = "Sheet1!A1:Z1000"
# ============================================================================# STATE PERSISTENCE# ============================================================================
def load_states() -> dict: try: return json.loads(STATE_FILE.read_text()) except Exception: return {}
def save_states(states: dict) -> None: STATE_FILE.write_text(json.dumps(states))
# ============================================================================# AUTH# ============================================================================
def create_flow() -> Flow: if not os.path.exists(CLIENT_SECRETS): raise FileNotFoundError(f"Missing client secret: {CLIENT_SECRETS}") return Flow.from_client_secrets_file( CLIENT_SECRETS, scopes=SCOPES, redirect_uri=REDIRECT_URI )
def load_credentials() -> Credentials | None: if not os.path.exists(TOKEN_PATH): return None creds = Credentials.from_authorized_user_file(TOKEN_PATH, SCOPES) if not creds.valid: if creds.expired and creds.refresh_token: creds.refresh(GoogleRequest()) Path(TOKEN_PATH).write_text(creds.to_json(), encoding="utf-8") else: return None return creds
def require_credentials() -> Credentials: creds = load_credentials() if creds is None: raise HTTPException( status_code=401, detail="Not authenticated. Visit http://localhost:8000/auth/start", ) return creds
# ============================================================================# GMAIL# ============================================================================
def _raw_message(to: str, subject: str, body: str) -> dict: msg = MIMEText(body) msg["to"] = to msg["subject"] = subject return {"raw": base64.urlsafe_b64encode(msg.as_bytes()).decode()}
def send_email(to: str, subject: str, body: str, creds: Credentials = None) -> dict: if creds is None: creds = require_credentials() return ( build("gmail", "v1", credentials=creds) .users().messages() .send(userId="me", body=_raw_message(to, subject, body)) .execute() )
# ============================================================================# DRIVE# ============================================================================
def _direct_link(file_id: str) -> str: return f"https://drive.google.com/uc?export=download&id={file_id}"
def _make_public(service, file_id: str) -> dict: service.permissions().create( fileId=file_id, body={"type": "anyone", "role": "reader"}, ).execute() return service.files().get( fileId=file_id, fields="id,name,webViewLink,webContentLink,mimeType,size,createdTime,modifiedTime", ).execute()
def _drive(creds: Credentials): return build("drive", "v3", credentials=creds)
def create_drive_folder( folder_name: str, parent_folder_id: str | None = None, creds: Credentials = None,) -> str: if creds is None: creds = require_credentials() meta = { "name": folder_name, "mimeType": "application/vnd.google-apps.folder", } pid = parent_folder_id or DEFAULT_DRIVE_FOLDER_ID if pid: meta["parents"] = [pid] folder = _drive(creds).files().create(body=meta, fields="id").execute() return folder["id"]
def upload_file_to_drive( filepath: str, creds: Credentials = None, folder_id: str | None = None, make_public: bool = True,) -> dict: if creds is None: creds = require_credentials() svc = _drive(creds) meta = {"name": os.path.basename(filepath)} if folder_id: meta["parents"] = [folder_id] media = MediaFileUpload(filepath, resumable=True) file = svc.files().create( body=meta, media_body=media, fields="id,name,webViewLink,webContentLink,mimeType,size", ).execute() file_id = file["id"] if make_public: file = _make_public(svc, file_id) file["direct_download_link"] = _direct_link(file_id) return file
def create_file_on_drive( filename: str, content: str, mimetype: str = "text/plain", creds: Credentials = None, folder_id: str | None = None, make_public: bool = True,) -> dict: if creds is None: creds = require_credentials() svc = _drive(creds) meta = {"name": filename} if folder_id: meta["parents"] = [folder_id]
suffix = Path(filename).suffix or ".txt" tmp = tempfile.NamedTemporaryFile( mode="w", suffix=suffix, delete=False, encoding="utf-8" ) tmp.write(content) tmp.close() tmp_path = tmp.name
try: media = MediaFileUpload(tmp_path, mimetype=mimetype, resumable=True) file = svc.files().create( body=meta, media_body=media, fields="id,name,webViewLink,webContentLink,mimeType,size", ).execute() media._fd.close() finally: try: os.unlink(tmp_path) except Exception: pass
file_id = file["id"] if make_public: file = _make_public(svc, file_id) file["direct_download_link"] = _direct_link(file_id) return file
def get_drive_file(file_id: str, creds: Credentials = None) -> dict: if creds is None: creds = require_credentials() file = _drive(creds).files().get( fileId=file_id, fields="id,name,webViewLink,webContentLink,mimeType,size,createdTime,modifiedTime", ).execute() file["direct_download_link"] = _direct_link(file_id) return file
def list_drive_files( folder_id: str | None = None, page_size: int = 20, creds: Credentials = None,) -> list: if creds is None: creds = require_credentials() fid = folder_id or DEFAULT_DRIVE_FOLDER_ID query = f"'{fid}' in parents and trashed=false" if fid else "trashed=false" files = _drive(creds).files().list( q=query, pageSize=page_size, fields="files(id,name,webViewLink,webContentLink,mimeType,size,createdTime)", ).execute().get("files", []) for f in files: f["direct_download_link"] = _direct_link(f["id"]) return files
# ============================================================================# SHEETS# ============================================================================
def _sheets(creds: Credentials): return build("sheets", "v4", credentials=creds)
def read_sheet( spreadsheet_id: str, range_name: str = "Sheet1!A1:Z1000", creds: Credentials = None,) -> list: if creds is None: creds = require_credentials() return ( _sheets(creds).spreadsheets().values() .get(spreadsheetId=spreadsheet_id, range=range_name) .execute().get("values", []) )
def write_sheet( spreadsheet_id: str, range_name: str, values: list[list], creds: Credentials = None,) -> dict: if creds is None: creds = require_credentials() return ( _sheets(creds).spreadsheets().values() .update( spreadsheetId=spreadsheet_id, range=range_name, valueInputOption="USER_ENTERED", body={"values": values}, ).execute() )
def append_sheet( spreadsheet_id: str, range_name: str, values: list[list], creds: Credentials = None,) -> dict: if creds is None: creds = require_credentials() return ( _sheets(creds).spreadsheets().values() .append( spreadsheetId=spreadsheet_id, range=range_name, valueInputOption="USER_ENTERED", insertDataOption="INSERT_ROWS", body={"values": values}, ).execute() )
def clear_sheet( spreadsheet_id: str, range_name: str, creds: Credentials = None,) -> dict: if creds is None: creds = require_credentials() return ( _sheets(creds).spreadsheets().values() .clear(spreadsheetId=spreadsheet_id, range=range_name) .execute() )
def get_sheet_metadata( spreadsheet_id: str, creds: Credentials = None,) -> dict: if creds is None: creds = require_credentials() info = _sheets(creds).spreadsheets().get(spreadsheetId=spreadsheet_id).execute() return { "spreadsheet_id": info["spreadsheetId"], "title": info["properties"]["title"], "url": f"https://docs.google.com/spreadsheets/d/{info['spreadsheetId']}", "sheets": [ { "sheet_id": s["properties"]["sheetId"], "title": s["properties"]["title"], "rows": s["properties"]["gridProperties"]["rowCount"], "cols": s["properties"]["gridProperties"]["columnCount"], } for s in info.get("sheets", []) ], }
def append_row_to_sheet( values: list, spreadsheet_id: str = DEFAULT_SPREADSHEET_ID, range_name: str = "Sheet1!A1", creds: Credentials = None,): if not spreadsheet_id: return None return append_sheet(spreadsheet_id, range_name, [values], creds=creds)
# ============================================================================# SHEETS β JOB RECORD HELPERS# ============================================================================
def ensure_sheet_header(creds: Credentials = None) -> None: if not DEFAULT_SPREADSHEET_ID: return try: existing = read_sheet(DEFAULT_SPREADSHEET_ID, "Sheet1!A1:Z1", creds=creds) if not existing: write_sheet( DEFAULT_SPREADSHEET_ID, "Sheet1!A1", [SHEETS_HEADERS], creds=creds, ) except Exception as exc: print(f"[WARN] Could not write sheet header: {exc}")
def _find_job_row(job_id: str, creds: Credentials) -> int | None: """Find 1-based row number of job_id in column B.""" try: rows = read_sheet(DEFAULT_SPREADSHEET_ID, "Sheet1!B:B", creds=creds) for i, row in enumerate(rows, start=1): if row and row[0] == job_id: return i except Exception: pass return None
def _create_sheet_record( job_id: str, timestamp: str, youtube_url: str, email_to: str, creds: Credentials,) -> None: """Insert initial row when job starts.""" try: row = [ timestamp, # A β Timestamp job_id, # B β Job ID "", # C β Video Title youtube_url, # D β YouTube URL "", # E β Model Used "initiated", # F β Status "", # G β Summary Link "", # H β Q&A Link "", # I β Transcript Link email_to, # J β Email Sent To "", # K β Email Status "", # L β Email Message ID "", # M β Completed At "", # N β Error ] append_sheet(DEFAULT_SPREADSHEET_ID, "Sheet1!A1", [row], creds=creds) except Exception as exc: print(f"[WARN] Could not create sheet record: {exc}")
def _update_sheet_record( job_id: str, creds: Credentials, video_title: str = "", model_used: str = "", status: str = "", summary_link: str = "", qa_link: str = "", transcript_link: str = "", email_status: str = "", email_msg_id: str = "", completed_at: str = "", error: str = "",) -> None: """Find job row by job_id and overwrite with updated values.""" if not DEFAULT_SPREADSHEET_ID: return try: row_num = _find_job_row(job_id, creds) if row_num is None: print(f"[WARN] Row for job {job_id} not found in sheet.") return
# Read existing to preserve immutable columns existing = read_sheet( DEFAULT_SPREADSHEET_ID, f"Sheet1!A{row_num}:N{row_num}", creds=creds, ) existing_row = existing[0] if existing else [""] * 14
def _v(new: str, idx: int) -> str: return new if new != "" else ( existing_row[idx] if len(existing_row) > idx else "" )
updated_row = [ _v("", 0), # A β Timestamp (immutable) job_id, # B β Job ID (immutable) _v(video_title, 2), # C β Video Title _v("", 3), # D β YouTube URL (immutable) _v(model_used, 4), # E β Model Used _v(status, 5), # F β Status _v(summary_link, 6), # G β Summary Link _v(qa_link, 7), # H β Q&A Link _v(transcript_link, 8), # I β Transcript Link _v("", 9), # J β Email Sent To (immutable) _v(email_status, 10), # K β Email Status _v(email_msg_id, 11), # L β Email Message ID _v(completed_at, 12), # M β Completed At _v(error, 13), # N β Error ]
write_sheet( DEFAULT_SPREADSHEET_ID, f"Sheet1!A{row_num}:N{row_num}", [updated_row], creds=creds, ) except Exception as exc: print(f"[WARN] Could not update sheet record: {exc}")
# ============================================================================# STARTUP# ============================================================================
@app.on_event("startup")def on_startup(): creds = load_credentials() if creds: ensure_sheet_header(creds=creds)
# ============================================================================# BASIC ROUTES# ============================================================================
@app.get("/")def root(): return FileResponse("index.html")
@app.get("/health")def health(): creds = load_credentials() return { "status": "ok", "version": "7.0.0", "authenticated": creds is not None, "endpoints": { "auth": ["/auth/start", "/auth/status", "/auth/revoke"], "gmail": ["/email"], "drive": ["/drive/create", "/drive/file/{id}", "/drive/files"], "sheets": ["/sheets/info", "/sheets/read", "/sheets/write", "/sheets/append", "/sheets/clear"], "jobs": ["/generate", "/status/{job_id}", "/jobs"], "misc": ["/health"], }, }
# ============================================================================# OAUTH# ============================================================================
@app.get("/auth/start")def auth_start(): flow = create_flow() verifier = secrets.token_urlsafe(64) challenge = ( base64.urlsafe_b64encode(hashlib.sha256(verifier.encode()).digest()) .rstrip(b"=").decode() ) auth_url, state = flow.authorization_url( access_type="offline", include_granted_scopes="true", prompt="consent", code_challenge=challenge, code_challenge_method="S256", ) states = load_states() states[state] = verifier save_states(states) return RedirectResponse(auth_url)
@app.get("/auth/callback")def auth_callback(request: Request): state = request.query_params.get("state", "") states = load_states() if state not in states: raise HTTPException(status_code=400, detail="Invalid or expired OAuth state.") verifier = states.pop(state) save_states(states) flow = create_flow() flow.fetch_token( authorization_response=str(request.url).replace("https://", "http://"), code_verifier=verifier, ) Path(TOKEN_PATH).write_text(flow.credentials.to_json(), encoding="utf-8") return HTMLResponse(""" <html> <body style="font-family:sans-serif;text-align:center;padding-top:80px; background:#f0fdf4;color:#166534"> <h1>β
Authorization Successful</h1> <p>Gmail, Drive and Sheets are now connected.</p> <p>You can close this tab.</p> </body> </html> """)
@app.get("/auth/status")def auth_status(): creds = load_credentials() return {"authenticated": creds is not None}
@app.delete("/auth/revoke")def auth_revoke(): for p in [Path(TOKEN_PATH), STATE_FILE]: if p.exists(): p.unlink() return {"status": "revoked"}
# ============================================================================# EMAIL# ============================================================================
@app.post("/email")def email(payload: EmailRequest): creds = require_credentials() result = send_email(payload.to, payload.subject, payload.body, creds=creds) return {"status": "sent", "message_id": result.get("id")}
# ============================================================================# DRIVE ROUTES# ============================================================================
@app.post("/drive/create")def drive_create(payload: CreateFileRequest): creds = require_credentials() file = create_file_on_drive( filename=payload.filename, content=payload.content, mimetype=payload.mimetype, creds=creds, folder_id=payload.folder_id, make_public=payload.make_public, ) return { "file_id": file["id"], "name": file["name"], "mime_type": file.get("mimeType"), "web_view_link": file.get("webViewLink"), "direct_download_link": file["direct_download_link"], }
@app.get("/drive/file/{file_id}")def drive_get_file(file_id: str): creds = require_credentials() file = get_drive_file(file_id, creds=creds) return { "file_id": file["id"], "name": file["name"], "mime_type": file.get("mimeType"), "size_bytes": file.get("size"), "created": file.get("createdTime"), "modified": file.get("modifiedTime"), "web_view_link": file.get("webViewLink"), "direct_download_link": file["direct_download_link"], }
@app.get("/drive/files")def drive_list_files(folder_id: str = "", limit: int = 20): creds = require_credentials() files = list_drive_files( folder_id=folder_id or None, page_size=limit, creds=creds, ) return {"count": len(files), "files": files}
# ============================================================================# SHEETS ROUTES# ============================================================================
@app.get("/sheets/info")def sheets_info(spreadsheet_id: str = DEFAULT_SPREADSHEET_ID): if not spreadsheet_id: raise HTTPException(status_code=400, detail="spreadsheet_id is required.") return get_sheet_metadata(spreadsheet_id, creds=require_credentials())
@app.get("/sheets/read")def sheets_read( spreadsheet_id: str = DEFAULT_SPREADSHEET_ID, range_name: str = "Sheet1!A1:Z1000",): if not spreadsheet_id: raise HTTPException(status_code=400, detail="spreadsheet_id is required.") rows = read_sheet(spreadsheet_id, range_name, creds=require_credentials()) return { "spreadsheet_id": spreadsheet_id, "range": range_name, "row_count": len(rows), "values": rows, }
@app.post("/sheets/write")def sheets_write(payload: SheetWriteRequest): if not payload.spreadsheet_id: raise HTTPException(status_code=400, detail="spreadsheet_id is required.") result = write_sheet( payload.spreadsheet_id, payload.range_name, payload.values, creds=require_credentials(), ) return { "status": "written", "updated_range": result.get("updatedRange"), "updated_rows": result.get("updatedRows"), "updated_columns": result.get("updatedColumns"), "updated_cells": result.get("updatedCells"), }
@app.post("/sheets/append")def sheets_append(payload: SheetWriteRequest): if not payload.spreadsheet_id: raise HTTPException(status_code=400, detail="spreadsheet_id is required.") result = append_sheet( payload.spreadsheet_id, payload.range_name, payload.values, creds=require_credentials(), ) updates = result.get("updates", {}) return { "status": "appended", "updated_range": updates.get("updatedRange"), "updated_rows": updates.get("updatedRows"), "updated_cells": updates.get("updatedCells"), }
@app.post("/sheets/clear")def sheets_clear(payload: SheetClearRequest): if not payload.spreadsheet_id: raise HTTPException(status_code=400, detail="spreadsheet_id is required.") result = clear_sheet( payload.spreadsheet_id, payload.range_name, creds=require_credentials(), ) return { "status": "cleared", "cleared_range": result.get("clearedRange"), "spreadsheet_id": result.get("spreadsheetId"), }
# ============================================================================# JOB STATUS ROUTES# ============================================================================
@app.get("/status/{job_id}")def get_status(job_id: str): with _jobs_lock: job = _jobs.get(job_id) if job is None: raise HTTPException(status_code=404, detail=f"Job '{job_id}' not found.") return job
@app.get("/jobs")def list_jobs(): with _jobs_lock: return { "total": len(_jobs), "jobs": [ { "job_id": j["job_id"], "status": j["status"], "youtube_url": j["youtube_url"], "started_at": j["started_at"], "completed_at": j["completed_at"], } for j in _jobs.values() ], }
# ============================================================================# PIPELINE BACKGROUND WORKER# ============================================================================
def _upload_with_title( local_file: Path, drive_name: str, step_key: str, job_id: str, folder_id: str, creds: Credentials,) -> dict: """Copy file with video-title name, upload to Drive, clean up.""" _set_step(job_id, step_key, "running") try: tmp_path = local_file.parent / drive_name if tmp_path.exists(): tmp_path.unlink() shutil.copy2(local_file, tmp_path) result = upload_file_to_drive( str(tmp_path), creds=creds, folder_id=folder_id, make_public=True, ) tmp_path.unlink() _set_step(job_id, step_key, "done") return result except Exception as exc: _set_step(job_id, step_key, "failed") return {"error": str(exc)}
def _run_pipeline(job_id: str, youtube_url: str, email_to: str): creds = load_credentials() timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
# Create initial sheet row immediately _create_sheet_record( job_id=job_id, timestamp=timestamp, youtube_url=youtube_url, email_to=email_to, creds=creds, )
try: # ββ STEP 1: Fetch transcript βββββββββββββββββββββββββββββββββββββ _update_job(job_id, status="fetching_transcript") _set_step(job_id, "fetch_transcript", "running") _update_sheet_record(job_id, creds, status="fetching_transcript")
try: pipeline = TranscriptSummaryPipeline( youtube_url=youtube_url, languages=["en", "en-US", "en-GB"], ) transcript = pipeline.fetcher.run() _set_step(job_id, "fetch_transcript", "done") _update_sheet_record( job_id, creds, video_title=pipeline.video_title, status="transcript_ready", ) except Exception as exc: _set_step(job_id, "fetch_transcript", "failed") raise RuntimeError(f"Transcript fetch failed: {exc}")
video_title = pipeline.video_title
# ββ STEP 2: Summarize ββββββββββββββββββββββββββββββββββββββββββββ _update_job(job_id, status="summarizing") _set_step(job_id, "summarize", "running") _update_sheet_record(job_id, creds, status="summarizing")
try: summary, qa, model_used = pipeline.summarizer.run(transcript) _set_step(job_id, "summarize", "done") _update_sheet_record( job_id, creds, model_used=model_used, status="summarized", ) except Exception as exc: _set_step(job_id, "summarize", "failed") raise RuntimeError(f"Summarization failed: {exc}")
# ββ STEP 3: Create Drive folder ββββββββββββββββββββββββββββββββββ _update_job(job_id, status="creating_drive_folder") _set_step(job_id, "create_drive_folder", "running") _update_sheet_record(job_id, creds, status="creating_drive_folder")
try: folder_id = create_drive_folder(video_title, creds=creds) _set_step(job_id, "create_drive_folder", "done") except Exception as exc: _set_step(job_id, "create_drive_folder", "failed") raise RuntimeError(f"Drive folder creation failed: {exc}")
# ββ STEP 4β6: Upload files βββββββββββββββββββββββββββββββββββββββ _update_job(job_id, status="uploading_drive") _update_sheet_record(job_id, creds, status="uploading_to_drive")
summary_drive = _upload_with_title( SUMMARY_FILE, f"{video_title}__summary.txt", "upload_summary", job_id, folder_id, creds, ) qa_drive = _upload_with_title( QA_FILE, f"{video_title}__qa.txt", "upload_qa", job_id, folder_id, creds, ) transcript_drive = _upload_with_title( TRANSCRIPT_FILE, f"{video_title}__transcript.txt", "upload_transcript", job_id, folder_id, creds, )
summary_link = summary_drive.get("direct_download_link", "N/A") qa_link = qa_drive.get("direct_download_link", "N/A") transcript_link = transcript_drive.get("direct_download_link", "N/A")
_update_sheet_record( job_id, creds, status="drive_uploaded", summary_link=summary_link, qa_link=qa_link, transcript_link=transcript_link, )
# ββ STEP 7: Send email βββββββββββββββββββββββββββββββββββββββββββ _update_job(job_id, status="sending_email") _set_step(job_id, "send_email", "running") _update_sheet_record(job_id, creds, status="sending_email")
email_subject = f"β
YouTube Summary Ready β {video_title}" email_body = f"""Hello,
Your YouTube video has been processed successfully.
π₯ Title : {video_title}π Video URL : {youtube_url}
π Summary : {summary_link}β Q&A : {qa_link}π Transcript : {transcript_link}
ββββββββββββββββββββββββββββββββModel Used : {model_used}ββββββββββββββββββββββββββββββββ
Regards,Google Integration API""" try: email_result = send_email( to=email_to, subject=email_subject, body=email_body, creds=creds, ) email_status = "sent" email_msg_id = email_result.get("id", "") _set_step(job_id, "send_email", "done") except Exception as exc: email_status = f"failed: {exc}" email_msg_id = "" _set_step(job_id, "send_email", "failed")
# ββ STEP 8: Final sheet update βββββββββββββββββββββββββββββββββββ _update_job(job_id, status="logging_sheet") _set_step(job_id, "log_sheet", "running")
completed_at = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
try: _update_sheet_record( job_id, creds, status="completed", email_status=email_status, email_msg_id=email_msg_id, completed_at=completed_at, ) _set_step(job_id, "log_sheet", "done") sheets_status = "logged" except Exception as exc: sheets_status = f"failed: {exc}" _set_step(job_id, "log_sheet", "failed")
# ββ COMPLETE βββββββββββββββββββββββββββββββββββββββββββββββββββββ _update_job( job_id, status="completed", completed_at=completed_at, result={ "video_title": video_title, "youtube_url": youtube_url, "model_used": model_used, "drive": { "folder_id": folder_id, "summary": { "web_view_link": summary_drive.get("webViewLink"), "direct_download_link": summary_link, }, "qa": { "web_view_link": qa_drive.get("webViewLink"), "direct_download_link": qa_link, }, "transcript": { "web_view_link": transcript_drive.get("webViewLink"), "direct_download_link": transcript_link, }, }, "email": {"status": email_status, "message_id": email_msg_id}, "sheets": {"status": sheets_status}, }, )
except Exception as exc: completed_at = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC") _update_job( job_id, status="failed", completed_at=completed_at, error=str(exc), ) _update_sheet_record( job_id, creds, status="failed", completed_at=completed_at, error=str(exc), )
# ============================================================================# GENERATE ROUTE# ============================================================================
@app.post("/generate")def generate(payload: GenerateRequest): """ Kick off full pipeline in background thread. Returns job_id immediately β poll GET /status/{job_id} for progress. """ require_credentials()
job_id = secrets.token_hex(8) _new_job(job_id, payload.youtube_url, payload.email_to)
thread = threading.Thread( target=_run_pipeline, args=(job_id, payload.youtube_url, payload.email_to), daemon=True, ) thread.start()
return { "job_id": job_id, "status": "initiated", "poll_url": f"/status/{job_id}", "started_at": _jobs[job_id]["started_at"], }
# ============================================================================# RUN# ============================================================================
if __name__ == "__main__": import uvicorn uvicorn.run("fastapi_app:app", host="0.0.0.0", port=8000, reload=True)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445#!/usr/bin/env python3
from __future__ import annotations
import reimport sysimport jsonimport loggingimport time
from pathlib import Pathfrom typing import Optional, Listfrom urllib.parse import urlparse, parse_qs# from google import genai
from youtube_transcript_api import ( YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound, VideoUnavailable,)
# ============================================================================# CONFIG# ============================================================================
BASE_DIR = Path(__file__).resolve().parentOUTPUT_DIR = BASE_DIR / "output"OUTPUT_DIR.mkdir(exist_ok=True)
TRANSCRIPT_FILE = OUTPUT_DIR / "transcript.txt"SUMMARY_FILE = OUTPUT_DIR / "summary.txt"QA_FILE = OUTPUT_DIR / "qa.txt"
GEMINI_API_KEY = "AIzaSyCNz5wQAyJ65kNRkwr0-1A-_Z6-lQzdcyc"
GEMINI_MODELS = [ "gemini-2.5-flash", "gemini-2.5-flash-lite", "gemini-2.5-pro",]
POLLING_CONFIG = { "attempt_1": {"wait_before": 0, "description": "Immediate attempt on trigger"}, "attempt_2": {"wait_before": 300, "description": "Retry after 5 minutes"}, "attempt_3": {"wait_before": 900, "description": "Retry after 15 minutes"}, "attempt_4": {"wait_before": 1800, "description": "Retry after 30 minutes"}, "attempt_5": {"wait_before": 3600, "description": "Retry after 1 hour"}, "attempt_6": {"wait_before": 3600, "description": "Retry after 2 hours total"}, "attempt_7": {"wait_before": 3600, "description": "Retry after 3 hours total"}, "attempt_8": {"wait_before": 3600, "description": "Retry after 4 hours total"}, "attempt_9": {"wait_before": 3600, "description": "Final attempt at 5 hours total"},}
SYSTEM_PROMPT = """You are an expert content summarizer and educator.
Produce the full output containing exactly two parts separated by a line with only 5 exclamation marks:
!!!!!
--- PART 1: SUMMARY ---
Write a detailed, well-structured summary of the entire content.Use the following structure:
## OverviewA 3-5 sentence high-level overview of the entire content.
## Key Topics CoveredList the main topics discussed, each with a brief explanation.
## Detailed SummaryA thorough section-by-section breakdown of the content in the order it was presented.Use subheadings for each major section or topic shift.
## Key TakeawaysA bullet list of the most important insights, facts, or conclusions from the content.
---
!!!!!
--- PART 2: Q&A ---
Generate a comprehensive Q&A section based on the content.Format each entry exactly like this:
Q1: [First question]Answer: [Detailed answer]
Q2: [Second question]Answer: [Detailed answer]
Q3: [Third question]Answer: [Detailed answer]
... and so on until all important questions are covered.
Rules:- Number every question and answer with matching numbers (Q1/A1, Q2/A2, etc.)- Each answer must be detailed and self-contained- Cover all major topics, concepts, facts, and insights from the content- Minimum 10 Q&A pairs, more if the content is rich- Do NOT use bullet points inside answers β write in full sentences
---"""
# ============================================================================# LOGGING# ============================================================================
logging.basicConfig( level=logging.INFO, format="%(asctime)s | %(levelname)s | %(message)s",)logger = logging.getLogger("gemini_pipeline")
# ============================================================================# HELPERS# ============================================================================
def _format_duration(seconds: int) -> str: if seconds < 60: return f"{seconds}s" if seconds < 3600: return f"{seconds // 60}m" h = seconds // 3600 m = (seconds % 3600) // 60 return f"{h}h {m}m" if m else f"{h}h"
def fetch_video_title(video_id: str) -> str: """Fetch YouTube video title via oembed β no API key needed.""" try: import urllib.request url = ( f"https://www.youtube.com/oembed" f"?url=https://www.youtube.com/watch?v={video_id}&format=json" ) with urllib.request.urlopen(url, timeout=10) as resp: data = json.loads(resp.read().decode()) title = data.get("title", "") safe = re.sub(r'[\\/*?:"<>|]', "", title) safe = re.sub(r"\s+", "_", safe.strip()) return safe[:80] or video_id except Exception: return video_id
# ============================================================================# YOUTUBE TRANSCRIPT FETCHER# ============================================================================
class YouTubeTranscriptFetcher: """Fetches YouTube transcript with polling retry for new uploads."""
def __init__( self, youtube_url: str, output_file: Path = TRANSCRIPT_FILE, languages: Optional[List[str]] = None, polling_config: dict = None, ): self.youtube_url = youtube_url self.output_file = Path(output_file) self.languages = languages or ["en", "en-US", "en-GB"] self.polling_config = polling_config or POLLING_CONFIG self.video_id = self._extract_video_id(youtube_url) self.api = YouTubeTranscriptApi()
@staticmethod def _extract_video_id(url: str) -> str: parsed = urlparse(url) if parsed.hostname == "youtu.be": return parsed.path.lstrip("/").split("?")[0] if parsed.hostname in ("youtube.com", "www.youtube.com", "m.youtube.com"): path_parts = parsed.path.strip("/").split("/") if path_parts[0] in ("live", "shorts", "embed") and len(path_parts) >= 2: return path_parts[1].split("?")[0] params = parse_qs(parsed.query) if "v" in params: return params["v"][0] raise ValueError(f"Could not extract video ID from URL: {url}") raise ValueError(f"Unsupported YouTube URL: {url}")
def _fetch_once(self) -> str: transcript = self.api.fetch(self.video_id, languages=self.languages) return " ".join(item.text for item in transcript)
def _save(self, text: str) -> None: self.output_file.parent.mkdir(parents=True, exist_ok=True) self.output_file.write_text(text, encoding="utf-8")
def run(self) -> str: logger.info("Video ID : %s", self.video_id) logger.info("Output file : %s", self.output_file) logger.info("Total polling attempts: %d", len(self.polling_config))
attempts = list(self.polling_config.items())
for idx, (attempt_key, config) in enumerate(attempts, start=1): wait_before = config["wait_before"] description = config["description"]
if wait_before > 0: logger.info( "[%d/%d] %s β waiting %s before retry...", idx, len(attempts), description, _format_duration(wait_before), ) time.sleep(wait_before)
logger.info( "[%d/%d] %s β fetching transcript now...", idx, len(attempts), description, )
try: text = self._fetch_once() self._save(text) logger.info( "[%d/%d] β
Transcript fetched β %d characters", idx, len(attempts), len(text), ) return text
except TranscriptsDisabled as e: logger.warning("[%d/%d] Transcripts disabled: %s", idx, len(attempts), e) raise # no point retrying
except VideoUnavailable as e: logger.warning("[%d/%d] Video unavailable: %s", idx, len(attempts), e)
except NoTranscriptFound as e: logger.warning("[%d/%d] No transcript yet: %s", idx, len(attempts), e)
except KeyboardInterrupt: logger.warning("Interrupted by user.") raise
except Exception as e: logger.exception("[%d/%d] Unexpected error: %s", idx, len(attempts), e)
if idx < len(attempts): next_cfg = attempts[idx][1] logger.info( "[%d/%d] Will retry in %s (%s)", idx, len(attempts), _format_duration(next_cfg["wait_before"]), next_cfg["description"], ) else: logger.error("All %d polling attempts exhausted.", len(attempts))
raise RuntimeError( f"Transcript not available after {len(attempts)} attempts (~5 hours). " f"Video ID: {self.video_id}" )
# ============================================================================# GEMINI SUMMARIZER# ============================================================================
class GeminiSummarizer: """Sends transcript to Gemini with model fallback + per-model retry."""
# Retry config MAX_RETRIES = 5 BASE_WAIT = 10 # seconds MAX_WAIT = 120 # seconds cap
# Errors β retry same model with backoff RETRYABLE = ["503", "502", "500", "UNAVAILABLE", "SERVICE_UNAVAILABLE"] # Errors β skip to next model immediately SKIP_TO_NEXT = ["429", "RESOURCE_EXHAUSTED", "quota", "404", "NOT_FOUND"]
def __init__( self, api_key: str = GEMINI_API_KEY, models: list = None, summary_file: Path = SUMMARY_FILE, qa_file: Path = QA_FILE, ): self.client = genai.Client(api_key=api_key) self.models = models or GEMINI_MODELS self.summary_file = Path(summary_file) self.qa_file = Path(qa_file)
def _call_api(self, transcript: str) -> tuple[str, str]: """ Try each model in order. Per model: retry up to MAX_RETRIES on transient errors with backoff. Returns (response_text, model_used). """ overall_last_error = None
for model in self.models: logger.info("ββ Trying model: %s", model) wait = self.BASE_WAIT last_err = None
for attempt in range(1, self.MAX_RETRIES + 1): try: logger.info(" [%d/%d] Sending request...", attempt, self.MAX_RETRIES) response = self.client.models.generate_content( model=model, contents=transcript, config={"system_instruction": SYSTEM_PROMPT}, ) logger.info( "β
Response received from: %s (attempt %d)", model, attempt, ) return response.text, model
except Exception as e: err = str(e) last_err = e
if any(k in err for k in self.SKIP_TO_NEXT): logger.warning( " [%d/%d] %s β quota/not-found, skipping to next model.", attempt, self.MAX_RETRIES, model, ) break # skip to next model
elif any(k in err for k in self.RETRYABLE): if attempt < self.MAX_RETRIES: logger.warning( " [%d/%d] %s β transient error. " "Retrying in %ds...", attempt, self.MAX_RETRIES, model, wait, ) time.sleep(wait) wait = min(wait * 2, self.MAX_WAIT) else: logger.warning( " [%d/%d] %s β max retries reached, " "trying next model.", attempt, self.MAX_RETRIES, model, )
else: logger.error( " [%d/%d] %s β unhandled error: %s", attempt, self.MAX_RETRIES, model, err, ) raise
overall_last_error = last_err
raise RuntimeError( f"All models and retries exhausted. Last error: {overall_last_error}" )
@staticmethod def _split(full_text: str) -> tuple[str, str]: for pattern in (r"^\s*!{5}\s*$", r"^\s*!{3}\s*$"): parts = re.split(pattern, full_text, flags=re.MULTILINE) if len(parts) >= 2: return parts[0].strip(), "".join(parts[1:]).strip() return full_text.strip(), ""
def run(self, transcript: str) -> tuple[str, str, str]: full, model_used = self._call_api(transcript) summary, qa = self._split(full)
self.summary_file.write_text(summary, encoding="utf-8") self.qa_file.write_text(qa, encoding="utf-8")
logger.info("Summary saved β %s", self.summary_file) logger.info("Q&A saved β %s", self.qa_file)
return summary, qa, model_used
# ============================================================================# PIPELINE# ============================================================================
class TranscriptSummaryPipeline:
def __init__( self, youtube_url: str, languages: Optional[List[str]] = None, polling_config: dict = None, ): self.youtube_url = youtube_url self.fetcher = YouTubeTranscriptFetcher( youtube_url=youtube_url, output_file=TRANSCRIPT_FILE, languages=languages, polling_config=polling_config, ) self.summarizer = GeminiSummarizer() self.video_id = self.fetcher.video_id self.video_title = fetch_video_title(self.video_id)
def run(self) -> dict: logger.info("=== Pipeline started ===") logger.info("Video title : %s", self.video_title)
transcript = self.fetcher.run() summary, qa, model = self.summarizer.run(transcript)
logger.info("=== Pipeline complete | model: %s ===", model)
return { "video_id": self.video_id, "video_title": self.video_title, "model_used": model, "summary": summary, "qa": qa, "transcript": transcript, }
# ============================================================================# CLI# ============================================================================
def main(): if len(sys.argv) < 2: print("Usage: python gemini.py <youtube_url>", file=sys.stderr) sys.exit(1)
pipeline = TranscriptSummaryPipeline( youtube_url=sys.argv[1], languages=["en", "en-US", "en-GB"], ) result = pipeline.run() for key, value in result.items(): if key not in ("summary", "qa", "transcript"): print(f"{key}: {value}")
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>YT Summariser</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@tabler/icons-webfont@latest/tabler-icons.min.css" /> <style> *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; } body { font-family: system-ui, sans-serif; background: #f9f9f8; color: #1a1a18; min-height: 100vh; display: flex; align-items: center; justify-content: center; } .wrap { width: 100%; max-width: 460px; padding: 2.5rem 1.5rem; }
/* ββ Logo ββ */ .logo { display: flex; align-items: center; gap: 10px; margin-bottom: 2.5rem; } .logo-icon { width: 36px; height: 36px; background: #E24B4A; border-radius: 8px; display: flex; align-items: center; justify-content: center; } .logo-icon i { color: #fff; font-size: 18px; } .logo-text { font-size: 15px; font-weight: 600; } .logo-sub { font-size: 12px; color: #888; }
/* ββ Auth banner ββ */ .auth-banner { display: flex; align-items: center; justify-content: space-between; padding: 10px 14px; border-radius: 8px; margin-bottom: 1.5rem; font-size: 13px; border: 1px solid #ddd; background: #fff; } .auth-banner.ok { border-color: #86efac; background: #f0fdf4; } .auth-banner.bad { border-color: #fca5a5; background: #fff5f5; } .auth-left { display: flex; align-items: center; gap: 8px; } .auth-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; } .auth-dot.ok { background: #22c55e; } .auth-dot.bad { background: #ef4444; } .auth-text { color: #444; } .auth-btn { font-size: 12px; font-weight: 600; padding: 5px 12px; border-radius: 6px; border: none; cursor: pointer; background: #1a1a18; color: #fff; text-decoration: none; display: inline-flex; align-items: center; gap: 5px; transition: opacity .15s; } .auth-btn:hover { opacity: .8; } .auth-btn.connected { background: #dcfce7; color: #166534; cursor: default; }
/* ββ Heading ββ */ h1 { font-size: 22px; font-weight: 600; margin-bottom: 6px; } .subtitle { font-size: 14px; color: #666; margin-bottom: 2rem; line-height: 1.6; }
/* ββ Form ββ */ .field { margin-bottom: 1rem; } label { display: block; font-size: 13px; color: #555; margin-bottom: 5px; } input[type=text], input[type=email] { width: 100%; padding: 9px 12px; font-size: 14px; border: 1px solid #ddd; border-radius: 8px; background: #fff; outline: none; transition: border-color .15s; } input:focus { border-color: #E24B4A; }
/* ββ Buttons ββ */ .btn { width: 100%; padding: 10px; font-size: 14px; font-weight: 600; cursor: pointer; border-radius: 8px; border: none; background: #E24B4A; color: #fff; margin-top: .5rem; display: flex; align-items: center; justify-content: center; gap: 8px; transition: opacity .15s; } .btn:hover { opacity: .88; } .btn:disabled { opacity: .5; cursor: not-allowed; }
/* ββ Error ββ */ .err { font-size: 13px; color: #a32d2d; margin-top: 1rem; display: none; padding: 10px 12px; border: 1px solid #f09595; border-radius: 8px; background: #fcebeb; }
/* ββ Status box ββ */ .status-box { margin-top: 1.5rem; border: 1px solid #e5e5e3; border-radius: 12px; overflow: hidden; display: none; background: #fff; } .status-header { padding: 12px 16px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid #e5e5e3; } .status-label { font-size: 13px; font-weight: 600; } .badge { font-size: 11px; padding: 3px 8px; border-radius: 20px; font-weight: 600; } .badge-running { background: #dbeafe; color: #1e40af; } .badge-done { background: #dcfce7; color: #166534; } .badge-failed { background: #fee2e2; color: #991b1b; }
/* ββ Steps ββ */ .steps { padding: 12px 16px; display: flex; flex-direction: column; gap: 9px; } .step { display: flex; align-items: center; gap: 10px; font-size: 13px; color: #999; } .step.active { color: #1a1a18; } .step-icon { width: 20px; height: 20px; border-radius: 50%; display: flex; align-items: center; justify-content: center; flex-shrink: 0; font-size: 11px; border: 1px solid #ddd; background: #f5f5f4; } .step-icon.done { background: #dcfce7; border-color: #86efac; color: #166534; } .step-icon.running { background: #dbeafe; border-color: #93c5fd; color: #1e40af; } .step-icon.failed { background: #fee2e2; border-color: #fca5a5; color: #991b1b; }
/* ββ Result links ββ */ .result-box { padding: 12px 16px; border-top: 1px solid #e5e5e3; display: none; flex-direction: column; gap: 8px; } .result-link { display: flex; align-items: center; gap: 8px; font-size: 13px; } .result-link a { color: #1e40af; text-decoration: none; } .result-link a:hover { text-decoration: underline; } .result-note { font-size: 12px; color: #888; margin-top: 2px; }
/* ββ Spinner ββ */ .spinner { width: 11px; height: 11px; border: 1.5px solid currentColor; border-top-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; display: inline-block; } @keyframes spin { to { transform: rotate(360deg); } } </style></head><body><div class="wrap">
<!-- Logo --> <div class="logo"> <div class="logo-icon"><i class="ti ti-brand-youtube"></i></div> <div> <div class="logo-text">YT Summariser</div> <div class="logo-sub">Transcript β Summary β Q&A</div> </div> </div>
<!-- Auth banner --> <div class="auth-banner bad" id="auth-banner"> <div class="auth-left"> <div class="auth-dot bad" id="auth-dot"></div> <span class="auth-text" id="auth-text">Google not connected</span> </div> <a href="/auth/start" class="auth-btn" id="auth-btn" target="_blank" onclick="onAuthClick()"> <i class="ti ti-brand-google"></i> Connect Google </a> </div>
<!-- Heading --> <h1>Summarise a YouTube video</h1> <p class="subtitle">Paste a YouTube link and your email β we'll process the transcript and send results to your inbox.</p>
<!-- Form --> <div class="field"> <label for="yt-url">YouTube URL</label> <input type="text" id="yt-url" placeholder="https://www.youtube.com/watch?v=..." /> </div> <div class="field"> <label for="email">Email address</label> <input type="email" id="email" placeholder="you@example.com" /> </div>
<button class="btn" id="submit-btn" onclick="submitJob()"> <i class="ti ti-player-play"></i> Start processing </button>
<div class="err" id="err-box"></div>
<!-- Status box --> <div class="status-box" id="status-box"> <div class="status-header"> <span class="status-label" id="status-label">Processingβ¦</span> <span class="badge badge-running" id="status-badge">Running</span> </div> <div class="steps" id="steps-list"></div> <div class="result-box" id="result-box"></div> </div>
</div>
<script> /* ββ Step config ββ */ const STEP_LABELS = { fetch_transcript: 'Fetching transcript', summarize: 'Summarising with Gemini', create_drive_folder: 'Creating Drive folder', upload_summary: 'Uploading summary', upload_qa: 'Uploading Q&A', upload_transcript: 'Uploading transcript', send_email: 'Sending email', log_sheet: 'Logging to Sheets', }; const STEP_ICONS = { fetch_transcript: 'ti-file-text', summarize: 'ti-brain', create_drive_folder: 'ti-folder-plus', upload_summary: 'ti-upload', upload_qa: 'ti-upload', upload_transcript: 'ti-upload', send_email: 'ti-mail', log_sheet: 'ti-table', };
let pollTimer = null;
/* ββ Auth status check ββ */ async function checkAuth() { try { const res = await fetch('/auth/status'); const data = await res.json(); setAuthBanner(data.authenticated); } catch (e) { setAuthBanner(false); } }
function setAuthBanner(ok) { const banner = document.getElementById('auth-banner'); const dot = document.getElementById('auth-dot'); const text = document.getElementById('auth-text'); const btn = document.getElementById('auth-btn');
if (ok) { banner.className = 'auth-banner ok'; dot.className = 'auth-dot ok'; text.textContent = 'Google configured'; btn.className = 'auth-btn connected'; btn.innerHTML = '<i class="ti ti-circle-check"></i> Connected'; btn.removeAttribute('href'); btn.onclick = null; } else { banner.className = 'auth-banner bad'; dot.className = 'auth-dot bad'; text.textContent = 'Google not connected'; btn.className = 'auth-btn'; btn.innerHTML = '<i class="ti ti-brand-google"></i> Connect Google'; btn.href = '/auth/start'; btn.onclick = onAuthClick; } }
function onAuthClick() { // After OAuth window closes, recheck setTimeout(() => { const check = setInterval(async () => { const res = await fetch('/auth/status'); const data = await res.json(); if (data.authenticated) { clearInterval(check); setAuthBanner(true); } }, 2000); // Stop checking after 2 min setTimeout(() => clearInterval(check), 120000); }, 3000); }
/* ββ Helpers ββ */ function showErr(msg) { const b = document.getElementById('err-box'); b.textContent = msg; b.style.display = 'block'; } function clearErr() { document.getElementById('err-box').style.display = 'none'; }
/* ββ Submit ββ */ async function submitJob() { clearErr(); const url = document.getElementById('yt-url').value.trim(); const email = document.getElementById('email').value.trim(); if (!url) { showErr('Please enter a YouTube URL.'); return; } if (!email) { showErr('Please enter an email address.'); return; }
const btn = document.getElementById('submit-btn'); btn.disabled = true; btn.innerHTML = '<span class="spinner"></span> Startingβ¦';
try { const res = await fetch('/generate', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ youtube_url: url, email_to: email }), }); const data = await res.json(); if (!res.ok) { showErr(data.detail || 'Request failed.'); btn.disabled = false; btn.innerHTML = '<i class="ti ti-player-play"></i> Start processing'; return; } startPolling(data.job_id); } catch (e) { showErr('Could not reach the server. Is it running on localhost:8000?'); btn.disabled = false; btn.innerHTML = '<i class="ti ti-player-play"></i> Start processing'; } }
/* ββ Polling ββ */ function startPolling(jobId) { document.getElementById('status-box').style.display = 'block'; renderSteps({}); pollTimer = setInterval(() => poll(jobId), 2500); }
async function poll(jobId) { try { const res = await fetch('/status/' + jobId); const data = await res.json(); renderSteps(data.steps || {}); updateBadge(data.status);
const running = Object.entries(data.steps || {}).find(([, v]) => v === 'running'); if (running) document.getElementById('status-label').textContent = STEP_LABELS[running[0]] + 'β¦';
if (data.status === 'completed') { clearInterval(pollTimer); document.getElementById('status-label').textContent = 'Done!'; showResult(data.result); const btn = document.getElementById('submit-btn'); btn.disabled = false; btn.innerHTML = '<i class="ti ti-player-play"></i> Process another'; } else if (data.status === 'failed') { clearInterval(pollTimer); document.getElementById('status-label').textContent = 'Failed'; showErr('Pipeline failed: ' + (data.error || 'unknown error')); const btn = document.getElementById('submit-btn'); btn.disabled = false; btn.innerHTML = '<i class="ti ti-player-play"></i> Try again'; } } catch (e) {} }
/* ββ Render steps ββ */ function renderSteps(steps) { const list = document.getElementById('steps-list'); list.innerHTML = Object.entries(STEP_LABELS).map(([key, label]) => { const state = steps[key] || 'pending'; const iconCls = STEP_ICONS[key] || 'ti-circle'; let inner = ''; if (state === 'done') inner = '<i class="ti ti-check"></i>'; else if (state === 'running') inner = '<span class="spinner"></span>'; else if (state === 'failed') inner = '<i class="ti ti-x"></i>'; return `<div class="step ${state !== 'pending' ? 'active' : ''}"> <div class="step-icon ${state}">${inner}</div> <i class="ti ${iconCls}" style="font-size:14px;color:#aaa"></i> <span>${label}</span> </div>`; }).join(''); }
function updateBadge(status) { const b = document.getElementById('status-badge'); b.className = 'badge'; if (status === 'completed') { b.classList.add('badge-done'); b.textContent = 'Completed'; } else if (status === 'failed') { b.classList.add('badge-failed'); b.textContent = 'Failed'; } else { b.classList.add('badge-running'); b.textContent = 'Running'; } }
/* ββ Show result links ββ */ function showResult(result) { if (!result) return; const box = document.getElementById('result-box'); box.style.display = 'flex'; const drive = result.drive || {}; const links = [ ['ti-file-text', 'Summary', drive.summary?.web_view_link], ['ti-help-circle', 'Q&A', drive.qa?.web_view_link], ['ti-align-left', 'Transcript', drive.transcript?.web_view_link], ]; box.innerHTML = links .filter(([,, u]) => u) .map(([icon, label, u]) => ` <div class="result-link"> <i class="ti ${icon}" style="font-size:15px;color:#888"></i> <a href="${u}" target="_blank">${label} <i class="ti ti-external-link" style="font-size:11px"></i></a> </div>`) .join('') + `<p class="result-note"><i class="ti ti-mail" style="font-size:13px;vertical-align:-2px"></i> Results also sent to your email</p>`; }
/* ββ Init ββ */ checkAuth();</script></body></html>1234567fastapiuvicorn[standard]google-api-python-clientgoogle-auth-httplib2google-auth-oauthlibrequestsyoutube_transcript_api.gitignoreAdded+220
client_secret.jsonAdded+12
fastapi_app.pyAdded+1138
gemini_transcript.pyAdded+445
index.htmlAdded+378
requirements.txtAdded+7