Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,17 +2,11 @@ import spaces
|
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import torch
|
4 |
import gradio as gr
|
5 |
-
import os
|
6 |
|
7 |
-
|
8 |
-
USE_CUDA = torch.cuda.is_available()
|
9 |
-
device_ids_parallel = [0]
|
10 |
-
device = torch.device("cuda:{}".format(device_ids_parallel[0]) if USE_CUDA else "cpu")
|
11 |
-
|
12 |
-
# 初始化
|
13 |
peft_model_id = "CMLM/ZhongJing-2-1_8b"
|
14 |
base_model_id = "Qwen/Qwen1.5-1.8B-Chat"
|
15 |
-
model = AutoModelForCausalLM.from_pretrained(base_model_id, device_map="auto")
|
16 |
model.load_adapter(peft_model_id)
|
17 |
tokenizer = AutoTokenizer.from_pretrained(
|
18 |
"CMLM/ZhongJing-2-1_8b",
|
@@ -21,9 +15,12 @@ tokenizer = AutoTokenizer.from_pretrained(
|
|
21 |
pad_token=''
|
22 |
)
|
23 |
|
24 |
-
|
25 |
@spaces.GPU
|
26 |
def single_turn_chat(question):
|
|
|
|
|
|
|
27 |
prompt = f"Question: {question}"
|
28 |
messages = [
|
29 |
{"role": "system", "content": "You are a helpful TCM medical assistant named 仲景中医大语言模型, created by 医哲未来 of Fudan University."},
|
@@ -36,9 +33,12 @@ def single_turn_chat(question):
|
|
36 |
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
37 |
return response
|
38 |
|
39 |
-
|
40 |
@spaces.GPU
|
41 |
def multi_turn_chat(question, chat_history=None):
|
|
|
|
|
|
|
42 |
if not isinstance(question, str):
|
43 |
raise ValueError("The question must be a string.")
|
44 |
|
@@ -76,7 +76,7 @@ def multi_turn_chat(question, chat_history=None):
|
|
76 |
def clear_history():
|
77 |
return [], []
|
78 |
|
79 |
-
#
|
80 |
single_turn_interface = gr.Interface(
|
81 |
fn=single_turn_chat,
|
82 |
inputs=["text"],
|
@@ -85,7 +85,7 @@ single_turn_interface = gr.Interface(
|
|
85 |
description="博极医源,精勤不倦。Unlocking the Wisdom of Traditional Chinese Medicine with AI."
|
86 |
)
|
87 |
|
88 |
-
#
|
89 |
with gr.Blocks() as multi_turn_interface:
|
90 |
chatbot = gr.Chatbot(label="仲景GPT-V2-1.8B 多轮对话")
|
91 |
state = gr.State([])
|
@@ -100,3 +100,4 @@ with gr.Blocks() as multi_turn_interface:
|
|
100 |
|
101 |
single_turn_interface.launch()
|
102 |
multi_turn_interface.launch()
|
|
|
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import torch
|
4 |
import gradio as gr
|
|
|
5 |
|
6 |
+
# Initialize
|
|
|
|
|
|
|
|
|
|
|
7 |
peft_model_id = "CMLM/ZhongJing-2-1_8b"
|
8 |
base_model_id = "Qwen/Qwen1.5-1.8B-Chat"
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(base_model_id, device_map="auto")
|
10 |
model.load_adapter(peft_model_id)
|
11 |
tokenizer = AutoTokenizer.from_pretrained(
|
12 |
"CMLM/ZhongJing-2-1_8b",
|
|
|
15 |
pad_token=''
|
16 |
)
|
17 |
|
18 |
+
# Single turn chat
|
19 |
@spaces.GPU
|
20 |
def single_turn_chat(question):
|
21 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
22 |
+
model.to(device)
|
23 |
+
|
24 |
prompt = f"Question: {question}"
|
25 |
messages = [
|
26 |
{"role": "system", "content": "You are a helpful TCM medical assistant named 仲景中医大语言模型, created by 医哲未来 of Fudan University."},
|
|
|
33 |
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
34 |
return response
|
35 |
|
36 |
+
# Multi-turn chat
|
37 |
@spaces.GPU
|
38 |
def multi_turn_chat(question, chat_history=None):
|
39 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
40 |
+
model.to(device)
|
41 |
+
|
42 |
if not isinstance(question, str):
|
43 |
raise ValueError("The question must be a string.")
|
44 |
|
|
|
76 |
def clear_history():
|
77 |
return [], []
|
78 |
|
79 |
+
# Single turn interface
|
80 |
single_turn_interface = gr.Interface(
|
81 |
fn=single_turn_chat,
|
82 |
inputs=["text"],
|
|
|
85 |
description="博极医源,精勤不倦。Unlocking the Wisdom of Traditional Chinese Medicine with AI."
|
86 |
)
|
87 |
|
88 |
+
# Multi-turn interface
|
89 |
with gr.Blocks() as multi_turn_interface:
|
90 |
chatbot = gr.Chatbot(label="仲景GPT-V2-1.8B 多轮对话")
|
91 |
state = gr.State([])
|
|
|
100 |
|
101 |
single_turn_interface.launch()
|
102 |
multi_turn_interface.launch()
|
103 |
+
|