import gradio as gr import os from huggingface_hub import InferenceClient from huggingface_hub.inference._generated.types.chat_completion import ChatCompletionStreamOutput MODEL = "nomiChroma3.1" client = InferenceClient("HuggingFaceH4/zephyr-7b-beta") def respond( message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p, ): messages = [{"role": "system", "content": system_message}] for val in history: if val[0]: messages.append({"role": "user", "content": val[0]}) if val[1]: messages.append({"role": "assistant", "content": val[1]}) messages.append({"role": "user", "content": message}) response = "" try: for message in client.chat_completion( messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p, ): try: if isinstance(message, ChatCompletionStreamOutput): content = message.choices[0].delta.content if content is not None: response += content yield response if message.choices[0].finish_reason == 'stop': break elif isinstance(message, dict): content = message.get('choices', [{}])[0].get('delta', {}).get('content') if content: response += content yield response if message.get('choices', [{}])[0].get('finish_reason') == 'stop': break elif isinstance(message, str): if message.strip(): response += message yield response except Exception as e: print(f"Error processing message: {e}") continue if response: yield response except Exception as e: print(f"An error occurred: {e}") yield f"An error occurred: {e}" # Custom CSS for maritime theme with sidebar custom_css = """ /* Global styles */ .gradio-container { background-color: #e6f3ff !important; } /* Header styling */ .header-container { text-align: center; padding: 2rem 0; margin-bottom: 2rem; border-bottom: 2px solid rgba(26, 54, 93, 0.1); } .header-title { color: #1a365d; font-size: 2.5rem; margin-bottom: 0.5rem; } .header-subtitle { color: #2c5282; font-size: 1.1rem; } /* Sidebar styling */ .sidebar { background: rgba(255, 255, 255, 0.9) !important; border-radius: 12px !important; padding: 20px !important; border: 1px solid rgba(26, 54, 93, 0.1) !important; height: fit-content !important; } .sidebar-title { font-size: 1.2rem !important; color: #1a365d !important; margin-bottom: 1rem !important; padding-bottom: 0.5rem !important; border-bottom: 2px solid rgba(26, 54, 93, 0.1) !important; } /* Chat container */ .chat-container { background: rgba(255, 255, 255, 0.7) !important; border-radius: 12px !important; padding: 20px !important; height: 600px !important; overflow-y: auto !important; } /* Message styling */ .message.user, .message.bot { padding: 12px 16px !important; margin: 8px 0 !important; border-radius: 8px !important; } .message.user { background-color: #e1f0ff !important; } .message.bot { background-color: #ffffff !important; } /* Input styling */ textarea { background-color: #ffffff !important; border: 1px solid rgba(26, 54, 93, 0.2) !important; border-radius: 8px !important; padding: 12px !important; } /* Slider styling */ .gr-slider { margin: 1rem 0 !important; } """ # Main application with gr.Blocks(css=custom_css, theme=gr.themes.Base()) as demo: # Header gr.HTML("""

Maritime Legal Compliance

AI-powered assistance for Indian maritime law queries

""") # Main layout with sidebar with gr.Row(): # Sidebar with gr.Column(scale=1, elem_classes="sidebar"): gr.Markdown("### Configuration", elem_classes="sidebar-title") system_message = gr.Textbox( value="You are a maritime legal assistant with expertise strictly in Indian maritime law. Provide detailed legal advice and information based on Indian maritime legal principles and regulations.", label="System Prompt", lines=4 ) max_tokens = gr.Slider( minimum=1, maximum=2048, value=512, step=1, label="Response Length" ) temperature = gr.Slider( minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Creativity Level" ) top_p = gr.Slider( minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Response Focus" ) gr.Markdown("### Example Queries", elem_classes="sidebar-title") examples = gr.Examples( examples=[ ["What are the key regulations governing ports in India?"], ["Explain the concept of cabotage in Indian maritime law."], ["What are the legal requirements for registering a vessel in India?"], ], inputs=[system_message], label="Click on any example to load it" ) # Main chat area with gr.Column(scale=3): chatbot = gr.Chatbot(height=500, elem_classes="chat-container") msg = gr.Textbox( show_label=False, placeholder="Type your maritime law query here...", container=False ) with gr.Row(): submit = gr.Button("Send", variant="primary") clear = gr.Button("Clear") # Event handlers submit_click = submit.click( respond, inputs=[msg, chatbot, system_message, max_tokens, temperature, top_p], outputs=chatbot ) msg_submit = msg.submit( respond, inputs=[msg, chatbot, system_message, max_tokens, temperature, top_p], outputs=chatbot ) clear.click(lambda: None, None, chatbot, queue=False) # Launch the app if __name__ == "__main__": demo.launch()