-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
64 lines (52 loc) · 1.63 KB
/
app.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import requests
import google.generativeai as genai
from flask import Flask, request, send_file
from flask_cors import CORS
from markdown import markdown
from readability import Document
from youtube import YouTube
app = Flask(__name__)
CORS(app)
@app.route('/')
def index():
return send_file('index.html')
@app.route('/summarize', methods=['GET'])
async def summarize(is_news=False):
url = request.args.get('url')
yt = YouTube()
if yt.is_link(url):
content = yt.get_transcriptions(url)
if not content:
return f'''
<html><body><content>
Could not transcribe YouTube video.
</content></body></html>
'''
else:
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0'}
html_content = requests.get(url, headers=headers).text
content = Document(html_content).summary()
prompt_file = "prompts/article.md"
if is_news: prompt_file = "prompts/news.md"
with open(prompt_file) as f:
system_instruction = f.read()
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
model = genai.GenerativeModel(
'gemini-2.0-flash-exp',
system_instruction=system_instruction,
)
response = model.generate_content(content)
return f'''
<html><body><article>
{markdown(response.text)}
</article></body></html>
'''
@app.route('/news', methods=['GET'])
async def news():
return await summarize(is_news=True)
if __name__ == '__main__':
app.run(
host='0.0.0.0',
debug=os.environ.get("DEBUG") == '1'
)