Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

bored_app.py 3.0 KB

You have to be logged in to leave a comment. Sign In
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
89
90
91
92
93
  1. from pywebio import *
  2. from pywebio.output import *
  3. from pywebio.input import *
  4. from pywebio.pin import *
  5. import requests
  6. from typing import List
  7. import requests
  8. import spacy
  9. # ---- extract_books util functions
  10. def extract_noun_phrases(text: str):
  11. """Extract noun phrases from a text"""
  12. nlp = spacy.load("en_core_web_sm")
  13. doc = nlp(text)
  14. return [chunk.text for chunk in doc.noun_chunks]
  15. def get_query(phrase: str):
  16. """Turn noun phrase into a query by replacing space with +"""
  17. return '+'.join(phrase.split(' '))
  18. def get_query_for_noun_phrases(text: str):
  19. """Turn list of noun phrases into a list of queries"""
  20. noun_phrases = extract_noun_phrases(text)
  21. return [get_query(phrase) for phrase in noun_phrases]
  22. def get_books(query: str):
  23. """Get the first 3 books based on the query"""
  24. api = f"https://openlibrary.org/search.json?title={query}"
  25. response = requests.get(api)
  26. content = response.json()['docs'][:3]
  27. return content
  28. def get_books_of_text(text: str):
  29. """Get books given a test"""
  30. queries = get_query_for_noun_phrases(text)
  31. books = []
  32. for query in queries:
  33. books.extend(get_books(query))
  34. return books
  35. # ---- bore API util function
  36. def get_activity_content(inputs: dict):
  37. """Get a random activity using Bored API"""
  38. if inputs['value'] == 'random':
  39. api = "https://www.boredapi.com/api/activity"
  40. else:
  41. api = f"https://www.boredapi.com/api/activity?type={inputs['value']}"
  42. response = requests.get(api)
  43. content = response.json()
  44. return content
  45. # ---- layout util functions
  46. def display_activity_for_boredom():
  47. put_markdown("# Find things to do when you're bored")
  48. activity_types = ['random', "education", "recreational", "social",
  49. "diy", "charity", "cooking", "relaxation", "music", "busywork"]
  50. put_select(name='type', label='Activity Type', options=activity_types)
  51. def create_book_table(books: List[dict]):
  52. if books == []:
  53. put_markdown("No books with this topic is found")
  54. return
  55. book_table = [[book['title'], book.get('author_name', ['_'])[0]] for book in books]
  56. book_table.insert(0, ['Title', 'Author'])
  57. put_table(book_table)
  58. # ---- main func running as the web app on pyweb.io FaaS platform
  59. def main():
  60. display_activity_for_boredom()
  61. while True:
  62. new_inputs = pin_wait_change(['type'])
  63. with use_scope('activity', clear=True):
  64. content = get_activity_content(new_inputs)
  65. put_markdown(f"""## Your activity for today: {content['activity']}""")
  66. put_markdown(f"""Number of participants: {content['participants']}
  67. Price: {content['price']}
  68. Accessibility: {content['accessibility']}""")
  69. put_markdown("## Books related to this activity you might be interested in:")
  70. with put_loading():
  71. books = get_books_of_text(content['activity'])
  72. create_book_table(books)
  73. if __name__ == '__main__':
  74. start_server(main, port=36635, debug=True)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...