Noobjay commited on
Commit
5498e60
Β·
verified Β·
1 Parent(s): 1e05e4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -14
app.py CHANGED
@@ -2,33 +2,29 @@ import gradio as gr
2
  import requests
3
  import os
4
 
5
- # Get API token from secrets
6
- hf_token = os.getenv("HF_API_TOKEN")
7
  API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-2-7b-chat-hf"
8
  headers = {
9
- "Authorization": f"Bearer {hf_token}"
10
  }
11
 
12
  def smart_bot(message):
13
- prompt = f"""
14
- You are a helpful AI assistant. Reply in the same language the user speaks.
15
- User: {message}
16
- AI:"""
17
 
18
- response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
19
 
20
  if response.status_code == 200:
21
  output = response.json()
22
  return output[0]["generated_text"].split("AI:")[-1].strip()
23
  else:
24
- return f"❌ Error: {response.text}"
25
 
26
- with gr.Blocks() as app:
27
- gr.Markdown("## πŸ€– Tagalog-English AI ChatBot (LLaMA 2)")
28
  user_input = gr.Textbox(label="πŸ’¬ Your Message")
29
- output = gr.Textbox(label="πŸ€– Bot Reply")
30
- submit = gr.Button("Send")
31
 
32
- submit.click(fn=smart_bot, inputs=user_input, outputs=output)
33
 
34
  app.launch()
 
2
  import requests
3
  import os
4
 
 
 
5
  API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-2-7b-chat-hf"
6
  headers = {
7
+ "Authorization": f"Bearer {os.getenv('HF_API_TOKEN')}"
8
  }
9
 
10
  def smart_bot(message):
11
+ prompt = f"You are a helpful assistant. Reply in the same language used by the user.\nUser: {message}\nAI:"
12
+ payload = {"inputs": prompt}
 
 
13
 
14
+ response = requests.post(API_URL, headers=headers, json=payload)
15
 
16
  if response.status_code == 200:
17
  output = response.json()
18
  return output[0]["generated_text"].split("AI:")[-1].strip()
19
  else:
20
+ return f"❌ Error: {response.status_code} - {response.text}"
21
 
22
+ with gr.Blocks(theme=gr.themes.Soft()) as app:
23
+ gr.Markdown("## πŸ€– Tagalog-English Smart ChatBot")
24
  user_input = gr.Textbox(label="πŸ’¬ Your Message")
25
+ bot_reply = gr.Textbox(label="πŸ€– Bot Reply")
26
+ send_btn = gr.Button("Send")
27
 
28
+ send_btn.click(fn=smart_bot, inputs=user_input, outputs=bot_reply)
29
 
30
  app.launch()