import gradio as gr import aiohttp # Configuration for the licensing service LICENSING_SERVICE_BASE = 'https://gbdlicensing01containerapp.delightfulocean-00db0bf9.westeurope.azurecontainerapps.io' async def call_licensing_api( endpoint: str, index_name: str, api_key: str, input_text: str = None, question: str = None, ) -> dict: if endpoint not in ("create_index", "answer_question"): raise ValueError("endpoint must be either 'create_index' or 'answer_question'") if endpoint == "create_index" and input_text is None: raise ValueError("When calling 'create_index', you must provide input_text") if endpoint == "answer_question" and question is None: raise ValueError("When calling 'answer_question', you must provide question") headers = {"X-API-Key": api_key, "Content-Type": "application/json"} payload = {"index_name": index_name} if endpoint == "create_index": payload["input_text"] = input_text else: payload["question"] = question timeout = aiohttp.ClientTimeout(total=60) async with aiohttp.ClientSession(timeout=timeout) as session: url = f"{LICENSING_SERVICE_BASE}/{endpoint}" async with session.post(url, json=payload, headers=headers) as resp: resp.raise_for_status() return await resp.json() async def get_answer(api_key: str, final_selection: str, question: str): """Fetches answer and summary for the question based on the final topic selection.""" index_map = { 'Licensing Foundamentals': 'licensing-fundamentals' # other mappings if needed } index_name = index_map.get(final_selection) response = await call_licensing_api( endpoint="answer_question", index_name=index_name, api_key=api_key, question=question ) # Expecting {'answer': ..., 'summary': ...} return response.get('summary', ''), response.get('answer', '') def main(): with gr.Blocks() as demo: # API key input api_key_input = gr.Textbox( label='Licensing Service API Key', placeholder='Paste your API key here', type='password' ) # Step 1: Primary topic selection primary_topic = gr.Radio( choices=['License Program', 'Products', 'Customer info'], label='What would you like to learn about?' ) # Step 2: Product type, shown only if 'Products' selected product_container = gr.Column(visible=False) with product_container: product_type = gr.Radio( choices=['Software', 'Online services'], label='Which products?' ) # Step 3: Service type, shown only if 'Online services' selected service_container = gr.Column(visible=False) with service_container: service_type = gr.Radio( choices=['Licensing Foundamentals'], label='Select an online service' ) # Step 4: QA, shown only after service_type selection qa_container = gr.Column(visible=False) with qa_container: 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, service_type, question], outputs=[summary, answer] ) # Callbacks to reveal steps primary_topic.change( fn=lambda sel: gr.update(visible=(sel=='Products')), inputs=[primary_topic], outputs=[product_container] ) # Hide downstream when primary changes away primary_topic.change( fn=lambda sel: gr.update(visible=False), inputs=[primary_topic], outputs=[service_container, qa_container] ) product_type.change( fn=lambda sel: gr.update(visible=(sel=='Online services')), inputs=[product_type], outputs=[service_container] ) product_type.change( fn=lambda sel: gr.update(visible=False), inputs=[product_type], outputs=[qa_container] ) service_type.change( fn=lambda sel: gr.update(visible=(sel=='Licensing Foundamentals')), inputs=[service_type], outputs=[qa_container] ) demo.launch(share=True) if __name__ == '__main__': main()