lanbogao commited on
Commit
093a866
0 Parent(s):

Duplicate from lanbogao/docker_fastapi

Browse files
Files changed (6) hide show
  1. .gitattributes +34 -0
  2. Dockerfile +11 -0
  3. README.md +11 -0
  4. fetchYoutubeSubtitle.py +97 -0
  5. main.py +20 -0
  6. requirements.txt +7 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+
7
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
+
9
+ COPY . .
10
+
11
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
README.md ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Docker Fastapi
3
+ emoji: 🐢
4
+ colorFrom: purple
5
+ colorTo: blue
6
+ sdk: docker
7
+ pinned: false
8
+ duplicated_from: lanbogao/docker_fastapi
9
+ ---
10
+
11
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
fetchYoutubeSubtitle.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import requests
4
+ import logging
5
+
6
+ logger = logging.getLogger(__name__)
7
+
8
+ from typing import List, Dict
9
+
10
+ SUBTITLE_DOWNLOADER_URL = 'https://savesubs.com'
11
+
12
+ def fetchYoutubeSubtitleUrls(video_id):
13
+ headers = {
14
+ 'accept': 'application/json, text/plain, */*',
15
+ 'accept-language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7',
16
+ 'cache-control': 'no-cache',
17
+ 'Content-Type': 'application/json; charset=UTF-8',
18
+ 'pragma': 'no-cache',
19
+ 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
20
+ 'x-auth-token': os.environ.get('SAVESUBS_X_AUTH_TOKEN', ''),
21
+ 'x-requested-domain': 'savesubs.com',
22
+ 'X-requested-with': 'xmlhttprequest',
23
+ 'sec-ch-ua': '"Google Chrome";v="111", "Not(A:Brand";v="8", "Chromium";v="111"',
24
+ 'sec-ch-ua-mobile': '?0',
25
+ 'sec-ch-ua-platform': 'Linux',
26
+ 'sec-fetch-dest': 'empty',
27
+ 'sec-fetch-mode': 'cors',
28
+ 'sec-fetch-site': 'same-origin',
29
+ 'authority': 'savesubs.com',
30
+ 'origin': 'https://savesubs.com',
31
+ 'referer': f'https://savesubs.com/process?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D{video_id}'
32
+ }
33
+ data = {
34
+ 'data': {'url': f'https://www.youtube.com/watch?v={video_id}'}
35
+ }
36
+ session = requests.Session()
37
+
38
+ proxy = os.environ.get('PROXY', None)
39
+ if proxy:
40
+ session.proxies = {
41
+ "http": proxy,
42
+ "https": proxy,
43
+ }
44
+
45
+ response = session.post(SUBTITLE_DOWNLOADER_URL + '/action/extract', json=data, headers=headers)
46
+
47
+
48
+ if response.status_code != 200:
49
+ logger.error("response.status_code: {}".format(response.status_code))
50
+ return {'title': None, 'subtitleList': None, 'error': response.reason}
51
+ else:
52
+ try:
53
+ json = response.json().get('response', {})
54
+ logger.info('subtitle url json: {}'.format(json))
55
+ return {'title': json.get('title'), 'subtitleList': json.get('formats')}
56
+ except Exception as error:
57
+ logger.error(error)
58
+ return {'title': None, 'subtitleList': None, 'error': str(error)}
59
+
60
+
61
+ async def find(subtitleList: List[Dict], args: Dict) -> Dict:
62
+ key = list(args.keys())[0]
63
+ return next((item for item in subtitleList if item.get(key, None) == args[key]), None)
64
+
65
+ async def fetchYoutubeSubtitle(videoId: str) -> Dict:
66
+ subtitle_url = ""
67
+ betterSubtitle = {}
68
+ subtitleList = []
69
+ title = ""
70
+ error = ""
71
+
72
+ result = fetchYoutubeSubtitleUrls(videoId)
73
+ title, subtitleList, error = result["title"], result["subtitleList"], result.get("error", None)
74
+
75
+ if not subtitleList or len(subtitleList) <= 0:
76
+ return {"title": title, "subtitle": None, "error": error}
77
+
78
+ betterSubtitle = (
79
+ await find(subtitleList, {"quality": "English"})
80
+ or await find(subtitleList, {"quality": "English (auto"})
81
+ or await find(subtitleList, {"quality": "zh-CN"})
82
+ or subtitleList[0]
83
+ )
84
+
85
+ subtitleUrl = f"{SUBTITLE_DOWNLOADER_URL}{betterSubtitle['url']}?ext=srt"
86
+
87
+ session = requests.Session()
88
+
89
+ proxy = os.environ.get('PROXY', None)
90
+ if proxy:
91
+ session.proxies = {
92
+ "http": proxy,
93
+ "https": proxy,
94
+ }
95
+ response = session.get(subtitleUrl)
96
+
97
+ return {"title": title, "subtitle": response.text}
main.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from fastapi.responses import JSONResponse
3
+ from fetchYoutubeSubtitle import fetchYoutubeSubtitle
4
+
5
+ app = FastAPI()
6
+
7
+ @app.get("/")
8
+ def read_root():
9
+ return {"Hello": "World!"}
10
+
11
+
12
+ @app.get("/json")
13
+ def read_json():
14
+ return JSONResponse(content={"Hello": "World!"})
15
+
16
+
17
+ @app.get("/subtitle/")
18
+ async def get_subtitle(vid: str):
19
+ subtitle = await fetchYoutubeSubtitle(vid)
20
+ return JSONResponse(content=subtitle)
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi==0.74.*
2
+ requests==2.27.*
3
+ sentencepiece==0.1.*
4
+ torch==1.11.*
5
+ transformers==4.*
6
+ uvicorn[standard]==0.17.*
7
+ langchain