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("""
AI-powered assistance for Indian maritime law queries