Noobjay commited on
Commit
a203b87
·
verified ·
1 Parent(s): 67ed24a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -13
app.py CHANGED
@@ -1,22 +1,35 @@
 
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
- import torch
4
 
5
- # Libre at public na chatbot model sa Hugging Face
6
- model_name = "microsoft/DialoGPT-small"
7
 
8
- tokenizer = AutoTokenizer.from_pretrained(model_name)
9
- model = AutoModelForCausalLM.from_pretrained(model_name)
 
10
 
11
  def smart_bot(message):
12
- inputs = tokenizer.encode(message + tokenizer.eos_token, return_tensors="pt")
13
- reply_ids = model.generate(inputs, max_length=100, pad_token_id=tokenizer.eos_token_id)
14
- reply = tokenizer.decode(reply_ids[:, inputs.shape[-1]:][0], skip_special_tokens=True)
15
- return reply
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  with gr.Blocks(theme=gr.themes.Soft()) as app:
18
- gr.Markdown("<h1 style='text-align: center;'>🤖 Tagalog-English Smart ChatBot (Free Demo)</h1>")
19
- gr.Markdown("<marquee style='color: black; font-weight: bold;'>Created by Niko Sedan - Using Microsoft DialoGPT</marquee>")
20
 
21
  with gr.Row():
22
  user_input = gr.Textbox(label="💬 Type your message here")
@@ -26,4 +39,3 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
26
  send_btn.click(fn=smart_bot, inputs=user_input, outputs=bot_reply)
27
 
28
  app.launch()
29
-
 
1
+ import os
2
  import gradio as gr
3
+ import requests
 
4
 
5
+ API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-2-7b-chat"
6
+ API_TOKEN = os.getenv("HF_API_TOKEN")
7
 
8
+ headers = {
9
+ "Authorization": f"Bearer {API_TOKEN}"
10
+ }
11
 
12
  def smart_bot(message):
13
+ prompt = f"""
14
+ You are a friendly, helpful AI assistant. Reply in the same language as the question — Tagalog if the user types in Tagalog, English if in English. Use natural, polite tone.
15
+ User: {message}
16
+ AI:"""
17
+
18
+ payload = {"inputs": prompt}
19
+ response = requests.post(API_URL, headers=headers, json=payload)
20
+
21
+ if response.status_code == 200:
22
+ try:
23
+ reply = response.json()[0]["generated_text"].split("AI:")[-1].strip()
24
+ return reply
25
+ except Exception:
26
+ return "⚠️ Error: Unexpected response format."
27
+ else:
28
+ return "❌ Error: " + response.text
29
 
30
  with gr.Blocks(theme=gr.themes.Soft()) as app:
31
+ gr.Markdown("<h1 style='text-align: center;'>🤖 Tagalog-English Smart ChatBot</h1>")
32
+ gr.Markdown("<marquee style='color: black; font-weight: bold;'>Created by Niko Sedan using Hugging Face LLaMA 2</marquee>")
33
 
34
  with gr.Row():
35
  user_input = gr.Textbox(label="💬 Type your message here")
 
39
  send_btn.click(fn=smart_bot, inputs=user_input, outputs=bot_reply)
40
 
41
  app.launch()