Global / app.py
VIATEUR-AI's picture
Update app.py
4c3af6e verified
import gradio as gr
import json
DB_FILE = "books.json"
# Load database
def load_books():
with open(DB_FILE, "r") as f:
return json.load(f)
# Search books
def search_books(query):
books = load_books()
results = []
for b in books:
if query.lower() in b["title"].lower():
results.append(f"πŸ“˜ {b['title']}\nπŸ”— {b['link']}")
return "\n\n".join(results) if results else "No books found ❌"
# Filter by level (P or S)
def filter_level(level):
books = load_books()
results = [b for b in books if b["class"] == level]
if not results:
return "No books found"
return "\n\n".join([f"πŸ“˜ {b['title']}\nπŸ”— {b['link']}" for b in results])
# Show all books
def all_books():
books = load_books()
return "\n\n".join([f"πŸ“˜ {b['title']}\nπŸ”— {b['link']}" for b in books])
# Open REB directly
def open_reb():
return "πŸ‘‰ Visit REB official site: https://www.reb.rw"
# UI
with gr.Blocks() as app:
gr.Markdown("# πŸ“š Rwanda Online Library Platform")
gr.Markdown("Powered by REB + AI Library System")
with gr.Tab("πŸ” Search Books"):
q = gr.Textbox(label="Search book (P1–S6)")
out = gr.Textbox()
gr.Button("Search").click(search_books, q, out)
with gr.Tab("πŸ“‚ Filter by Level"):
level = gr.Dropdown(["P", "S"], label="Select Level (P=Primary, S=Secondary)")
out2 = gr.Textbox()
gr.Button("Filter").click(filter_level, level, out2)
with gr.Tab("πŸ“š All Books"):
out3 = gr.Textbox()
gr.Button("Show All Books").click(all_books, None, out3)
with gr.Tab("🌐 REB Official"):
out4 = gr.Textbox()
gr.Button("Open REB").click(open_reb, None, out4)
gr.Markdown("πŸ“Œ Data Source: Rwanda Basic Education Board (REB)")
app.launch()