Skip to content

Commit

Permalink
fix: openapi schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Morriz committed May 22, 2024
1 parent f51be9d commit dda11c7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
10 changes: 8 additions & 2 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
query_media,
query_mediabiasfactcheck,
)
from api.tools.youtube import Video, youtube_search, youtube_transcripts
from api.youtube import Video, VideoTranscript, youtube_search, youtube_transcripts
from lib.auth import verify_apikey

app = FastAPI()
Expand Down Expand Up @@ -59,6 +59,9 @@ async def get_youtube_search(
get_transcripts: bool = False,
_: None = Depends(verify_apikey),
) -> List[Video]:
"""
Get the details of matching videos by either providing Youtube channels, a query, or both
"""
if not (channels or query):
raise HTTPException(
status_code=400, detail="No query given when no channels are provided!"
Expand All @@ -74,11 +77,14 @@ async def get_youtube_search(
)


@app.get("/youtube-transcripts", response_model=Dict[str, str])
@app.get("/youtube-transcripts", response_model=List[VideoTranscript])
async def get_youtube_transcripts(
ids: str,
_: None = Depends(verify_apikey),
) -> Dict[str, str]:
"""
Extract transcripts from a list of Youtube video ids
"""
return youtube_transcripts(ids)


Expand Down
16 changes: 10 additions & 6 deletions api/tools/youtube.py → api/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ class Transcript(BaseModel):
duration: int


class VideoTranscript(BaseModel):
"""Video transcript model"""

id: str
text: str


class Video(BaseModel):
"""Video model"""

Expand Down Expand Up @@ -136,9 +143,6 @@ async def youtube_search(
get_descriptions: bool = False,
get_transcripts: bool = False,
) -> List[Video]:
"""
Get the details of matching videos by either providing Youtube channels, a query, or both
"""
if channels:
channels_arr = channels.lower().split(",")
media = [
Expand Down Expand Up @@ -184,14 +188,14 @@ async def youtube_search(
@cache(ttl=3600)
def youtube_transcripts(
ids: str,
) -> Dict[str, str]:
) -> List[VideoTranscript]:
"""
Extract transcripts from a list of Youtube video ids
"""
results: Dict[str, str] = {}
results: List[VideoTranscript] = []
for video_id in ids.split(","):
transcript = _get_video_transcript(video_id, strip_timestamps=True)
results[video_id] = transcript
results.append(VideoTranscript(id=video_id, text=transcript))
return results


Expand Down
23 changes: 20 additions & 3 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ paths:
/media-videos:
get:
summary: Get Youtube Search
description: Get the details of matching videos by either providing Youtube
channels, a query, or both
operationId: get_youtube_search_media_videos_get
security:
- APIKeyQuery: []
Expand Down Expand Up @@ -217,6 +219,7 @@ paths:
/youtube-transcripts:
get:
summary: Get Youtube Transcripts
description: Extract transcripts from a list of Youtube video ids
operationId: get_youtube_transcripts_youtube_transcripts_get
security:
- APIKeyQuery: []
Expand All @@ -235,9 +238,9 @@ paths:
content:
application/json:
schema:
type: object
additionalProperties:
type: string
type: array
items:
$ref: '#/components/schemas/VideoTranscript'
title: Response Get Youtube Transcripts Youtube Transcripts Get
'422':
description: Validation Error
Expand Down Expand Up @@ -411,6 +414,20 @@ components:
- url_suffix
title: Video
description: Video model
VideoTranscript:
properties:
id:
type: string
title: Id
text:
type: string
title: Text
type: object
required:
- id
- text
title: VideoTranscript
description: Video transcript model
securitySchemes:
APIKeyQuery:
type: apiKey
Expand Down
3 changes: 1 addition & 2 deletions pages/2-Youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import streamlit as st
import streamlit.components.v1 as components

from api.main import youtube_search
from api.tools.youtube import youtube_search
from api.youtube import youtube_search

with open("index.html", "r") as f:
html_code = f.read()
Expand Down

0 comments on commit dda11c7

Please sign in to comment.