import gradio as gr from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline import os # Token එක Secrets tab එකෙන් load කිරීම HF_TOKEN = os.getenv("HF_TOKEN") model_id = "google/gemma-2b-it" # Model load කිරීම tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=HF_TOKEN) model = AutoModelForCausalLM.from_pretrained(model_id, use_auth_token=HF_TOKEN) generator = pipeline("text-generation", model=model, tokenizer=tokenizer) # Website topic keywords keywords = ["product", "service", "refund", "delivery", "account", "support", "price", "order", "website", "අඩවිය", "සේවාව"] # Chat function with topic restriction def chat_with_gemma(message): if not any(word in message.lower() for word in keywords): return "මට ඔබේ website එක ගැන විතරක් උත්තර දිය හැක. කරුණාකර අපගේ සේවාවන් පිළිබඳව ප්‍රශ්නයක් අසන්න." prompt = f"You are a helpful assistant for the website MyWebsite. Only answer questions related to this website. Question: {message}" response = generator(prompt, max_new_tokens=150, do_sample=True, temperature=0.7)[0]["generated_text"] return response # Gradio UI iface = gr.Interface( fn=chat_with_gemma, inputs=gr.Textbox(lines=2, placeholder="ඔබේ website එක ගැන ප්‍රශ්නයක් මෙහි ටයිප් කරන්න..."), outputs="text", title="MyWebsite Chatbot 🇱🇰", description="මෙම chatbot එක ඔබේ website එක පිළිබඳව විතරක් උත්තර දේ. වෙනත් ප්‍රශ්නවලට උත්තර නොදේ." ) iface.launch()