as-cle-bert commited on
Commit
d37ace6
·
verified ·
1 Parent(s): f125a18

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import atproto as atp
3
+ import markdown
4
+ from langchain_cohere import ChatCohere
5
+ from langchain_core.prompts import ChatPromptTemplate
6
+ import os
7
+
8
+ api_key = st.secrets["COHERE_API_KEY"]
9
+ llm = ChatCohere(cohere_api_key=api_key)
10
+ template = ChatPromptTemplate([
11
+ (
12
+ "system",
13
+ "You are a helpful and efficient feed summarization assistant. You should provide a coincise and well-style, polished overview of what the post authors are telling"
14
+ ),
15
+ (
16
+ "human",
17
+ "{message}"
18
+ )
19
+ ])
20
+
21
+ chain = template | llm
22
+ client = atp.Client()
23
+
24
+
25
+ def text_inference(message):
26
+ res = chain.invoke({"message": message})
27
+ ret = res.content
28
+ return ret
29
+
30
+ def get_feed(username: str, password: str):
31
+ try:
32
+ client.login(username, password)
33
+ except Exception as e:
34
+ return "### Your username or password are wrong :("
35
+ strtort = ""
36
+ strsum = ""
37
+ # Get "Home" page. Use pagination (cursor + limit) to fetch all posts
38
+ timeline = client.get_timeline(algorithm='reverse-chronological')
39
+ c = 0
40
+ for feed_view in timeline.feed:
41
+ c+=1
42
+ action = 'New Post🆕'
43
+ if feed_view.reason:
44
+ action_by = feed_view.reason.by.handle
45
+ action = f'Reposted by @{action_by}🔁'
46
+
47
+ post = feed_view.post.record
48
+ author = feed_view.post.author
49
+
50
+ strtort += f'<h3>{action}</h3>\n<img alt="Avatar for {author.display_name}" src="{author.avatar}" width=50> <b>{author.display_name}<b>:<br><p>{post.text}</p><hr>'
51
+ strsum += f'Author: {author.display_name} - Post content: {post.text}'
52
+ if c>=25:
53
+ break
54
+ res = text_inference(strsum)
55
+ reshtml = markdown.markdown(res)
56
+ smry = f"\n\n<details>\n\t<summary><b>Feed Summary</b></summary>\n\n{reshtml}\n\n</details>\n\n"
57
+ strtort = f"{smry}\n\n{strtort}"
58
+ return strtort
59
+
60
+
61
+ if __name__ == "__main__":
62
+ # Title of the web app
63
+ st.title("BlueSky User Feed🦋")
64
+ st.subheader("Your home feed and a summary of it, all in one placr")
65
+ st.image("https://upload.wikimedia.org/wikipedia/commons/thumb/7/7a/Bluesky_Logo.svg/1920px-Bluesky_Logo.svg.png", width=150)
66
+ # Input text box for the search query
67
+ username = st.text_input("You BlueSky username/handle", placeholder="user.bsky.social")
68
+ password = st.text_input("Password", type="password")
69
+
70
+ # Button to initiate search
71
+ if st.button("Show my feed!"):
72
+ if username != "" and password!="":
73
+ results = get_feed(username, password)
74
+ if results != "### Your username or password are wrong :(":
75
+ st.write(f"## Home Feed (Following)🏠\n\n----------------------\n\n")
76
+ st.html(results)
77
+ else:
78
+ st.write(results)
79
+ else:
80
+ if username == "" and password!="":
81
+ st.write("### Please enter a username")
82
+ elif password == "" and username!="":
83
+ st.write("### Please enter a password")
84
+ else:
85
+ st.write("### Please enter a username and a password")
86
+