aleche28 commited on
Commit
2dc69e7
·
verified ·
1 Parent(s): 8d7ebba

Google Books API Integration

Browse files

# Google Books API Integration

This PR adds two MCP tools for integration with Google Books API.
Those tools can be tested with Gradio UI in two dedicated tabs:

* __Google Books - Search__: search books with filters (title, author, isbn, genre) and return a json with the list of books info. Each book has only some basic information ('lite' version of the book), but full information are available with the other tool described below.
Response object looks like this:
```json
{
"kind": "books#volumes",
"totalItems": 1000000,
"items": [
{
"kind": "books#volume",
"id": "pZ49u0fSuGsC",
"etag": "kCfPFbplXAM",
"selfLink": "https://www.googleapis.com/books/v1/volumes/pZ49u0fSuGsC",
"volumeInfo": {
"title": "Professional Test Driven Development with C#",
"subtitle": "Developing Real World Applications with TDD",
"authors": [
"James Bender",
"Jeff McWherter"
],
...
},
...
},
...
]
}
```

* __Google Books - Get Book By Id__: this tool can be useful to retrieve a book by its Google Books ID, returning a json with all information about the book.
In case of success, the returned json looks like a single item from the previous tool's response object (plus some additional fields).
In case of non-existent book, the returned json looks like this:

```json
{
"error": {
"code": 404,
"message": "The volume ID could not be found.",
"errors": [
{
"message": "The volume ID could not be found.",
"domain": "global",
"reason": "notFound"
}
]
}
}
```

Files changed (1) hide show
  1. app.py +100 -2
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import gradio as gr
 
2
  import requests
3
 
4
  import flux
@@ -70,7 +71,56 @@ def calculate(n1, op, n2):
70
  if op == "*": return str(n1 * n2)
71
  if op == "/" and n2 != 0: return str(n1 / n2)
72
  return "Error"
73
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  print(f"DEBUG: Gradio version={gr. __version__}")
76
 
@@ -80,7 +130,7 @@ with gr.Blocks() as demo:
80
 
81
  ### Built from the ground up for LLMs
82
 
83
- This app provides a simple calculator, a dad joke generator, a greeting function, and an image generation feature.
84
 
85
  Read the [project documentation](https://huggingface.co/spaces/Agents-MCP-Hackathon/simple-calculator/blob/main/README.md) to understand its background and motivations.
86
 
@@ -135,6 +185,54 @@ with gr.Blocks() as demo:
135
  outputs=img_output,
136
  api_name="generate_image")
137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  demo.launch(
139
  mcp_server=True,
140
  share=True,
 
1
  import gradio as gr
2
+ import json
3
  import requests
4
 
5
  import flux
 
71
  if op == "*": return str(n1 * n2)
72
  if op == "/" and n2 != 0: return str(n1 / n2)
73
  return "Error"
74
+
75
+
76
+ BASE_GOOGLE_BOOKS_VOLUMES_API = "https://www.googleapis.com/books/v1/volumes"
77
+
78
+ def get_google_book_filtered(title="", author="", genre="", isbn=""):
79
+ """
80
+ Get a filtered list of books from Google Books API.
81
+ It can be filtered by title, author, genre, and ISBN.
82
+ Results are ordered by relevance to the query.
83
+ Only a subset of the volume info is included.
84
+ Only 10 results are returned.
85
+ Args:
86
+ title: str - title of the book
87
+ author: str - author of the book
88
+ genre: str - genre of the book
89
+ isbn: str - ISBN of the book
90
+ Returns:
91
+ list - list of books
92
+ """
93
+ query = "q="
94
+ if title:
95
+ query += f"intitle:{title}"
96
+ if author:
97
+ query += f"+inauthor:{author}"
98
+ if genre:
99
+ query += f"+subject:{genre}"
100
+ if isbn:
101
+ query += f"+isbn:{isbn}"
102
+
103
+ if query != "q=":
104
+ url = f"{BASE_GOOGLE_BOOKS_VOLUMES_API}?{query}"
105
+ url += "&orderBy=relevance"
106
+ url += "&maxResults=10"
107
+ url += "&projection=lite" # only include a subset of the volume info
108
+
109
+ json_res = requests.get(url).json()
110
+ return json.dumps(json_res, indent=2)
111
+
112
+ def get_google_books_book_by_id(book_id):
113
+ """
114
+ Get a book from Google Books API by its ID.
115
+ Args:
116
+ book_id: str - ID of the book
117
+ Returns:
118
+ dict - book
119
+ """
120
+ url = f"{BASE_GOOGLE_BOOKS_VOLUMES_API}/{book_id}?projection=full"
121
+
122
+ json_res = requests.get(url).json()
123
+ return json.dumps(json_res, indent=2)
124
 
125
  print(f"DEBUG: Gradio version={gr. __version__}")
126
 
 
130
 
131
  ### Built from the ground up for LLMs
132
 
133
+ This app provides a simple calculator, a dad joke generator, a greeting function, an image generation feature and a Google Books API integration.
134
 
135
  Read the [project documentation](https://huggingface.co/spaces/Agents-MCP-Hackathon/simple-calculator/blob/main/README.md) to understand its background and motivations.
136
 
 
185
  outputs=img_output,
186
  api_name="generate_image")
187
 
188
+ tab_GB_search = gr.Tab("Google Books - Search")
189
+ with tab_GB_search:
190
+ with gr.Row():
191
+ title_textbox = gr.Textbox(
192
+ label="Title",
193
+ placeholder="Filter by title",
194
+ )
195
+ author_textbox = gr.Textbox(
196
+ label="Author",
197
+ placeholder="Filter by author",
198
+ )
199
+ with gr.Row():
200
+ genre_textbox = gr.Textbox(
201
+ label="Genre",
202
+ placeholder="Filter by genre",
203
+ )
204
+ isbn_textbox = gr.Textbox(
205
+ label="ISBN",
206
+ placeholder="Filter by ISBN",
207
+ )
208
+ with gr.Row():
209
+ search_btn = gr.Button("Search Books")
210
+ with gr.Row():
211
+ output_books_box = gr.Textbox(label="Results")
212
+ search_btn.click(
213
+ fn=get_google_book_filtered,
214
+ inputs=[title_textbox, author_textbox, genre_textbox, isbn_textbox],
215
+ outputs=output_books_box,
216
+ api_name="google_books_search",
217
+ )
218
+
219
+ tab_GB_get_by_id = gr.Tab("Google Books - Get Book By ID")
220
+ with tab_GB_get_by_id:
221
+ with gr.Row():
222
+ id_textbox = gr.Textbox(
223
+ label="ID",
224
+ placeholder="Book ID",
225
+ )
226
+ search_btn = gr.Button("Get Book")
227
+ with gr.Row():
228
+ output_book_box = gr.Textbox(label="Results")
229
+ search_btn.click(
230
+ fn=get_google_books_book_by_id,
231
+ inputs=[id_textbox],
232
+ outputs=output_book_box,
233
+ api_name="google_books_get_by_id",
234
+ )
235
+
236
  demo.launch(
237
  mcp_server=True,
238
  share=True,