Rivalcoder commited on
Commit
466968e
·
1 Parent(s): 0df7414
Files changed (2) hide show
  1. app.py +34 -12
  2. requirements.txt +4 -1
app.py CHANGED
@@ -1,34 +1,56 @@
1
  import gradio as gr
 
2
  from youtube_transcript_api import YouTubeTranscriptApi
3
  from youtube_transcript_api.proxies import WebshareProxyConfig
 
4
 
5
- # Initialize the YouTubeTranscriptApi with proxy configuration
 
 
 
6
  ytt_api = YouTubeTranscriptApi(
7
  proxy_config=WebshareProxyConfig(
8
- proxy_username="tlaukrdr", # Replace with your proxy username
9
- proxy_password="mc1aumn9xbhb" # Replace with your proxy password
10
  )
11
  )
12
 
13
- # Function to fetch and format YouTube transcript using the video ID
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def fetch_transcript(video_id: str):
15
  try:
16
- # Fetch transcript
17
  transcript_obj = ytt_api.fetch(video_id)
18
- # Extract and join only the text fields from FetchedTranscriptSnippet objects
19
  full_text = " ".join([snippet.text for snippet in transcript_obj.snippets])
20
  return full_text
21
  except Exception as e:
22
  return f"Error fetching transcript: {str(e)}"
23
 
24
-
25
- # Gradio Interface
26
- iface = gr.Interface(
27
  fn=fetch_transcript,
28
  inputs=gr.Textbox(label="Enter YouTube Video ID"),
29
  outputs=gr.Textbox(label="Transcript"),
30
- live=True
 
31
  )
32
 
33
- # Launch the Gradio app
34
- iface.launch()
 
 
 
 
 
 
1
  import gradio as gr
2
+ from fastapi import FastAPI, Request
3
  from youtube_transcript_api import YouTubeTranscriptApi
4
  from youtube_transcript_api.proxies import WebshareProxyConfig
5
+ from pydantic import BaseModel
6
 
7
+ # FastAPI app
8
+ app = FastAPI()
9
+
10
+ # Initialize YouTubeTranscriptApi with proxy
11
  ytt_api = YouTubeTranscriptApi(
12
  proxy_config=WebshareProxyConfig(
13
+ proxy_username="tlaukrdr", # Replace with real credentials
14
+ proxy_password="mc1aumn9xbhb"
15
  )
16
  )
17
 
18
+ # API input schema
19
+ class VideoIDInput(BaseModel):
20
+ video_id: str
21
+
22
+ # API Endpoint
23
+ @app.post("/api/transcript")
24
+ async def get_transcript(payload: VideoIDInput):
25
+ try:
26
+ transcript_obj = ytt_api.fetch(payload.video_id)
27
+ full_text = " ".join([snippet.text for snippet in transcript_obj.snippets])
28
+ return {"transcript": full_text}
29
+ except Exception as e:
30
+ return {"error": str(e)}, 500
31
+
32
+ # Gradio function
33
  def fetch_transcript(video_id: str):
34
  try:
 
35
  transcript_obj = ytt_api.fetch(video_id)
 
36
  full_text = " ".join([snippet.text for snippet in transcript_obj.snippets])
37
  return full_text
38
  except Exception as e:
39
  return f"Error fetching transcript: {str(e)}"
40
 
41
+ # Gradio UI
42
+ demo = gr.Interface(
 
43
  fn=fetch_transcript,
44
  inputs=gr.Textbox(label="Enter YouTube Video ID"),
45
  outputs=gr.Textbox(label="Transcript"),
46
+ live=True,
47
+ title="YouTube Transcript Fetcher"
48
  )
49
 
50
+ # Mount Gradio at root
51
+ app = gr.mount_gradio_app(app, demo, path="/")
52
+
53
+ # Run Locally
54
+ if __name__ == "__main__":
55
+ import uvicorn
56
+ uvicorn.run(app, host="0.0.0.0", port=7860)
requirements.txt CHANGED
@@ -1,2 +1,5 @@
 
1
  gradio
2
- youtube_transcript_api
 
 
 
1
+ fastapi
2
  gradio
3
+ uvicorn
4
+ pydantic
5
+ youtube-transcript-api