phamhoangf commited on
Commit
4d86614
·
verified ·
1 Parent(s): 67ccf05

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -63
app.py CHANGED
@@ -1,67 +1,44 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
-
5
- def respond(
6
- message,
7
- history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
10
- temperature,
11
- top_p,
12
- hf_token: gr.OAuthToken,
13
- ):
14
- """
15
- Để biết thêm thông tin về hỗ trợ API Suy luận của `huggingface_hub`, vui lòng kiểm tra tài liệu: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
16
- """
17
- # Đã thay đổi mô hình thành Qwen/Qwen3-4B-Thinking-2507
18
- client = InferenceClient(token=hf_token.token, model="Qwen/Qwen3-4B-Base")
19
-
20
- messages = [{"role": "system", "content": system_message}]
21
- messages.extend(history)
22
- messages.append({"role": "user", "content": message})
23
-
24
- response = ""
25
- for message in client.chat_completion(
26
- messages,
27
- max_tokens=max_tokens,
28
- stream=True,
29
- temperature=temperature,
30
- top_p=top_p,
31
- ):
32
- choices = message.choices
33
- token = ""
34
- if len(choices) and choices[0].delta.content:
35
- token = choices[0].delta.content
36
- response += token
37
- yield response
38
-
39
-
40
- """
41
- Để biết thông tin về cách tùy chỉnh ChatInterface, hãy xem tài liệu của gradio: https://www.gradio.app/docs/chatinterface
42
- """
43
- chatbot = gr.ChatInterface(
44
- respond,
45
- type="messages",
46
- additional_inputs=[
47
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
48
- gr.Slider(minimum=1, maximum=1024, value=512, step=1, label="Max new tokens"),
49
- gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature"),
50
- gr.Slider(
51
- minimum=0.1,
52
- maximum=1.0,
53
- value=0.95,
54
- step=0.05,
55
- label="Top-p (nucleus sampling)",
56
- ),
57
- ],
58
  )
59
 
60
- with gr.Blocks() as demo:
61
- with gr.Sidebar():
62
- gr.LoginButton()
63
- chatbot.render()
64
-
 
 
65
 
66
- if __name__ == "__main__":
67
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import pipeline, AutoTokenizer
4
+
5
+ # Tải mô hình và tokenizer
6
+ # device_map="auto" sẽ tự động sử dụng GPU nếu có
7
+ model_id = "phamhoangf/struct-aware-baseline-qwen3-4b"
8
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
9
+ pipe = pipeline(
10
+ "text-generation",
11
+ model=model_id,
12
+ torch_dtype=torch.bfloat16,
13
+ device_map="auto",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  )
15
 
16
+ def predict(message, history):
17
+ # Xây dựng prompt từ lịch sử trò chuyện theo template của Qwen2
18
+ messages = []
19
+ for user_msg, assistant_msg in history:
20
+ messages.append({"role": "user", "content": user_msg})
21
+ messages.append({"role": "assistant", "content": assistant_msg})
22
+ messages.append({"role": "user", "content": message})
23
 
24
+ # Tạo prompt hoàn chỉnh
25
+ prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
26
+
27
+ # Tạo văn bản
28
+ outputs = pipe(
29
+ prompt,
30
+ max_new_tokens=256,
31
+ do_sample=True,
32
+ temperature=0.7,
33
+ top_k=50,
34
+ top_p=0.95,
35
+ )
36
+
37
+ # Trích xuất phần trả lời
38
+ generated_text = outputs[0]["generated_text"]
39
+ # Lấy phần văn bản mới được tạo ra (sau prompt)
40
+ response = generated_text[len(prompt):]
41
+ return response
42
+
43
+ # Tạo giao diện Chat, giao diện này cũng tự động tạo ra một API
44
+ gr.ChatInterface(predict).launch()