Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline
|
| 3 |
import torch
|
| 4 |
-
from functools import lru_cache
|
| 5 |
|
| 6 |
MODEL_NAME = "openbmb/MiniCPM-V-4"
|
| 7 |
|
| 8 |
@lru_cache(maxsize=1)
|
| 9 |
def load_pipeline():
|
| 10 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
|
|
|
|
|
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
MODEL_NAME,
|
| 13 |
torch_dtype=torch.float16,
|
|
@@ -20,32 +23,47 @@ def load_pipeline():
|
|
| 20 |
device=model.device.index if torch.cuda.is_available() else -1
|
| 21 |
)
|
| 22 |
|
| 23 |
-
def
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
prompt,
|
| 27 |
-
max_length=
|
| 28 |
do_sample=True,
|
| 29 |
-
top_k=
|
| 30 |
-
top_p=
|
| 31 |
num_return_sequences=1
|
| 32 |
-
)
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
with gr.Blocks() as demo:
|
| 36 |
-
gr.Markdown("
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
fn=
|
| 47 |
-
inputs=[
|
| 48 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
)
|
| 50 |
|
| 51 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from functools import lru_cache
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline
|
| 5 |
import torch
|
|
|
|
| 6 |
|
| 7 |
MODEL_NAME = "openbmb/MiniCPM-V-4"
|
| 8 |
|
| 9 |
@lru_cache(maxsize=1)
|
| 10 |
def load_pipeline():
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 12 |
+
MODEL_NAME, trust_remote_code=True
|
| 13 |
+
)
|
| 14 |
model = AutoModelForCausalLM.from_pretrained(
|
| 15 |
MODEL_NAME,
|
| 16 |
torch_dtype=torch.float16,
|
|
|
|
| 23 |
device=model.device.index if torch.cuda.is_available() else -1
|
| 24 |
)
|
| 25 |
|
| 26 |
+
def respond(user_message, chat_history):
|
| 27 |
+
# ์ด์ ๋ํ์ ์ ๋ฉ์์ง๋ฅผ ํ๋์ prompt๋ก ํฉ์นจ
|
| 28 |
+
history_text = ""
|
| 29 |
+
for u, a in chat_history:
|
| 30 |
+
history_text += f"็จๆท๏ผ{u}\nๅฉ็๏ผ{a}\n"
|
| 31 |
+
prompt = history_text + f"็จๆท๏ผ{user_message}\nๅฉ็๏ผ"
|
| 32 |
+
|
| 33 |
+
pipeline = load_pipeline()
|
| 34 |
+
output = pipeline(
|
| 35 |
prompt,
|
| 36 |
+
max_length=history_text.count("\n") + len(user_message.split()) + 50,
|
| 37 |
do_sample=True,
|
| 38 |
+
top_k=50,
|
| 39 |
+
top_p=0.95,
|
| 40 |
num_return_sequences=1
|
| 41 |
+
)[0]["generated_text"]
|
| 42 |
+
|
| 43 |
+
# ๋ชจ๋ธ์ด ๋ฐํํ ์ ์ฒด ํ
์คํธ์์ ์ด์์คํดํธ ์๋ต ๋ถ๋ถ๋ง ์ถ์ถ
|
| 44 |
+
assistant_reply = output.split("ๅฉ็๏ผ", 1)[-1].strip()
|
| 45 |
+
chat_history.append((user_message, assistant_reply))
|
| 46 |
+
return chat_history
|
| 47 |
|
| 48 |
with gr.Blocks() as demo:
|
| 49 |
+
gr.Markdown("## MiniCPM-V-4 Chatbot Demo")
|
| 50 |
+
|
| 51 |
+
chatbot = gr.Chatbot(label="๋ํ")
|
| 52 |
+
user_input = gr.Textbox(
|
| 53 |
+
placeholder="๋ฉ์์ง๋ฅผ ์
๋ ฅํ์ธ์...",
|
| 54 |
+
lines=1
|
| 55 |
+
)
|
| 56 |
+
clear_btn = gr.Button("์ด๊ธฐํ")
|
| 57 |
+
|
| 58 |
+
user_input.submit(
|
| 59 |
+
fn=respond,
|
| 60 |
+
inputs=[user_input, chatbot],
|
| 61 |
+
outputs=chatbot
|
| 62 |
+
)
|
| 63 |
+
clear_btn.click(
|
| 64 |
+
lambda: [],
|
| 65 |
+
None,
|
| 66 |
+
chatbot
|
| 67 |
)
|
| 68 |
|
| 69 |
demo.launch()
|