Noobjay commited on
Commit
6fde106
Β·
verified Β·
1 Parent(s): 5f4e717

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -42
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
- 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()
 
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()