inventwithdean
commited on
Commit
·
18b5344
1
Parent(s):
37fd7e0
Add latest posts feature
Browse files- app.py +10 -0
- database.py +20 -1
app.py
CHANGED
@@ -52,6 +52,10 @@ async def retrieve_random_text_post() -> str:
|
|
52 |
post = await db.get_text_post_random()
|
53 |
return post
|
54 |
|
|
|
|
|
|
|
|
|
55 |
|
56 |
async def retrieve_similar_text_post(query: str) -> str:
|
57 |
"""Retrieves a text post and its id semantically similar to the query through Vector Similarity"""
|
@@ -121,6 +125,12 @@ with socialnet:
|
|
121 |
submit_btn = gr.Button("Retrieve")
|
122 |
submit_btn.click(retrieve_random_text_post, inputs=None, outputs=text_output)
|
123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
with gr.TabItem("Retrieve Advanced"):
|
125 |
gr.Markdown(
|
126 |
"Retrieve using query, uses semantic search using Vector Similarity"
|
|
|
52 |
post = await db.get_text_post_random()
|
53 |
return post
|
54 |
|
55 |
+
async def retrieve_latest_text_posts() -> str:
|
56 |
+
"""Retrieves latest 5 text posts with their ids from the database"""
|
57 |
+
posts = await db.get_text_posts_latest()
|
58 |
+
return posts
|
59 |
|
60 |
async def retrieve_similar_text_post(query: str) -> str:
|
61 |
"""Retrieves a text post and its id semantically similar to the query through Vector Similarity"""
|
|
|
125 |
submit_btn = gr.Button("Retrieve")
|
126 |
submit_btn.click(retrieve_random_text_post, inputs=None, outputs=text_output)
|
127 |
|
128 |
+
with gr.TabItem("Retrieve Latest"):
|
129 |
+
gr.Markdown("Retrieve latest 5 posts!")
|
130 |
+
text_output = gr.Textbox(placeholder="Posts will appear here!", label="Output")
|
131 |
+
submit_btn = gr.Button("Retrieve")
|
132 |
+
submit_btn.click(retrieve_latest_text_posts, inputs=None, outputs=text_output)
|
133 |
+
|
134 |
with gr.TabItem("Retrieve Advanced"):
|
135 |
gr.Markdown(
|
136 |
"Retrieve using query, uses semantic search using Vector Similarity"
|
database.py
CHANGED
@@ -36,10 +36,29 @@ class NetworkDB:
|
|
36 |
)
|
37 |
await conn.close()
|
38 |
if post is not None:
|
39 |
-
formatted_post = f"
|
40 |
return formatted_post
|
41 |
return "[Internal Message: No post found!]"
|
42 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
return "[Internal Message: Server Error]"
|
44 |
|
45 |
async def get_text_post_similar(self, query_embedding: list[float]) -> str:
|
|
|
36 |
)
|
37 |
await conn.close()
|
38 |
if post is not None:
|
39 |
+
formatted_post = f"<|PostId_{id}|>\n{post}"
|
40 |
return formatted_post
|
41 |
return "[Internal Message: No post found!]"
|
42 |
except Exception as e:
|
43 |
+
print(f"Unexpected Error: {e}")
|
44 |
+
return "[Internal Message: Server Error]"
|
45 |
+
|
46 |
+
async def get_text_posts_latest(self) -> str:
|
47 |
+
try:
|
48 |
+
conn = await asyncpg.connect(self.database_url)
|
49 |
+
posts = await conn.fetch("SELECT (id, content) from text_posts ORDER BY uploaded_at DESC LIMIT 5")
|
50 |
+
await conn.close()
|
51 |
+
if len(posts) == 0:
|
52 |
+
return "[Internal Message: No posts in the database]"
|
53 |
+
formatted_posts = ""
|
54 |
+
for i, post in enumerate(posts):
|
55 |
+
post = post[0]
|
56 |
+
if i > 0:
|
57 |
+
formatted_posts += "\n\n"
|
58 |
+
formatted_posts += f'<|PostId_{post[0]}|>\n{post[1]}'
|
59 |
+
return formatted_posts
|
60 |
+
except Exception as e:
|
61 |
+
print(f"Unexpected Error: {e}")
|
62 |
return "[Internal Message: Server Error]"
|
63 |
|
64 |
async def get_text_post_similar(self, query_embedding: list[float]) -> str:
|