Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,43 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
device_map="cpu",
|
| 12 |
-
torch_dtype=torch.float32,
|
| 13 |
-
low_cpu_mem_usage=True
|
| 14 |
)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
tokenizer=tokenizer,
|
| 20 |
-
device=-1
|
| 21 |
-
)
|
| 22 |
|
| 23 |
-
def chat_fn(
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
iface = gr.Interface(
|
| 34 |
fn=chat_fn,
|
| 35 |
-
inputs=gr.Textbox(lines=4, placeholder="Nhập câu hỏi..."),
|
| 36 |
outputs="text",
|
| 37 |
-
title="
|
| 38 |
)
|
| 39 |
|
| 40 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# Lấy key từ Secret
|
| 6 |
+
API_TOKEN = os.environ.get("HF_KEY")
|
| 7 |
+
API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-3.2-1B-Instruct"
|
| 8 |
+
HEADERS = {"Authorization": f"Bearer {API_TOKEN}"}
|
| 9 |
|
| 10 |
+
SYSTEM_PROMPT = (
|
| 11 |
+
"Bạn là một chatbot chuyên TÓM TẮT và TỔNG HỢP ý chính. "
|
| 12 |
+
"Nhiệm vụ: nhận câu hỏi hoặc đoạn văn bản và trả lời ngắn gọn, "
|
| 13 |
+
"liệt kê các điểm quan trọng nhất.\n\n"
|
|
|
|
|
|
|
|
|
|
| 14 |
)
|
| 15 |
|
| 16 |
+
def query(payload):
|
| 17 |
+
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
| 18 |
+
return response.json()
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
def chat_fn(user_input):
|
| 21 |
+
prompt = SYSTEM_PROMPT + "Người dùng hỏi: " + user_input + "\n\nTóm tắt & tổng hợp:"
|
| 22 |
+
data = query({
|
| 23 |
+
"inputs": prompt,
|
| 24 |
+
"parameters": {
|
| 25 |
+
"max_new_tokens": 200,
|
| 26 |
+
"temperature": 0.7,
|
| 27 |
+
"top_p": 0.9,
|
| 28 |
+
}
|
| 29 |
+
})
|
| 30 |
+
# Trả về text
|
| 31 |
+
try:
|
| 32 |
+
return data[0]["generated_text"]
|
| 33 |
+
except Exception:
|
| 34 |
+
return str(data)
|
| 35 |
|
| 36 |
iface = gr.Interface(
|
| 37 |
fn=chat_fn,
|
| 38 |
+
inputs=gr.Textbox(lines=4, placeholder="Nhập đoạn văn hoặc câu hỏi..."),
|
| 39 |
outputs="text",
|
| 40 |
+
title="🤖 Chatbot Tóm Tắt & Tổng Hợp Ý Chính (HF API)"
|
| 41 |
)
|
| 42 |
|
| 43 |
if __name__ == "__main__":
|