Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,54 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
model_name = "bryanlimy/tagalog-english-chatbot"
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def chat_bot(message, history=[]):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
return reply
|
| 15 |
|
| 16 |
-
#
|
| 17 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 18 |
-
gr.Markdown("<h1 style='text-align: center;'>π€ Tagalog-English
|
| 19 |
-
gr.Markdown("<marquee style='color:black; font-weight:bold'>Created by Niko Jay Sedan
|
| 20 |
-
gr.Markdown("
|
| 21 |
|
| 22 |
with gr.Row():
|
| 23 |
with gr.Column():
|
| 24 |
-
user_input = gr.Textbox(label="π
|
| 25 |
submit_btn = gr.Button("Send")
|
| 26 |
-
bot_output = gr.Textbox(label="π€
|
| 27 |
|
| 28 |
submit_btn.click(fn=chat_bot, inputs=user_input, outputs=bot_output)
|
| 29 |
|
| 30 |
-
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
+
import requests
|
| 5 |
|
| 6 |
+
model_name = "microsoft/DialoGPT-small"
|
|
|
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 9 |
|
| 10 |
+
def translate(text, source_lang, target_lang):
|
| 11 |
+
response = requests.post("https://libretranslate.de/translate", data={
|
| 12 |
+
"q": text,
|
| 13 |
+
"source": source_lang,
|
| 14 |
+
"target": target_lang,
|
| 15 |
+
"format": "text"
|
| 16 |
+
})
|
| 17 |
+
return response.json()["translatedText"]
|
| 18 |
+
|
| 19 |
+
def detect_language(text):
|
| 20 |
+
if any(word in text.lower() for word in ["ako", "ikaw", "bakit", "ng", "hindi", "oo"]):
|
| 21 |
+
return "tl"
|
| 22 |
+
return "en"
|
| 23 |
+
|
| 24 |
def chat_bot(message, history=[]):
|
| 25 |
+
lang = detect_language(message)
|
| 26 |
+
|
| 27 |
+
if lang == "tl":
|
| 28 |
+
translated_msg = translate(message, "tl", "en")
|
| 29 |
+
inputs = tokenizer.encode(translated_msg + tokenizer.eos_token, return_tensors="pt")
|
| 30 |
+
reply_ids = model.generate(inputs, max_length=100, pad_token_id=tokenizer.eos_token_id)
|
| 31 |
+
reply_en = tokenizer.decode(reply_ids[:, inputs.shape[-1]:][0], skip_special_tokens=True)
|
| 32 |
+
reply = translate(reply_en, "en", "tl")
|
| 33 |
+
else:
|
| 34 |
+
inputs = tokenizer.encode(message + tokenizer.eos_token, return_tensors="pt")
|
| 35 |
+
reply_ids = model.generate(inputs, max_length=100, pad_token_id=tokenizer.eos_token_id)
|
| 36 |
+
reply = tokenizer.decode(reply_ids[:, inputs.shape[-1]:][0], skip_special_tokens=True)
|
| 37 |
+
|
| 38 |
return reply
|
| 39 |
|
| 40 |
+
# UI with Gradio
|
| 41 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 42 |
+
gr.Markdown("<h1 style='text-align: center;'>π€ Tagalog-English ChatBot</h1>")
|
| 43 |
+
gr.Markdown("<marquee style='color:black; font-weight:bold'>Created by Niko Jay Sedan</marquee>")
|
| 44 |
+
gr.Markdown("π¬ Magtanong sa Tagalog o English. Sasagot sa parehong wika.")
|
| 45 |
|
| 46 |
with gr.Row():
|
| 47 |
with gr.Column():
|
| 48 |
+
user_input = gr.Textbox(label="π Your Message")
|
| 49 |
submit_btn = gr.Button("Send")
|
| 50 |
+
bot_output = gr.Textbox(label="π€ Bot Reply")
|
| 51 |
|
| 52 |
submit_btn.click(fn=chat_bot, inputs=user_input, outputs=bot_output)
|
| 53 |
|
| 54 |
+
app.launch()
|