-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
52 lines (45 loc) · 1.55 KB
/
main.py
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
48
49
50
import spacy
import requests
from flask_cors import CORS
from flask import Flask, request
from youtube_transcript_api import YouTubeTranscriptApi
app = Flask(__name__)
CORS(app)
SMMRY_API_KEY = "92E708A8B3"
def get_transcript(video_id, percent):
transcript = {}
try:
transcript = YouTubeTranscriptApi.get_transcript(
video_id,languages=["en"])
except:
return {"error":"No transcript found"}
transcript_string = " "
for line in transcript:
transcript_string += line['text']+" "
return identify_sentences(transcript_string, percent)
def identify_sentences(transcript_string, percent):
nlp = spacy.load("en_core_web_sm")
doc = nlp(transcript_string.lower())
sentences = doc.sents
transcript_with_sentences=""
num_sentences = 0
for sentence in doc.sents:
transcript_with_sentences+=sentence.text+".\n ";
num_sentences+=1
return get_summary(transcript_with_sentences, percent, num_sentences)
def get_summary(transcript, percent, num_sentences):
summary_length= round(float(percent)*num_sentences)
api_url= ("http://api.smmry.com/&SM_API_KEY=%s&SM_LENGTH=%s"
% (SMMRY_API_KEY,summary_length))
r = requests.post(api_url, data={"sm_api.input":transcript})
if "sm_api_content" not in r.json():
return {"error":"No transcript found, or transcript too short!"}
return {"result":r.json()["sm_api_content"]}
@app.route("/")
def summarize():
video_id = request.args.get("video_id")
percent_to_summarize= request.args.get("percent")
summary=get_transcript(video_id, percent_to_summarize)
return summary
if __name__=="__main__":
app.run(host='0.0.0.0')