Spaces:
Sleeping
Sleeping
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354import gradio as grfrom transformers import AutoModelForCausalLM, AutoTokenizerimport torchimport requestsmodel_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)return reply# 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.")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")submit_btn.click(fn=chat_bot, inputs=user_input, outputs=bot_output)app.launch()12345678910111213141516171819202122232425262728293031323334353637import gradio as grimport requestsAPI_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 replyelse:return "β Error: " + response.textwith gr.Blocks(theme=gr.themes.Soft()) as app: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="π¬ Enter your question here")send_btn = gr.Button("Send")bot_reply = gr.Textbox(label="π€ Bot's Reply")send_btn.click(fn=smart_bot, inputs=user_input, outputs=bot_reply)app.launch()
app.py+25-42