LaoCzi commited on
Commit
f4c4572
·
1 Parent(s): 05d19fc

Create app.py

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