-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
88 lines (70 loc) · 2.57 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import feedparser
import pytz
import openai
import streamlit as st
from datetime import datetime, timedelta
st.title("AI generated summary of Crypto news from the last 24 hours")
st.markdown("Follow us on [Mastodon](https://mastodon.online/@ngmi)")
# List of RSS feed URLs
rss_feeds = [
"https://decrypt.co/feed",
"https://www.coindesk.com/arc/outboundfeeds/rss/",
# Add more RSS feeds here
]
# OpenAI API key input
openai_api_key = st.text_input("Enter your OpenAI API Key:")
openai.api_key = openai_api_key
def get_summary_and_sentiment(text):
messages = [
{
"role": "system",
"content": "You are a helpful assistant that can provide a short summary with less than 400 characters", # noqa: E501
},
{"role": "user", "content": text},
]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
max_tokens=150,
n=1,
stop=None,
temperature=0.7,
)
result = response.choices[0].message["content"].strip()
# Limit the output to 450 characters and stop at the last full sentence
limited_result = ""
for sentence in result.split(". "):
if len(limited_result) + len(sentence) + 1 <= 450:
if limited_result != "":
limited_result += ". "
limited_result += sentence
else:
break
return limited_result
# Get headlines
def get_headlines(feed_url, time_limit):
headlines_and_links = []
feed = feedparser.parse(feed_url)
for entry in feed.entries:
published_date = datetime.strptime(entry.published, "%a, %d %b %Y %H:%M:%S %z")
if published_date >= time_limit:
headlines_and_links.append((entry.title, entry.link))
return headlines_and_links
if openai_api_key:
# Main function
local_tz = pytz.timezone(
"UTC"
) # Replace 'your_local_timezone' with your actual timezone
time_limit = datetime.now(local_tz) - timedelta(days=1)
all_headlines_and_links = []
for feed_url in rss_feeds:
headlines_and_links = get_headlines(feed_url, time_limit)
all_headlines_and_links.extend(headlines_and_links)
combined_titles = " ".join([headline for headline, _ in all_headlines_and_links])
summary_and_sentiment = get_summary_and_sentiment(combined_titles)
st.write(f"**Summary:**\n{summary_and_sentiment}")
st.header("News articles:")
for title, link in all_headlines_and_links:
st.markdown(f"[{title}]({link})")
else:
st.write("Please enter your OpenAI API key to get the summary.")