Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,35 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
import torch
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
def smart_bot(message):
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 18 |
-
gr.Markdown("<h1 style='text-align: center;'>🤖 Tagalog-English Smart ChatBot
|
| 19 |
-
gr.Markdown("<marquee style='color: black; font-weight: bold;'>Created by Niko Sedan
|
| 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()
|
|
|