phamhoangf commited on
Commit
8a8eb9f
·
verified ·
1 Parent(s): 14b1cea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -7
app.py CHANGED
@@ -1,10 +1,44 @@
1
  import gradio as gr
 
 
2
 
3
- with gr.Blocks(fill_height=True) as demo:
4
- with gr.Sidebar():
5
- gr.Markdown("# Inference Provider")
6
- gr.Markdown("This Space showcases the Qwen/Qwen3-4B-Instruct-2507 model, served by the nscale API. Sign in with your Hugging Face account to use this API.")
7
- button = gr.LoginButton("Sign in")
8
- gr.load("models/Qwen/Qwen3-4B-Instruct-2507", accept_token=button, provider="nscale")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import pipeline, AutoTokenizer
4
 
5
+ # Tải 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()