Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| import aiohttp | |
| import asyncio | |
| LICENSING_SERVICE_BASE = os.getenv( | |
| "LICENSING_SERVICE_BASE", | |
| "https://gbdlicensing01containerapp.delightfulocean-00db0bf9.westeurope.azurecontainerapps.io" | |
| ).rstrip("/") | |
| # Map from human topic index name | |
| INDEX_MAP = { | |
| "Licensing Fundamentals": "licensing-fundamentals", | |
| # add more as you onboard them | |
| } | |
| async def call_licensing_api( | |
| endpoint: str, | |
| api_key: str, | |
| payload: dict, | |
| ) -> dict: | |
| """Generic POST to /create_index, /answer_question or /classify_topic.""" | |
| url = f"{LICENSING_SERVICE_BASE}/{endpoint}" | |
| headers = {"X-API-Key": api_key, "Content-Type": "application/json"} | |
| timeout = aiohttp.ClientTimeout(total=60) | |
| async with aiohttp.ClientSession(timeout=timeout) as session: | |
| async with session.post(url, json=payload, headers=headers) as resp: | |
| resp.raise_for_status() | |
| return await resp.json() | |
| async def classify_topic(api_key: str, question: str) -> str: | |
| """Ask your service to pick exactly one topic from INDEX_MAP.keys().""" | |
| payload = { | |
| "question": question, | |
| "topics": list(INDEX_MAP.keys()) | |
| } | |
| resp = await call_licensing_api("classify_topic", api_key, payload) | |
| return resp.get("topic", "") | |
| async def get_answer(api_key: str, selected_topic: str, question: str): | |
| """Get summary + answer, auto-classifying if needed.""" | |
| if not selected_topic or selected_topic not in INDEX_MAP: | |
| selected_topic = await classify_topic(api_key, question) | |
| index_name = INDEX_MAP[selected_topic] | |
| payload = {"index_name": index_name, "question": question} | |
| resp = await call_licensing_api("answer_question", api_key, payload) | |
| return resp.get("summary", ""), resp.get("answer", "") | |
| async def main(): | |
| host = os.getenv("HOST", "0.0.0.0") | |
| port = int(os.getenv("PORT", 7860)) | |
| with gr.Blocks() as demo: | |
| api_key_input = gr.Textbox( | |
| label="Licensing Service API Key", | |
| placeholder="Paste your API key here", | |
| type="password" | |
| ) | |
| topic = gr.Dropdown( | |
| choices=list(INDEX_MAP.keys()), | |
| label="Topic (optional)", | |
| value=None # no placeholder—None means “no selection” | |
| ) | |
| question = gr.Textbox( | |
| lines=3, | |
| placeholder="Type your question here…", | |
| label="Your Question" | |
| ) | |
| summary = gr.Textbox( | |
| lines=2, | |
| interactive=False, | |
| label="Summary" | |
| ) | |
| answer = gr.Textbox( | |
| lines=5, | |
| interactive=False, | |
| label="Answer" | |
| ) | |
| submit = gr.Button("Submit") | |
| submit.click( | |
| fn=get_answer, | |
| inputs=[api_key_input, topic, question], | |
| outputs=[summary, answer] | |
| ) | |
| demo.launch( | |
| server_name=host, | |
| server_port=port, | |
| share=True | |
| ) | |
| if __name__ == "__main__": | |
| asyncio.run(main()) | |