Spaces:
Sleeping
Sleeping
phucpx
commited on
Commit
·
7db6cdc
1
Parent(s):
198ca19
add passsage templates
Browse files- app.py +141 -160
- requirements.txt +0 -3
app.py
CHANGED
@@ -1,173 +1,154 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
from dashscope.api_entities.dashscope_response import Role
|
9 |
-
from typing import List, Optional, Tuple, Dict, Any, AsyncGenerator
|
10 |
-
from urllib.error import HTTPError
|
11 |
-
|
12 |
-
|
13 |
-
default_system = 'Bạn là Trợ lý gia sư AI dạy ngôn ngữ Tiếng Anh, tên là Teacher Bee AI. Bạn được xây dựng bởi Prep Education để hướng dẫn học viên làm bài tập trên nền tảng Prepedu.com.'
|
14 |
-
|
15 |
-
History = List[Tuple[str, str]]
|
16 |
-
Messages = List[Dict[str, str]]
|
17 |
-
|
18 |
-
latex_delimiters = [{
|
19 |
-
"left": "\\(",
|
20 |
-
"right": "\\)",
|
21 |
-
"display": True
|
22 |
-
}, {
|
23 |
-
"left": "\\begin\{equation\}",
|
24 |
-
"right": "\\end\{equation\}",
|
25 |
-
"display": True
|
26 |
-
}, {
|
27 |
-
"left": "\\begin\{align\}",
|
28 |
-
"right": "\\end\{align\}",
|
29 |
-
"display": True
|
30 |
-
}, {
|
31 |
-
"left": "\\begin\{alignat\}",
|
32 |
-
"right": "\\end\{alignat\}",
|
33 |
-
"display": True
|
34 |
-
}, {
|
35 |
-
"left": "\\begin\{gather\}",
|
36 |
-
"right": "\\end\{gather\}",
|
37 |
-
"display": True
|
38 |
-
}, {
|
39 |
-
"left": "\\begin\{CD\}",
|
40 |
-
"right": "\\end\{CD\}",
|
41 |
-
"display": True
|
42 |
-
}, {
|
43 |
-
"left": "\\[",
|
44 |
-
"right": "\\]",
|
45 |
-
"display": True
|
46 |
-
}]
|
47 |
-
|
48 |
-
|
49 |
-
def clear_session() -> tuple[str, list[Any]]:
|
50 |
-
return '', []
|
51 |
-
|
52 |
-
|
53 |
-
def modify_system_session(system: str) -> tuple[str, str, list[Any]]:
|
54 |
-
if system is None or len(system) == 0:
|
55 |
-
system = default_system
|
56 |
-
return system, system, []
|
57 |
-
|
58 |
-
|
59 |
-
def history_to_messages(history: History, system: str) -> Messages:
|
60 |
-
messages = [{'role': Role.SYSTEM, 'content': system}]
|
61 |
-
for h in history:
|
62 |
-
messages.append({'role': Role.USER, 'content': h[0].text})
|
63 |
-
messages.append({'role': Role.ASSISTANT, 'content': h[1].text})
|
64 |
-
return messages
|
65 |
-
|
66 |
-
|
67 |
-
def messages_to_history(messages: Messages) -> tuple[str, list[list[str]]]:
|
68 |
-
assert messages[0]['role'] == Role.SYSTEM
|
69 |
-
system = messages[0]['content']
|
70 |
-
history = []
|
71 |
-
for q, r in zip(messages[1::2], messages[2::2]):
|
72 |
-
history.append([q['content'], r['content']])
|
73 |
-
return system, history
|
74 |
-
|
75 |
-
|
76 |
-
async def model_chat(query: Optional[str], history: Optional[History], system: str, radio: str) -> AsyncGenerator[Tuple[str, str, History, str], None]:
|
77 |
-
if query is None:
|
78 |
-
query = ''
|
79 |
-
if history is None:
|
80 |
-
history = []
|
81 |
-
messages = history_to_messages(history, system)
|
82 |
-
messages.append({'role': Role.USER, 'content': query})
|
83 |
-
|
84 |
-
label_model = radio
|
85 |
-
|
86 |
-
async with aiohttp.ClientSession() as session:
|
87 |
-
async with session.post(
|
88 |
-
url="http://bore.testsprep.online:8082/v1/chat/completions",
|
89 |
-
json={
|
90 |
-
"model": label_model,
|
91 |
-
"messages": messages,
|
92 |
-
"result_format": "message",
|
93 |
-
"stream": True
|
94 |
-
}
|
95 |
-
) as response:
|
96 |
-
if response.status == HTTPStatus.OK:
|
97 |
-
async for line in response.content:
|
98 |
-
decoded_line = line.decode('utf-8')
|
99 |
-
yield '', decoded_line, history, system
|
100 |
-
else:
|
101 |
-
raise ValueError(f"Request failed with status {response.status}")
|
102 |
|
|
|
|
|
103 |
|
104 |
-
def choose_radio(radio, system):
|
105 |
-
chatbot = mgr.Chatbot(label=f'{radio.lower()}')
|
106 |
|
107 |
-
|
108 |
-
|
109 |
|
110 |
-
return chatbot, system, system, ""
|
111 |
|
|
|
|
|
112 |
|
113 |
-
def update_other_radios(value, other_radio1, other_radio2):
|
114 |
-
if value == "":
|
115 |
-
if other_radio1 != "":
|
116 |
-
selected = other_radio1
|
117 |
-
else:
|
118 |
-
selected = other_radio2
|
119 |
-
return selected, other_radio1, other_radio2
|
120 |
-
return value, "", ""
|
121 |
|
|
|
|
|
122 |
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
|
134 |
with gr.Row():
|
135 |
-
with gr.
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
clear_history.click(fn=clear_session,
|
158 |
-
inputs=[],
|
159 |
-
outputs=[textbox, chatbot])
|
160 |
-
modify_system.click(fn=modify_system_session,
|
161 |
-
inputs=[system_input],
|
162 |
-
outputs=[system_state, system_input, chatbot])
|
163 |
-
|
164 |
-
radio.change(choose_radio,
|
165 |
-
inputs=[radio, system_input],
|
166 |
-
outputs=[chatbot, system_state, system_input, textbox])
|
167 |
-
|
168 |
-
demo.queue(api_open=False, default_concurrency_limit=40)
|
169 |
-
demo.launch(max_threads=40, share=True)
|
170 |
-
|
171 |
-
|
172 |
-
if __name__ == "__main__":
|
173 |
-
main()
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
+
import sys
|
4 |
+
import json
|
5 |
+
import requests
|
6 |
+
import random
|
7 |
|
8 |
+
API_URL = "http://bore.testsprep.online:8082/v1/chat/completions"
|
9 |
+
DISABLED = os.getenv("DISABLED") == 'True'
|
10 |
+
BEARER_TOKEN = "ABC@123"
|
11 |
+
NUM_THREADS = 16
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
SYSTEM_PROMPT = """Bạn là Trợ lý gia sư AI dạy ngôn ngữ Tiếng Anh, tên là Teacher Bee AI. Bạn được xây dựng bởi Prep Education để hướng dẫn học viên làm bài tập trên nền tảng Prepedu.com.
|
14 |
+
Bạn là một trợ lý thân thiện, tính cách tốt bụng và supportive. Giả sử bạn đang hướng dẫn, giải thích và trả lời câu hỏi cho một đứa trẻ 12 tuổi hoặc ở trình độ ngôn ngữ không cao hơn trình độ của người học."""
|
15 |
|
|
|
|
|
16 |
|
17 |
+
def exception_handler(exception_type, exception, traceback):
|
18 |
+
print("%s: %s" % (exception_type.__name__, exception))
|
19 |
|
|
|
20 |
|
21 |
+
sys.excepthook = exception_handler
|
22 |
+
sys.tracebacklimit = 0
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
def predict(system_prompt, inputs, top_p, temperature, max_tokens, chat_counter, chatbot, history, request: gr.Request):
|
26 |
+
messages = [{"role": "system", "content": system_prompt}] if system_prompt else []
|
27 |
|
28 |
+
headers = {
|
29 |
+
"accept": "application/json",
|
30 |
+
"Authorization": "Bearer Prep@123",
|
31 |
+
"Content-Type": "application/json"
|
32 |
+
}
|
33 |
+
|
34 |
+
print("\n\n")
|
35 |
+
print("="*100)
|
36 |
+
print(f"chat_counter: {chat_counter}")
|
37 |
+
print(f"history: {history}")
|
38 |
+
|
39 |
+
if chat_counter != 0:
|
40 |
+
for i, data in enumerate(history):
|
41 |
+
if i % 2 == 0:
|
42 |
+
role = 'user'
|
43 |
+
else:
|
44 |
+
role = 'assistant'
|
45 |
+
|
46 |
+
messages.append({"role": role, "content": data})
|
47 |
+
|
48 |
+
messages.append({"role": "user", "content": inputs})
|
49 |
+
|
50 |
+
print(f"messages: {messages}")
|
51 |
+
|
52 |
+
payload = {
|
53 |
+
"model": "LA-SFT",
|
54 |
+
"messages": messages,
|
55 |
+
"do_sample": True,
|
56 |
+
"temperature": temperature,
|
57 |
+
"top_p": top_p,
|
58 |
+
"max_tokens": max_tokens,
|
59 |
+
"n": 1,
|
60 |
+
"stream": True,
|
61 |
+
"presence_penalty": 0,
|
62 |
+
"frequency_penalty": 0,
|
63 |
+
}
|
64 |
+
else:
|
65 |
+
messages.append({"role": "user", "content": inputs})
|
66 |
+
payload = {
|
67 |
+
"model": "LA-SFT",
|
68 |
+
"messages": messages,
|
69 |
+
"do_sample": True,
|
70 |
+
"temperature": temperature,
|
71 |
+
"top_p": top_p,
|
72 |
+
"max_tokens": max_tokens,
|
73 |
+
"n": 1,
|
74 |
+
"stream": True,
|
75 |
+
"presence_penalty": 0,
|
76 |
+
"frequency_penalty": 0,
|
77 |
+
}
|
78 |
+
|
79 |
+
chat_counter += 1
|
80 |
+
|
81 |
+
history.append(inputs)
|
82 |
+
token_counter = 0
|
83 |
+
partial_words = ""
|
84 |
+
counter = 0
|
85 |
+
|
86 |
+
try:
|
87 |
+
if payload:
|
88 |
+
print(f"\n>>> Payload: {payload}")
|
89 |
+
# Gọi API với stream=True
|
90 |
+
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
|
91 |
+
|
92 |
+
for chunk in response.iter_lines():
|
93 |
+
if counter == 0:
|
94 |
+
counter += 1
|
95 |
+
continue
|
96 |
+
|
97 |
+
if chunk.decode():
|
98 |
+
chunk = chunk.decode()
|
99 |
+
if len(chunk) > 12 and "content" in json.loads(chunk[6:])['choices'][0]['delta']:
|
100 |
+
partial_words += json.loads(chunk[6:])['choices'][0]["delta"]["content"]
|
101 |
+
if token_counter == 0:
|
102 |
+
history.append(" " + partial_words)
|
103 |
+
else:
|
104 |
+
history[-1] = partial_words
|
105 |
+
|
106 |
+
token_counter += 1
|
107 |
+
yield [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)], history, chat_counter, response, gr.update(interactive=False), gr.update(interactive=False)
|
108 |
+
|
109 |
+
except Exception as e:
|
110 |
+
print(f'error found: {e}')
|
111 |
+
|
112 |
+
yield [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)], history, chat_counter, response, gr.update(interactive=True), gr.update(interactive=True)
|
113 |
+
|
114 |
+
|
115 |
+
def reset_textbox():
|
116 |
+
return gr.update(value='', interactive=False), gr.update(interactive=False)
|
117 |
+
|
118 |
+
|
119 |
+
title = """<h1 align="center">Learning Assistant In-house Model</h1>"""
|
120 |
+
theme = gr.themes.Default(primary_hue="green")
|
121 |
+
|
122 |
+
with gr.Blocks(
|
123 |
+
css="""#col_container { margin-left: auto; margin-right: auto;} #chatbot {height: 520px; overflow: auto;}""",
|
124 |
+
theme=theme) as demo:
|
125 |
+
gr.HTML(title)
|
126 |
+
|
127 |
+
with gr.Column(elem_id="col_container", visible=True) as main_block:
|
128 |
+
chatbot = gr.Chatbot(elem_id='chatbot')
|
129 |
+
inputs = gr.Textbox(placeholder="Hi there!", label="Type an input and press Enter")
|
130 |
+
state = gr.State([])
|
131 |
|
132 |
with gr.Row():
|
133 |
+
with gr.Column(scale=7):
|
134 |
+
b1 = gr.Button(visible=True)
|
135 |
+
with gr.Column(scale=3):
|
136 |
+
server_status_code = gr.Textbox(label="Status code from OpenAI server")
|
137 |
+
|
138 |
+
system_prompt = gr.Textbox(placeholder="Enter system prompt here", label="System Prompt", value=SYSTEM_PROMPT)
|
139 |
+
|
140 |
+
with gr.Accordion("Parameters", open=False):
|
141 |
+
top_p = gr.Slider(minimum=0, maximum=1.0, value=0.9, step=0.05, interactive=True,
|
142 |
+
label="Top-p (nucleus sampling)")
|
143 |
+
temperature = gr.Slider(minimum=0, maximum=5.0, value=0.1, step=0.1, interactive=True, label="Temperature")
|
144 |
+
max_tokens = gr.Slider(minimum=0, maximum=16_000, value=4096, step=0.1, interactive=True, label="Max tokens")
|
145 |
+
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
146 |
+
|
147 |
+
inputs.submit(reset_textbox, [], [inputs, b1], queue=False)
|
148 |
+
inputs.submit(predict, [system_prompt, inputs, top_p, temperature, max_tokens, chat_counter, chatbot, state],
|
149 |
+
[chatbot, state, chat_counter, server_status_code, inputs, b1])
|
150 |
+
b1.click(reset_textbox, [], [inputs, b1], queue=False)
|
151 |
+
b1.click(predict, [system_prompt, inputs, top_p, temperature, max_tokens, chat_counter, chatbot, state],
|
152 |
+
[chatbot, state, chat_counter, server_status_code, inputs, b1])
|
153 |
+
|
154 |
+
demo.queue(max_size=10, default_concurrency_limit=NUM_THREADS, api_open=False).launch(share=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
DELETED
@@ -1,3 +0,0 @@
|
|
1 |
-
modelscope_studio
|
2 |
-
gradio
|
3 |
-
dashscope
|
|
|
|
|
|
|
|