Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 {
|
| 10 |
}
|
| 11 |
|
| 12 |
def smart_bot(message):
|
| 13 |
-
prompt = f""
|
| 14 |
-
|
| 15 |
-
User: {message}
|
| 16 |
-
AI:"""
|
| 17 |
|
| 18 |
-
response = requests.post(API_URL, headers=headers, json=
|
| 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
|
| 28 |
user_input = gr.Textbox(label="π¬ Your Message")
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 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()
|