NV9523 commited on
Commit
715fbe6
·
verified ·
1 Parent(s): 6ba3493

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -27
app.py CHANGED
@@ -1,40 +1,43 @@
1
- import torch
2
- from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
  import gradio as gr
 
 
4
 
5
- model_id = "meta-llama/Llama-3.2-1B-Instruct"
 
 
 
6
 
7
- # Load model
8
- tokenizer = AutoTokenizer.from_pretrained(model_id)
9
- model = AutoModelForCausalLM.from_pretrained(
10
- model_id,
11
- device_map="cpu",
12
- torch_dtype=torch.float32,
13
- low_cpu_mem_usage=True
14
  )
15
 
16
- pipe = pipeline(
17
- "text-generation",
18
- model=model,
19
- tokenizer=tokenizer,
20
- device=-1
21
- )
22
 
23
- def chat_fn(prompt):
24
- outputs = pipe(
25
- prompt,
26
- max_new_tokens=200,
27
- do_sample=True,
28
- temperature=0.7,
29
- top_p=0.9,
30
- )
31
- return outputs[0]["generated_text"]
 
 
 
 
 
 
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="Llama-3.2-1B-Instruct (CPU)"
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 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__":