Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Token එක Secrets tab එකෙන් load කිරීම
|
| 6 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 7 |
+
model_id = "google/gemma-2b-it"
|
| 8 |
+
|
| 9 |
+
# Model load කිරීම
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=HF_TOKEN)
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, use_auth_token=HF_TOKEN)
|
| 12 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 13 |
+
|
| 14 |
+
# Website topic keywords
|
| 15 |
+
keywords = ["product", "service", "refund", "delivery", "account", "support", "price", "order", "website", "අඩවිය", "සේවාව"]
|
| 16 |
+
|
| 17 |
+
# Chat function with topic restriction
|
| 18 |
+
def chat_with_gemma(message):
|
| 19 |
+
if not any(word in message.lower() for word in keywords):
|
| 20 |
+
return "මට ඔබේ website එක ගැන විතරක් උත්තර දිය හැක. කරුණාකර අපගේ සේවාවන් පිළිබඳව ප්රශ්නයක් අසන්න."
|
| 21 |
+
prompt = f"You are a helpful assistant for the website MyWebsite. Only answer questions related to this website. Question: {message}"
|
| 22 |
+
response = generator(prompt, max_new_tokens=150, do_sample=True, temperature=0.7)[0]["generated_text"]
|
| 23 |
+
return response
|
| 24 |
+
|
| 25 |
+
# Gradio UI
|
| 26 |
+
iface = gr.Interface(
|
| 27 |
+
fn=chat_with_gemma,
|
| 28 |
+
inputs=gr.Textbox(lines=2, placeholder="ඔබේ website එක ගැන ප්රශ්නයක් මෙහි ටයිප් කරන්න..."),
|
| 29 |
+
outputs="text",
|
| 30 |
+
title="MyWebsite Chatbot 🇱🇰",
|
| 31 |
+
description="මෙම chatbot එක ඔබේ website එක පිළිබඳව විතරක් උත්තර දේ. වෙනත් ප්රශ්නවලට උත්තර නොදේ."
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
iface.launch()
|