Spaces:
Running
Running
Commit
·
28668a9
1
Parent(s):
c365981
openAI
Browse files- app.py +18 -48
- phoGPT.py +19 -3
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,64 +1,34 @@
|
|
1 |
# app.py
|
2 |
import gradio as gr
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
config = AutoConfig.from_pretrained(model_path, trust_remote_code=True)
|
11 |
-
config.init_device = "cpu"
|
12 |
-
|
13 |
-
model = AutoModelForCausalLM.from_pretrained("vinai/PhoGPT-4B-Chat", trust_remote_code=True)
|
14 |
-
model.eval()
|
15 |
-
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
|
16 |
-
|
17 |
-
|
18 |
-
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
|
19 |
-
# 2.1 — Gom system message và history vào messages list
|
20 |
messages = [{"role": "system", "content": system_message}]
|
21 |
for u, b in history:
|
22 |
-
|
23 |
-
|
24 |
-
if b:
|
25 |
-
messages.append({"role": "assistant", "content": b})
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
-
# 2.2 —
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
)
|
34 |
-
|
35 |
-
# 2.3 — Tokenize và đưa lên device
|
36 |
-
# inputs = tokenizer(input_prompt, return_tensors="pt")
|
37 |
-
input_ids = tokenizer(input_prompt, return_tensors="pt")
|
38 |
-
# inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
39 |
-
|
40 |
-
# 2.4 — Sinh text
|
41 |
-
outputs = model.generate(
|
42 |
-
inputs=input_ids["input_ids"],
|
43 |
-
max_new_tokens=max_tokens,
|
44 |
temperature=temperature,
|
45 |
top_p=top_p,
|
46 |
-
do_sample=True,
|
47 |
-
eos_token_id=tokenizer.eos_token_id,
|
48 |
-
pad_token_id=tokenizer.pad_token_id,
|
49 |
)
|
50 |
-
# print('!!!! OUTPUTS 1: ',outputs)
|
51 |
-
# 2.5 — Decode và tách phần assistant trả lời
|
52 |
-
response = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
53 |
-
print('!! OUTPUTS 2: ',response)
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
return response
|
58 |
|
59 |
-
# 2.
|
60 |
-
|
61 |
-
|
62 |
|
63 |
# 3️⃣ Giao diện Gradio
|
64 |
demo = gr.ChatInterface(
|
|
|
1 |
# app.py
|
2 |
import gradio as gr
|
3 |
+
import openai
|
4 |
+
import os
|
5 |
|
6 |
+
# 1️⃣ Thiết lập OpenAI API key (đặt biến môi trường OPENAI_API_KEY trước khi chạy)
|
7 |
+
openai.api_key = os.getenv("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")
|
8 |
|
9 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
10 |
+
# 2.1 — Gom system + lịch sử chat vào messages list
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
messages = [{"role": "system", "content": system_message}]
|
12 |
for u, b in history:
|
13 |
+
messages.append({"role": "user", "content": u})
|
14 |
+
messages.append({"role": "assistant", "content": b})
|
|
|
|
|
15 |
messages.append({"role": "user", "content": message})
|
16 |
|
17 |
+
# 2.2 — Gọi OpenAI ChatCompletion
|
18 |
+
resp = openai.ChatCompletion.create(
|
19 |
+
model="gpt-4.1-nano",
|
20 |
+
messages=messages,
|
21 |
+
max_tokens=max_tokens,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
temperature=temperature,
|
23 |
top_p=top_p,
|
|
|
|
|
|
|
24 |
)
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
# 2.3 — Trích nội dung assistant reply
|
27 |
+
reply = resp.choices[0].message.content.strip()
|
|
|
28 |
|
29 |
+
# 2.4 — Cập nhật history và trả về
|
30 |
+
history.append((message, reply))
|
31 |
+
return history
|
32 |
|
33 |
# 3️⃣ Giao diện Gradio
|
34 |
demo = gr.ChatInterface(
|
phoGPT.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
-
|
2 |
-
|
|
|
3 |
import torch
|
|
|
4 |
|
5 |
# 1️⃣ Cấu hình và load model + tokenizer
|
6 |
model_path = "vinai/PhoGPT-4B-Chat"
|
@@ -56,4 +58,18 @@ def respond(message, history: list[tuple[str, str]], system_message, max_tokens,
|
|
56 |
|
57 |
# 2.6 — Cập nhật history và trả về
|
58 |
# history.append((message, response))
|
59 |
-
# return history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
import torch
|
5 |
+
from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
|
6 |
|
7 |
# 1️⃣ Cấu hình và load model + tokenizer
|
8 |
model_path = "vinai/PhoGPT-4B-Chat"
|
|
|
58 |
|
59 |
# 2.6 — Cập nhật history và trả về
|
60 |
# history.append((message, response))
|
61 |
+
# return history
|
62 |
+
|
63 |
+
# 3️⃣ Giao diện Gradio
|
64 |
+
demo = gr.ChatInterface(
|
65 |
+
respond, #câu phản hồi
|
66 |
+
additional_inputs=[
|
67 |
+
gr.Textbox("Bạn là một chatbot tiếng Việt thân thiện.", label="System message"),
|
68 |
+
gr.Slider(1, 2048, value=512, step=1, label="Max new tokens"),
|
69 |
+
gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="Temperature"),
|
70 |
+
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
71 |
+
],
|
72 |
+
)
|
73 |
+
|
74 |
+
if __name__ == "__main__":
|
75 |
+
demo.launch()
|
requirements.txt
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
gradio
|
2 |
torch
|
3 |
einops
|
4 |
-
transformers==4.38.1
|
|
|
|
1 |
gradio
|
2 |
torch
|
3 |
einops
|
4 |
+
transformers==4.38.1
|
5 |
+
openai
|