""" Embedding functionality for Norwegian RAG chatbot. Provides utilities for embedding the chatbot in external websites. """ import os from typing import Dict, Optional class EmbedGenerator: """ Generates embedding code for integrating the chatbot into external websites. """ def __init__( self, space_name: Optional[str] = None, username: Optional[str] = None, height: int = 500, width: str = "100%" ): """ Initialize the embed generator. Args: space_name: Hugging Face Space name username: Hugging Face username height: Default iframe height in pixels width: Default iframe width (can be pixels or percentage) """ self.space_name = space_name or "norwegian-rag-chatbot" self.username = username or "username" self.height = height self.width = width def get_iframe_code( self, height: Optional[int] = None, width: Optional[str] = None ) -> str: """ Generate iframe embed code. Args: height: Optional custom height width: Optional custom width Returns: HTML iframe code """ h = height or self.height w = width or self.width return f'' def get_javascript_widget_code(self) -> str: """ Generate JavaScript widget embed code. Returns: HTML script tag for widget """ return f'' def get_direct_url(self) -> str: """ Get direct URL to the Hugging Face Space. Returns: URL to the Hugging Face Space """ return f"https://huggingface.co/spaces/{self.username}/{self.space_name}" def get_embed_options(self) -> Dict[str, str]: """ Get all embedding options. Returns: Dictionary of embedding options """ return { "iframe": self.get_iframe_code(), "javascript": self.get_javascript_widget_code(), "url": self.get_direct_url() } def update_space_info(self, username: str, space_name: str) -> None: """ Update Hugging Face Space information. Args: username: Hugging Face username space_name: Hugging Face Space name """ self.username = username self.space_name = space_name def create_embed_html_file( embed_generator: EmbedGenerator, output_path: str = "/home/ubuntu/chatbot_project/embed_example.html" ) -> str: """ Create an HTML file with embedding examples. Args: embed_generator: EmbedGenerator instance output_path: Path to save the HTML file Returns: Path to the created HTML file """ html_content = f"""
This page demonstrates how to embed the Norwegian RAG Chatbot into your website. There are multiple ways to integrate the chatbot, depending on your needs.
The simplest way to embed the chatbot is using an iFrame. Copy and paste the following code into your HTML:
{embed_generator.get_iframe_code()}
For a more integrated experience, you can use the JavaScript widget. Copy and paste the following code into your HTML:
{embed_generator.get_javascript_widget_code()}
The widget will appear below once the page is hosted on a web server:
You can also provide a direct link to the chatbot:
{embed_generator.get_direct_url()}
You can customize the appearance of the embedded chatbot by modifying the iFrame dimensions:
{embed_generator.get_iframe_code(height=600, width="80%")}