Spaces:
Running
Running
| import gradio as gr | |
| from fastapi import FastAPI, Request | |
| from youtube_transcript_api import YouTubeTranscriptApi | |
| from youtube_transcript_api.proxies import WebshareProxyConfig | |
| from pydantic import BaseModel | |
| # FastAPI app | |
| app = FastAPI() | |
| # Initialize YouTubeTranscriptApi with proxy | |
| ytt_api = YouTubeTranscriptApi( | |
| proxy_config=WebshareProxyConfig( | |
| proxy_username="tlaukrdr", # Replace with real credentials | |
| proxy_password="mc1aumn9xbhb" | |
| ) | |
| ) | |
| # API input schema | |
| class VideoIDInput(BaseModel): | |
| video_id: str | |
| # API Endpoint | |
| async def get_transcript(payload: VideoIDInput): | |
| try: | |
| transcript_obj = ytt_api.fetch(payload.video_id) | |
| full_text = " ".join([snippet.text for snippet in transcript_obj.snippets]) | |
| return {"transcript": full_text} | |
| except Exception as e: | |
| return {"error": str(e)}, 500 | |
| # Gradio function | |
| def fetch_transcript(video_id: str): | |
| try: | |
| transcript_obj = ytt_api.fetch(video_id) | |
| full_text = " ".join([snippet.text for snippet in transcript_obj.snippets]) | |
| return full_text | |
| except Exception as e: | |
| return f"Error fetching transcript: {str(e)}" | |
| # Gradio UI | |
| demo = gr.Interface( | |
| fn=fetch_transcript, | |
| inputs=gr.Textbox(label="Enter YouTube Video ID"), | |
| outputs=gr.Textbox(label="Transcript"), | |
| live=True, | |
| title="YouTube Transcript Fetcher" | |
| ) | |
| # Mount Gradio at root | |
| app = gr.mount_gradio_app(app, demo, path="/") | |
| # Run Locally | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run(app, host="0.0.0.0", port=7860) | |