Spaces:
Running
Running
File size: 1,551 Bytes
c6f2536 aa1df32 5b9992a adb03d1 7539cb9 c6f2536 aa1df32 adb03d1 28868d4 3befc55 adb03d1 c6f2536 ab498f8 3befc55 c6f2536 adb03d1 5b9992a 7539cb9 5b9992a adb03d1 5b9992a 7539cb9 5b9992a c6f2536 aa1df32 7539cb9 dcdeeb3 aa1df32 7539cb9 aa1df32 |
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 |
import gradio as gr
from fastapi import FastAPI
from youtube_transcript_api import YouTubeTranscriptApi
from youtube_transcript_api.proxies import WebshareProxyConfig
import uvicorn
# 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 (no need for api=True, directly mount Gradio to FastAPI)
iface = gr.Interface(
fn=fetch_transcript,
inputs=gr.Textbox(label="Enter YouTube Video ID"),
outputs=gr.Textbox(label="Transcript"),
live=False
)
# FastAPI route to serve Gradio app
@app.get("/")
def read_root():
return {"message": "Welcome to the YouTube Transcript API!"}
# Mount Gradio interface to FastAPI app
iface.launch(server_name="0.0.0.0", server_port=8000, ssr_mode=False)
# Launch the FastAPI app using Uvicorn
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
|