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: str, chat_history: list[tuple[str, str]], max_tokens: int, temperature: float, top_p: float, ) -> tuple[list[tuple[str, str]], str]: """ Generate a response and update chat history. Returns tuple of (new_history, None) to clear input box. """ system_message = "You are a maritime legal assistant with expertise strictly in Indian maritime law. Provide detailed legal advice and information within word limit based on Indian maritime legal principles and regulations." messages = [{"role": "system", "content": system_message}] for user_msg, assistant_msg in chat_history: messages.extend([ {"role": "user", "content": user_msg}, {"role": "assistant", "content": assistant_msg} ]) messages.append({"role": "user", "content": message}) chat_history = chat_history + [(message, None)] response = "" try: for chunk in client.chat_completion( messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p, ): try: if isinstance(chunk, ChatCompletionStreamOutput): content = chunk.choices[0].delta.content if content: response += content chat_history[-1] = (message, response) yield chat_history, "" if chunk.choices[0].finish_reason == 'stop': break elif isinstance(chunk, dict): content = chunk.get('choices', [{}])[0].get('delta', {}).get('content') if content: response += content chat_history[-1] = (message, response) yield chat_history, "" if chunk.get('choices', [{}])[0].get('finish_reason') == 'stop': break elif isinstance(chunk, str) and chunk.strip(): response += chunk chat_history[-1] = (message, response) yield chat_history, "" except Exception as e: print(f"Error processing chunk: {e}") continue if not response: chat_history[-1] = (message, "I apologize, but I couldn't generate a response. Please try again.") yield chat_history, "" except Exception as e: error_msg = f"An error occurred: {str(e)}" chat_history[-1] = (message, error_msg) yield chat_history, "" # [Previous imports and respond function remain unchanged] custom_css = """ /* Global styles */ .gradio-container { background-color: #1a365d !important; font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif !important; } /* Header styling */ .header-container { text-align: center; padding: 1rem 0; margin-bottom: 1rem; border-bottom: 2px solid rgba(255, 255, 255, 0.1); } .header-title { color: #ffffff; font-size: 2rem; margin-bottom: 0.3rem; font-family: inherit; } .header-subtitle { color: #e6f3ff; font-size: 1rem; margin-bottom: 0.2rem; font-family: inherit; } /* Sidebar styling */ .sidebar { background: #e6f3ff !important; border-radius: 8px !important; padding: 15px !important; border: 1px solid rgba(176, 226, 255, 0.2) !important; height: fit-content !important; } .sidebar-title { color: #1a365d !important; font-size: 1.1rem !important; margin-bottom: 0.8rem !important; padding-bottom: 0.4rem !important; border-bottom: 2px solid rgba(26, 54, 93, 0.1) !important; font-family: inherit !important; } /* Example queries styling */ .example-queries { margin-bottom: 1.5rem !important; } .example-query-button { background-color: #cce7ff !important; color: #1a365d !important; border: none !important; margin: 3px 0 !important; padding: 6px 10px !important; border-radius: 4px !important; text-align: left !important; width: 100% !important; cursor: pointer !important; transition: background-color 0.3s ease !important; font-size: 0.9rem !important; font-family: inherit !important; } .example-query-button:hover { background-color: #b0e2ff !important; } /* Chat container */ .chat-container { background: #e6f3ff !important; border-radius: 8px !important; padding: 15px !important; height: 300px !important; overflow-y: auto !important; border: 1px solid rgba(176, 226, 255, 0.2) !important; backdrop-filter: blur(10px) !important; font-family: inherit !important; } /* Message styling */ .message.user, .message.bot { padding: 8px 12px !important; margin: 6px 0 !important; border-radius: 6px !important; color: #1a365d !important; font-size: 0.9rem !important; font-family: inherit !important; line-height: 1.5 !important; } .message.user { background-color: #cce7ff !important; } .message.bot { background-color: #e6f3ff !important; } /* Chat input styling */ textarea { background-color: #e6f3ff !important; border: 1px solid rgba(176, 226, 255, 0.3) !important; border-radius: 6px !important; padding: 8px !important; color: #1a365d !important; font-size: 0.9rem !important; font-family: inherit !important; } /* Button styling */ .gr-button { background-color: #cce7ff !important; color: #1a365d !important; border: none !important; padding: 6px 12px !important; font-size: 0.9rem !important; font-family: inherit !important; border-radius: 4px !important; } .gr-button:hover { background-color: #1a365d !important; color: #ffffff !important; } /* Markdown text styling */ .prose { font-family: inherit !important; } /* All text elements */ p, span, div { font-family: inherit !important; } """ def handle_example_click(example_query: str): """Handle example query click by returning the query and empty chat history""" return example_query, [] # Main application with gr.Blocks(css=custom_css, theme=gr.themes.Base()) as demo: # Header gr.HTML("""
AI-powered assistance for Indian maritime law queries
This chatbot uses Fine-tuned LLAMA-3.1 model personalised specifically to provide assistance with Indian maritime legal queries.