import os import requests import gradio as gr def get_book_recommendations(prompt: str) -> str: """ Fetches a book recommendation from Google Books API. Args: prompt: User search query, e.g., "thriller novel by Gillian Flynn" Returns: Top book recommendation in formatted text. """ api_key = os.getenv("GOOGLE_BOOKS_API_KEY") if not api_key: return "API key not found." search_query = prompt.strip() url = f"https://www.googleapis.com/books/v1/volumes?q={search_query}&key={api_key}" try: response = requests.get(url, timeout=10) response.raise_for_status() books = response.json().get("items", []) if not books: return "No books found." volume_info = books[0].get("volumeInfo", {}) title = volume_info.get("title", "Unknown Title") authors = ", ".join(volume_info.get("authors", ["Unknown Author"])) description = volume_info.get("description", "No description available.") return f"📚 **{title}** by *{authors}*\n\n{description}" except Exception as e: return f"Error: {str(e)}" # Define Gradio interface book_interface = gr.Interface( fn=get_book_recommendations, inputs=gr.Textbox(label="Enter your book query"), outputs=gr.Markdown(label="Top Recommendation"), api_name="get_book_recommendations" ) # Combine with other interfaces if needed demo = gr.TabbedInterface( [ book_interface, # add more tools here as you expand your agent system ], ["Book Finder"] ) if __name__ == "__main__": demo.launch(mcp_server=True)