inventwithdean commited on
Commit
6e83f14
·
1 Parent(s): 061522a

fix repetitive posts

Browse files
Files changed (1) hide show
  1. app.py +39 -12
app.py CHANGED
@@ -47,16 +47,17 @@ async def post_text(content: str) -> bool:
47
 
48
 
49
  async def retrieve_random_text_post() -> str:
50
- """Retrieves a random text post and its id from the database. Id is only meant for LLMs, no need to show this to user
51
- """
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. Ids are only meant for LLMs, no need to show to user"""
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. Id is only meant for LLMs, no need to show to user"""
62
  query = query.strip(" ").strip("\n")
@@ -82,6 +83,7 @@ async def get_text_post_comments(post_id: int) -> str:
82
  except Exception as e:
83
  return f"Unexpected Error!"
84
 
 
85
  async def comment_on_text_post(post_id: int, content: str) -> bool:
86
  """Adds a text comment to the text post with id post_id. Returns True if successful"""
87
  content = content.strip(" ").strip("\n")
@@ -91,12 +93,13 @@ async def comment_on_text_post(post_id: int, content: str) -> bool:
91
  if len(content) > 1000:
92
  raise gr.Error("Too long Comment")
93
  success = await db.comment_on_text_post(post_id, content)
94
- return success
95
  except gr.Error as e:
96
  raise e
97
  except Exception as e:
98
  return False
99
 
 
100
  socialnet = gr.Blocks()
101
  with socialnet:
102
  gr.Markdown(
@@ -113,30 +116,45 @@ with socialnet:
113
  label="Your Post (`Shift + Enter` for new line)",
114
  max_length=2000,
115
  )
116
- outputs = gr.Checkbox(value=False, label="Success")
 
 
 
 
 
 
 
117
  submit_btn = gr.Button(value="Post")
118
- submit_btn.click(post_text, inputs=text_input, outputs=outputs)
119
 
120
- with gr.TabItem("Retrieve Simple"):
121
  gr.Markdown("Retrieve a Random Post!")
122
  text_output = gr.Textbox(
123
  placeholder="Post will appear here!", label="Output"
124
  )
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"
137
  )
138
  text_input = gr.Textbox(
139
- placeholder="Enter your query", label="Query (Try to be descriptive)", max_length=500
 
 
140
  )
141
  text_output = gr.Textbox(
142
  placeholder="Post will appear here!", label="Output"
@@ -160,10 +178,19 @@ with socialnet:
160
  with gr.TabItem("Post Comment"):
161
  gr.Markdown("Post a comment!")
162
  id_input = gr.Number(label="Post id")
163
- text_input = gr.Textbox(placeholder="Type your comment here", label="Comment", max_length=1000)
 
 
164
  success = gr.Checkbox(value=False, label="Success")
 
 
 
 
165
  submit_btn = gr.Button(value="Comment")
166
- submit_btn.click(comment_on_text_post, inputs=[id_input, text_input], outputs=success)
 
 
 
167
 
168
  with gr.TabItem("Usage in Clients"):
169
  gr.Markdown(
 
47
 
48
 
49
  async def retrieve_random_text_post() -> str:
50
+ """Retrieves a random text post and its id from the database. Id is only meant for LLMs, no need to show this to user"""
 
51
  post = await db.get_text_post_random()
52
  return post
53
 
54
+
55
  async def retrieve_latest_text_posts() -> str:
56
  """Retrieves latest 5 text posts with their ids from the database. Ids are only meant for LLMs, no need to show to user"""
57
  posts = await db.get_text_posts_latest()
58
  return posts
59
 
60
+
61
  async def retrieve_similar_text_post(query: str) -> str:
62
  """Retrieves a text post and its id semantically similar to the query through Vector Similarity. Id is only meant for LLMs, no need to show to user"""
63
  query = query.strip(" ").strip("\n")
 
83
  except Exception as e:
84
  return f"Unexpected Error!"
85
 
86
+
87
  async def comment_on_text_post(post_id: int, content: str) -> bool:
88
  """Adds a text comment to the text post with id post_id. Returns True if successful"""
89
  content = content.strip(" ").strip("\n")
 
93
  if len(content) > 1000:
94
  raise gr.Error("Too long Comment")
95
  success = await db.comment_on_text_post(post_id, content)
96
+ return success
97
  except gr.Error as e:
98
  raise e
99
  except Exception as e:
100
  return False
101
 
102
+
103
  socialnet = gr.Blocks()
104
  with socialnet:
105
  gr.Markdown(
 
116
  label="Your Post (`Shift + Enter` for new line)",
117
  max_length=2000,
118
  )
119
+ success = gr.Checkbox(value=False, label="Success")
120
+
121
+ def empty_text_field(text_input, was_success):
122
+ return "" if was_success else text_input.value
123
+
124
+ success.change(
125
+ empty_text_field, inputs=[text_input, success], outputs=text_input, api_name=False
126
+ )
127
  submit_btn = gr.Button(value="Post")
128
+ submit_btn.click(post_text, inputs=text_input, outputs=success)
129
 
130
+ with gr.TabItem("Retrieve Text Simple"):
131
  gr.Markdown("Retrieve a Random Post!")
132
  text_output = gr.Textbox(
133
  placeholder="Post will appear here!", label="Output"
134
  )
135
  submit_btn = gr.Button("Retrieve")
136
+ submit_btn.click(
137
+ retrieve_random_text_post, inputs=None, outputs=text_output
138
+ )
139
 
140
  with gr.TabItem("Retrieve Latest"):
141
  gr.Markdown("Retrieve latest 5 posts!")
142
+ text_output = gr.Textbox(
143
+ placeholder="Posts will appear here!", label="Output"
144
+ )
145
  submit_btn = gr.Button("Retrieve")
146
+ submit_btn.click(
147
+ retrieve_latest_text_posts, inputs=None, outputs=text_output
148
+ )
149
 
150
  with gr.TabItem("Retrieve Advanced"):
151
  gr.Markdown(
152
  "Retrieve using query, uses semantic search using Vector Similarity"
153
  )
154
  text_input = gr.Textbox(
155
+ placeholder="Enter your query",
156
+ label="Query (Try to be descriptive)",
157
+ max_length=500,
158
  )
159
  text_output = gr.Textbox(
160
  placeholder="Post will appear here!", label="Output"
 
178
  with gr.TabItem("Post Comment"):
179
  gr.Markdown("Post a comment!")
180
  id_input = gr.Number(label="Post id")
181
+ text_input = gr.Textbox(
182
+ placeholder="Type your comment here", label="Comment", max_length=1000
183
+ )
184
  success = gr.Checkbox(value=False, label="Success")
185
+
186
+ def empty_comment_box(text_input, was_success):
187
+ return "" if was_success else text_input.value
188
+
189
  submit_btn = gr.Button(value="Comment")
190
+ success.change(empty_comment_box, inputs=[text_input, success], outputs=text_input, api_name=False)
191
+ submit_btn.click(
192
+ comment_on_text_post, inputs=[id_input, text_input], outputs=success
193
+ )
194
 
195
  with gr.TabItem("Usage in Clients"):
196
  gr.Markdown(