File size: 3,723 Bytes
f109e0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# from typing import Union
# import re
# from youtube_transcript_api import YouTubeTranscriptApi
# from youtube_transcript_api.formatters import TextFormatter
# import torch
# from transformers import pipeline
# from fastapi import FastAPI
# from fastapi.staticfiles import StaticFiles
# from fastapi.responses import FileResponse

# app = FastAPI()

# text_summary = pipeline("summarization", model="Falconsai/text_summarization")

# @app.get("/getdata")
# def getInput(input: str) -> dict:
#     output = text_summary(input)
#     return output[0]['summary_text']
    
# def extract_video_id(url):
#     regex = r"(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})"
#     match = re.search(regex, url)
#     if match:
#         return match.group(1)
#     return None


# def get_youtube_transcript(video_url):
#     video_id = extract_video_id(video_url)
#     if not video_id:
#         return "Video ID could not be extracted."

#     try:
#         transcript = YouTubeTranscriptApi.get_transcript(video_id)

#         formatter = TextFormatter()
#         text_transcript = formatter.format_transcript(transcript)
#         summary_text = summary(text_transcript)
#         print(summary_text)

#         return summary_text
#     except Exception as e:
#         return f"An error occurred: {e}"




# app.mount('/',StaticFiles(directory="static",html=True),name="static")

# @app.get('/')
# def index() -> FileResponse:
#     return FileResponse('/app/static/index.html',media_type="text/html")


# if __name__ == "__main__":
#     import uvicorn
#     uvicorn.run(app, host="127.0.0.1", port=5050)



from typing import Union
import re
from youtube_transcript_api import YouTubeTranscriptApi
from youtube_transcript_api.formatters import TextFormatter
from transformers import pipeline
from fastapi import FastAPI, Query
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse

app = FastAPI()

text_summary = pipeline("summarization", model="Falconsai/text_summarization", max_length=512)


def extract_video_id(url: str) -> Union[str, None]:
    regex = r"(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})"
    match = re.search(regex, url)
    if match:
        return match.group(1)
    return None

def get_youtube_transcript(video_url: str) -> Union[str, None]:
    video_id = extract_video_id(video_url)
    if not video_id:
        return "Video ID could not be extracted."
    try:
        transcript = YouTubeTranscriptApi.get_transcript(video_id)
        formatter = TextFormatter()
        text_transcript = formatter.format_transcript(transcript)
        return text_transcript
    except Exception as e:
        return f"An error occurred: {e}"

def summarize_text(text: str) -> str:
    summarized_text = text_summary(text)
    return summarized_text[0]['summary_text']

# Get the input from the frontend
@app.get("/getdata")
def get_data(input: str = Query(..., title="YouTube Video URL")) -> dict:
    print(input)
    transcript = get_youtube_transcript(input)
    if transcript:
        summary = summarize_text(transcript)
        return {"summary": summary}
    else:
        return {"error": "Failed to get transcript from the YouTube video."}



app.mount('/', StaticFiles(directory="static", html=True), name="static")


@app.get('/')
def index() -> FileResponse:
    return FileResponse('/app/static/index.html', media_type="text/html")


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="127.0.0.1", port=5050)