Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, Request | |
| import gradio as gr | |
| from slack_bolt import App | |
| from slack_bolt.adapter.fastapi import SlackRequestHandler | |
| import os | |
| import logging | |
| from download_judgements import fetch_case_names, fetch_judgements | |
| from interpret_judgement import interpret_judgement, interpret_by_case_title | |
| from simple_kv_store import SimpleKVStore | |
| from datetime import datetime | |
| from fastapi.staticfiles import StaticFiles | |
| from fastapi.responses import FileResponse | |
| #### Gradio | |
| io = gr.Interface( | |
| fn=interpret_by_case_title, | |
| inputs=[ | |
| gr.Dropdown( | |
| fetch_case_names(), label="Select a judement", info="Select a case from latest Supreme Court judgements" | |
| ), | |
| ], | |
| outputs=gr.Textbox(label="Judgement Summary"), | |
| title="Judgement Buzz", | |
| description="Judgement Buzz - Decoding complex Supreme Court rulings with ease.", | |
| allow_flagging= "never" | |
| ) | |
| ### Slack | |
| slack_app = App(token=os.environ.get("SLACK_BOT_TOKEN"), | |
| signing_secret=os.environ.get("SLACK_SIGNING_SECRET")) | |
| def get_case_digest(ack, respond, command): | |
| # Acknowledge command request | |
| ack() | |
| cases = fetch_judgements() | |
| blocks = [ | |
| { | |
| "type": "section", | |
| "text": {"type": "mrkdwn", "text": f"Here are 5 recent judgements we found for you."}, | |
| }, | |
| { | |
| "type": "section", | |
| "text": {"type": "mrkdwn", "text": f"Click button `Summarize` to get quick summary of the judgement"}, | |
| }, | |
| { | |
| "type": "divider" | |
| }, | |
| ] | |
| for case in cases[:5]: | |
| blocks = blocks + [ | |
| { | |
| "type": "section", | |
| "text": { | |
| "type": "mrkdwn", | |
| "text": f"*<{case['link']}| {case['title']}>*\n*Diary no*: {case['diary_no']}", | |
| }, | |
| "accessory": { | |
| "type": "button", | |
| "text": { | |
| "type": "plain_text", | |
| "text": "Summarize", | |
| "emoji": True | |
| }, | |
| "value": case['diary_no'], | |
| "action_id": "summarize_case" | |
| } | |
| }, | |
| { | |
| "type": "divider" | |
| }, | |
| ] | |
| # logging.info(blocks) | |
| resp = respond(text=f"hello", blocks=blocks, response_type='in_channel') | |
| def handle_summarize_action(ack, action, respond): | |
| ack() | |
| case = SimpleKVStore.get(action['value']) | |
| logging.info(f"Case retrieved {case}") | |
| today = datetime.today().strftime("%Y-%m-%d") | |
| summary = interpret_judgement(case['link'], case['diary_no'], case['title']) | |
| blocks = [ | |
| { | |
| "type": "header", | |
| "text": { | |
| "type": "plain_text", | |
| "text": f":newspaper: {case['title']} :newspaper:" | |
| } | |
| }, | |
| { | |
| "type": "context", | |
| "elements": [ | |
| { | |
| "text": f":calendar: *{today}*", | |
| "type": "mrkdwn" | |
| }, | |
| { | |
| "text": "|", | |
| "type": "mrkdwn" | |
| }, | |
| { | |
| "text": ":round_pushpin: New Delhi", | |
| "type": "mrkdwn" | |
| } | |
| ] | |
| }, | |
| { | |
| "type": "divider" | |
| }, | |
| { | |
| "type": "section", | |
| "text": { | |
| "type": "mrkdwn", | |
| "text": f"{summary}" | |
| } | |
| }, | |
| { | |
| "type": "divider" | |
| }, | |
| { | |
| "type": "context", | |
| "elements": [ | |
| { | |
| "type": "mrkdwn", | |
| "text": f":pushpin: Want to know more ? Click <{case['link']}|here> to access full court judgement." | |
| } | |
| ] | |
| } | |
| ] | |
| respond(blocks=blocks, response_type='in_channel', replace_original=False, delete_original=False) | |
| # main | |
| app_handler = SlackRequestHandler(slack_app) | |
| app = FastAPI() | |
| app = gr.mount_gradio_app(app, io, path='/gradio') | |
| async def endpoint(req: Request): | |
| logging.info('recevied slack event') | |
| return await app_handler.handle(req) | |
| app.mount("/static", StaticFiles(directory="static", html=True), name="static") | |
| async def root(): | |
| return FileResponse("static/index.html") | |