Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,37 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
-
import torch
|
| 4 |
import requests
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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="
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
-
|
| 53 |
|
| 54 |
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
|
| 4 |
+
API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-2-7b-chat"
|
| 5 |
+
headers = {
|
| 6 |
+
# Optional: If rate-limited, use your Hugging Face token here
|
| 7 |
+
# "Authorization": f"Bearer hf_your_token"
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
def smart_bot(message):
|
| 11 |
+
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
User: {message}
|
| 14 |
+
AI:"""
|
| 15 |
+
|
| 16 |
+
response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
|
| 17 |
+
|
| 18 |
+
if response.status_code == 200:
|
| 19 |
+
result = response.json()
|
| 20 |
+
reply = result[0]["generated_text"].split("AI:")[-1].strip()
|
| 21 |
+
return reply
|
| 22 |
+
else:
|
| 23 |
+
return "β Error: " + response.text
|
| 24 |
|
|
|
|
| 25 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 26 |
+
gr.Markdown("<h1 style='text-align: center;'>π€ Tagalog-English Smart ChatBot</h1>")
|
| 27 |
+
gr.Markdown("<marquee style='color: black; font-weight: bold;'>Created by Niko Sedan using Hugging Face LLaMA-2</marquee>")
|
|
|
|
| 28 |
|
| 29 |
with gr.Row():
|
| 30 |
with gr.Column():
|
| 31 |
+
user_input = gr.Textbox(label="π¬ Enter your question here")
|
| 32 |
+
send_btn = gr.Button("Send")
|
| 33 |
+
bot_reply = gr.Textbox(label="π€ Bot's Reply")
|
| 34 |
|
| 35 |
+
send_btn.click(fn=smart_bot, inputs=user_input, outputs=bot_reply)
|
| 36 |
|
| 37 |
app.launch()
|