Google Books API Integration (#1)
Browse files- Google Books API Integration (2dc69e7754ee8a85a388db228e0399042d381697)
Co-authored-by: Alessio Chessa <[email protected]>
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,
|
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,
|