Spaces:
Sleeping
Sleeping
Update app.py
Browse files12323678910111213141516171819202122232425262728293031323334353645678910111213141516171819203821222324402542434426272829304849503132333452353637import gradio as grfrom transformers import AutoModelForCausalLM, AutoTokenizerimport torchimport requests
model_name = "microsoft/DialoGPT-small"tokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForCausalLM.from_pretrained(model_name)
def translate(text, source_lang, target_lang): response = requests.post("https://libretranslate.de/translate", data={ "q": text, "source": source_lang, "target": target_lang, "format": "text" }) return response.json()["translatedText"]
def detect_language(text): if any(word in text.lower() for word in ["ako", "ikaw", "bakit", "ng", "hindi", "oo"]): return "tl" return "en"
def chat_bot(message, history=[]): lang = detect_language(message)
if lang == "tl": translated_msg = translate(message, "tl", "en") inputs = tokenizer.encode(translated_msg + tokenizer.eos_token, return_tensors="pt") reply_ids = model.generate(inputs, max_length=100, pad_token_id=tokenizer.eos_token_id) reply_en = tokenizer.decode(reply_ids[:, inputs.shape[-1]:][0], skip_special_tokens=True) reply = translate(reply_en, "en", "tl") else: inputs = tokenizer.encode(message + tokenizer.eos_token, return_tensors="pt") reply_ids = model.generate(inputs, max_length=100, pad_token_id=tokenizer.eos_token_id) reply = tokenizer.decode(reply_ids[:, inputs.shape[-1]:][0], skip_special_tokens=True)API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-2-7b-chat"headers = { # Optional: If rate-limited, use your Hugging Face token here # "Authorization": f"Bearer hf_your_token"}
def smart_bot(message): prompt = f"""You are a helpful, friendly chatbot that understands and replies in the same language used by the user. Reply in English or Tagalog depending on the input.
User: {message}AI:"""
response = requests.post(API_URL, headers=headers, json={"inputs": prompt}) if response.status_code == 200: result = response.json() reply = result[0]["generated_text"].split("AI:")[-1].strip() return reply return reply else: return "β Error: " + response.text
# UI with Gradiowith gr.Blocks(theme=gr.themes.Soft()) as app: gr.Markdown("<h1 style='text-align: center;'>π€ Tagalog-English ChatBot</h1>") gr.Markdown("<marquee style='color:black; font-weight:bold'>Created by Niko Jay Sedan</marquee>") gr.Markdown("π¬ Magtanong sa Tagalog o English. Sasagot sa parehong wika.") gr.Markdown("<h1 style='text-align: center;'>π€ Tagalog-English Smart ChatBot</h1>") gr.Markdown("<marquee style='color: black; font-weight: bold;'>Created by Niko Sedan using Hugging Face LLaMA-2</marquee>")
with gr.Row(): with gr.Column(): user_input = gr.Textbox(label="π Your Message") submit_btn = gr.Button("Send") bot_output = gr.Textbox(label="π€ Bot Reply") user_input = gr.Textbox(label="π¬ Enter your question here") send_btn = gr.Button("Send") bot_reply = gr.Textbox(label="π€ Bot's Reply")
submit_btn.click(fn=chat_bot, inputs=user_input, outputs=bot_output) send_btn.click(fn=smart_bot, inputs=user_input, outputs=bot_reply)
app.launch()app.py+25-42