Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,111 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import openai
|
3 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
4 |
+
|
5 |
+
import gradio as gr
|
6 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
7 |
+
from urllib.parse import urlparse, parse_qs
|
8 |
+
from requests.structures import CaseInsensitiveDict
|
9 |
+
|
10 |
+
|
11 |
+
def Prompt_T(context):
|
12 |
+
result = """I want you to act as a content writer who is working with youtube video transcript. Summarise the following text:
|
13 |
+
=========
|
14 |
+
"""+ context +"""
|
15 |
+
=========
|
16 |
+
Answer:"""
|
17 |
+
return result
|
18 |
+
|
19 |
+
|
20 |
+
def split_string(string, chunk_size):
|
21 |
+
return [string[i:i+chunk_size] for i in range(0, len(string), chunk_size)]
|
22 |
+
|
23 |
+
|
24 |
+
def gpt_api (input_text):
|
25 |
+
completion = openai.ChatCompletion.create(
|
26 |
+
model="gpt-3.5-turbo",
|
27 |
+
messages=[ {"role": "system", "content": input_text} ]
|
28 |
+
)
|
29 |
+
response = completion.choices[0].message.content
|
30 |
+
return response
|
31 |
+
|
32 |
+
def generate(video_url, request: gr.Request):
|
33 |
+
|
34 |
+
#Если есть get переменная v, создаем video_url
|
35 |
+
try:
|
36 |
+
headers =request.headers
|
37 |
+
url = headers.get('referer')
|
38 |
+
parsed_url = urlparse(url)
|
39 |
+
query_params = parse_qs(parsed_url.query)
|
40 |
+
my_dict = query_params
|
41 |
+
except KeyError as e:
|
42 |
+
print("Что то случилось в header", e)
|
43 |
+
|
44 |
+
try:
|
45 |
+
my_v = my_dict['v'][0]
|
46 |
+
video_url ="https://youtube.com/watch?v="+my_v
|
47 |
+
except KeyError:
|
48 |
+
print("Ключ 'v' отсутствует в словаре.")
|
49 |
+
my_v = ""
|
50 |
+
|
51 |
+
#Если две переменные пустые, то показываем базовую страницу с рекламой
|
52 |
+
if (video_url =="") and (my_v == ""):
|
53 |
+
html_embed='<div><br> An easy way to get video descriptions If you are on YouTube itself, simply add "zxc" in front of YouTube to the videos address.</div>'
|
54 |
+
summarize=""
|
55 |
+
return summarize, html_embed
|
56 |
+
|
57 |
+
#похоже ли video_url на номальну ссылку
|
58 |
+
if "youtube.com/watch?v=" in video_url: x=111
|
59 |
+
else: return "Неверный URL", "Ошибка"
|
60 |
+
|
61 |
+
#Пробуем извлеч video_id пока на английском
|
62 |
+
video_id = video_url[-11:]
|
63 |
+
try:
|
64 |
+
t = YouTubeTranscriptApi.get_transcript(video_id,languages=["en"])
|
65 |
+
# do something with the transcript
|
66 |
+
except Exception as e:
|
67 |
+
return "Несмогли нати трнскрипт", "Ошибка"
|
68 |
+
|
69 |
+
finalString = ""
|
70 |
+
for item in t:
|
71 |
+
text = item['text']
|
72 |
+
finalString += text + " "
|
73 |
+
|
74 |
+
|
75 |
+
print("Transcript:",finalString)
|
76 |
+
print("Transcript lenght:",len(finalString))
|
77 |
+
print ("===============================================")
|
78 |
+
input_string = finalString
|
79 |
+
chunk_size = 10000
|
80 |
+
result_list = split_string(input_string, chunk_size)
|
81 |
+
eng_answer=""
|
82 |
+
count= 0
|
83 |
+
for item in result_list:
|
84 |
+
count = count +1
|
85 |
+
context = item
|
86 |
+
eng_prompt = Prompt_T(context)
|
87 |
+
eng_answer = eng_answer +" \n" + gpt_api (eng_prompt)
|
88 |
+
print("Context:", context)
|
89 |
+
print(count, " - part eng_answer:", eng_answer)
|
90 |
+
print("==========================")
|
91 |
+
|
92 |
+
html_embed='<iframe width="450" height="158" src="https://www.youtube.com/embed/'+ video_id +'" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>'
|
93 |
+
html_content="<h6>"+"<br>"+eng_answer+"</h6>"
|
94 |
+
return html_content, html_embed
|
95 |
+
title = "YouTube Summorize (only english video)"
|
96 |
+
css="""
|
97 |
+
footer {visibility: hidden}
|
98 |
+
.gradio-container {padding-top: 100px}
|
99 |
+
"""
|
100 |
+
with gr.Blocks(css=css, title=title) as demo:
|
101 |
+
with gr.Row():
|
102 |
+
with gr.Column():
|
103 |
+
input_d = gr.Textbox(label="YouTube video URL", placeholder="https://www.youtube.com/watch?v=XXXXXXXX")
|
104 |
+
greet_btn = gr.Button("Summarise")
|
105 |
+
dt_2 = gr.outputs.HTML()
|
106 |
+
dt_1 = gr.outputs.HTML()
|
107 |
+
dt =[dt_1, dt_2 ]
|
108 |
+
greet_btn.click(generate, inputs=input_d, outputs=dt)
|
109 |
+
demo.load(generate, inputs=input_d, outputs=dt)
|
110 |
+
|
111 |
+
demo.launch(share=False, debug=True )
|