Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from fastapi import FastAPI | |
| from youtube_transcript_api import YouTubeTranscriptApi | |
| from youtube_transcript_api.proxies import WebshareProxyConfig | |
| # Initialize FastAPI app | |
| app = FastAPI() | |
| # Initialize the YouTubeTranscriptApi with proxy configuration | |
| ytt_api = YouTubeTranscriptApi( | |
| proxy_config=WebshareProxyConfig( | |
| proxy_username="tlaukrdr", # Replace with your proxy username | |
| proxy_password="mc1aumn9xbhb" # Replace with your proxy password | |
| ) | |
| ) | |
| # Function to fetch and format YouTube transcript using the video ID | |
| def fetch_transcript(video_id: str): | |
| try: | |
| transcript_obj = ytt_api.fetch(video_id) | |
| # Extract and join only the text fields from FetchedTranscriptSnippet objects | |
| 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 Interface for API (no UI) | |
| iface = gr.Interface( | |
| fn=fetch_transcript, | |
| inputs=gr.Textbox(label="Enter YouTube Video ID"), | |
| outputs=gr.Textbox(label="Transcript"), | |
| live=False, | |
| api=True # This flag turns the interface into an API | |
| ) | |
| # FastAPI route to serve Gradio app | |
| def read_root(): | |
| return {"message": "Welcome to the YouTube Transcript API!"} | |
| # Embed the Gradio interface as a FastAPI app | |
| iface.mount(app) | |
| # Launch the FastAPI app | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run(app, host="0.0.0.0", port=8000) | |