fix error
Browse files
app.py
CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import base64
|
4 |
|
5 |
-
# 更新为 MiniCPM-Llama3-V-2_5 模型
|
6 |
client = InferenceClient("openbmb/MiniCPM-Llama3-V-2_5")
|
7 |
|
8 |
def encode_image(image_path):
|
@@ -12,22 +11,19 @@ def encode_image(image_path):
|
|
12 |
def respond(
|
13 |
message,
|
14 |
image,
|
15 |
-
history
|
16 |
system_message,
|
17 |
max_tokens,
|
18 |
temperature,
|
19 |
top_p,
|
20 |
):
|
21 |
messages = [{"role": "system", "content": system_message}]
|
22 |
-
for
|
23 |
-
|
24 |
-
|
25 |
-
if val[1]:
|
26 |
-
messages.append({"role": "assistant", "content": val[1]})
|
27 |
|
28 |
-
# 处理图片输入
|
29 |
if image:
|
30 |
-
base64_image = encode_image(image
|
31 |
image_message = f"<image>{base64_image}</image>"
|
32 |
message = image_message + "\n" + message
|
33 |
|
@@ -43,7 +39,7 @@ def respond(
|
|
43 |
):
|
44 |
token = message.token.text
|
45 |
response += token
|
46 |
-
yield response
|
47 |
|
48 |
demo = gr.Interface(
|
49 |
respond,
|
@@ -56,9 +52,13 @@ demo = gr.Interface(
|
|
56 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
57 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
58 |
],
|
59 |
-
outputs=
|
|
|
|
|
|
|
60 |
title="MiniCPM-Llama3-V-2_5 Image and Text Chat",
|
61 |
-
description="Upload an image and ask questions about it, or just chat without an image."
|
|
|
62 |
)
|
63 |
|
64 |
if __name__ == "__main__":
|
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import base64
|
4 |
|
|
|
5 |
client = InferenceClient("openbmb/MiniCPM-Llama3-V-2_5")
|
6 |
|
7 |
def encode_image(image_path):
|
|
|
11 |
def respond(
|
12 |
message,
|
13 |
image,
|
14 |
+
history,
|
15 |
system_message,
|
16 |
max_tokens,
|
17 |
temperature,
|
18 |
top_p,
|
19 |
):
|
20 |
messages = [{"role": "system", "content": system_message}]
|
21 |
+
for user_msg, bot_msg in history:
|
22 |
+
messages.append({"role": "user", "content": user_msg})
|
23 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
|
|
|
|
24 |
|
|
|
25 |
if image:
|
26 |
+
base64_image = encode_image(image)
|
27 |
image_message = f"<image>{base64_image}</image>"
|
28 |
message = image_message + "\n" + message
|
29 |
|
|
|
39 |
):
|
40 |
token = message.token.text
|
41 |
response += token
|
42 |
+
yield response, history + [(message, response)]
|
43 |
|
44 |
demo = gr.Interface(
|
45 |
respond,
|
|
|
52 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
53 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
54 |
],
|
55 |
+
outputs=[
|
56 |
+
gr.Textbox(label="Response"),
|
57 |
+
gr.State() # for updated history
|
58 |
+
],
|
59 |
title="MiniCPM-Llama3-V-2_5 Image and Text Chat",
|
60 |
+
description="Upload an image and ask questions about it, or just chat without an image.",
|
61 |
+
allow_flagging="never"
|
62 |
)
|
63 |
|
64 |
if __name__ == "__main__":
|