GiantPandas commited on
Commit
8d1188d
·
verified ·
1 Parent(s): b9d791f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -109
app.py CHANGED
@@ -6,7 +6,13 @@ import sys
6
  import json
7
  import base64
8
  import tqdm
 
 
9
 
 
 
 
 
10
 
11
  from openai import OpenAI
12
  # 设置 OpenAI 的 API 密钥和 API 基础 URL 以使用 vLLM 的 API 服务器。
@@ -20,118 +26,60 @@ client = OpenAI(
20
  )
21
 
22
 
23
- def convert_sigle_message_to_gpt(dialogs):
24
- if not dialogs:
25
- return []
26
-
27
- merged = []
28
- current_role = None
29
- current_contents = []
30
-
31
- for item in dialogs:
32
- role = item["role"]
33
- content = item["content"]
34
-
35
- # 如果 role 变化了,说明要开启一个新的合并段
36
- if role != current_role:
37
- # 如果之前有积累的内容,需要写入 merged
38
- if current_role is not None:
39
- merged.append({
40
- "role": current_role,
41
- "content": current_contents
42
- })
43
- # 重置当前合并信息
44
- current_role = role
45
- current_contents = []
46
- if isinstance(content, tuple):
47
- for path in content:
48
- current_contents.append({
49
- "type": "image_url",
50
- "image_url": {"url": f"data:image/jpeg;base64,{encode_image(path)}"}})
51
- else:
52
- current_contents.append({"type": "text", "text": content})
53
- else:
54
- # 如果 role 相同,则把 content 加到当前内容列表里
55
- if isinstance(content, tuple):
56
- for path in content:
57
- current_contents.append({
58
- "type": "image_url",
59
- "image_url": {"url": f"data:image/jpeg;base64,{encode_image(path)}"}})
60
- else:
61
- current_contents.append({"type": "text", "text": content})
62
-
63
- # 循环结束后,把最后的合并段写入 merged
64
- if current_role is not None:
65
- merged.append({
66
- "role": current_role,
67
- "content": current_contents
68
- })
69
-
70
- return merged
71
-
72
- def clear_fn():
73
- return []
74
-
75
- def store_values(num_val, name_val):
76
- return num_val, name_val # 存入状态
77
-
78
- def use_values(num_state, name_state):
79
- return f"使用了数量: {num_state},文件名: {name_state}"
80
 
81
- def main():
82
-
83
-
84
-
85
- def bot(history: list):
86
- history = convert_sigle_message_to_gpt(history)
87
- response = openai.chat.completions.create(
88
- model=self.model_name,
89
- messages=history,
90
- extra_body={},
91
- extra_headers={
92
- "apikey": "empty"
93
- },
94
- stream=True,
95
- temperature=0.7,
96
- top_p=1.0,
97
- )
98
-
99
- history.append({"role": "assistant", "content": ""})
100
- for character in response:
101
- if not character:
102
- continue
103
- history[-1]["content"] += character
104
- yield history
105
-
106
- # 页面布局
107
 
108
- with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  with gr.Row():
110
- with gr.Column(scale=4):
111
- chatbot = gr.Chatbot(elem_id="chatbot", bubble_full_width=True, type="messages")
112
- chat_input = gr.MultimodalTextbox(
113
- interactive=True,
114
- file_count="multiple",
115
- placeholder="Enter message or upload file...",
116
- show_label=False,
117
- sources=["microphone", "upload"],
118
- elem_id="chat_page"
119
- )
120
- clear_button = gr.Button('Clear')
121
- chat_msg = chat_input.submit(
122
- add_message, [chatbot, chat_input], [chatbot, chat_input]
123
- )
124
-
125
- bot_msg = chat_msg.then(bot, chatbot, chatbot, api_name="bot_response")
126
- bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
127
-
128
- chatbot.like(print_like_dislike, None, None, like_user_message=True)
129
-
130
- clear_button.click(fn=clear_fn, inputs=[], outputs=chatbot)
131
 
132
- demo.launch(share=True)
 
 
 
133
 
134
-
135
- if __name__ == "__main__":
136
- main()
137
 
 
6
  import json
7
  import base64
8
  import tqdm
9
+ import base64
10
+
11
 
12
+ # Function to encode the image
13
+ def encode_image(image_path):
14
+ with open(image_path, "rb") as image_file:
15
+ return base64.b64encode(image_file.read()).decode("utf-8")
16
 
17
  from openai import OpenAI
18
  # 设置 OpenAI 的 API 密钥和 API 基础 URL 以使用 vLLM 的 API 服务器。
 
26
  )
27
 
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
+ def run_example(image, text_input=None, model_id=None):
31
+ messages=[
32
+ {"role": "user",
33
+ "content":
34
+ [
35
+ "type": "image_url","image_url": {"url": f"data:image/jpeg;base64,{encode_image(image)}"},
36
+ {"type": "text", "text": text_input} if text_input is not Noen,
37
+
38
+ ]
39
+ }
40
+ ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
+ response = openai.chat.completions.create(
43
+ model=self.model_name,
44
+ messages=messages,
45
+ extra_body=self.extra,
46
+ extra_headers={
47
+ "apikey": self.apikey
48
+ },
49
+ stream=self.stream,
50
+ temperature=0.7,
51
+ top_p=1.0,
52
+ )
53
+ for chunk in response:
54
+ yield chunk.choices[0].delta.content
55
+
56
+
57
+
58
+ css = """
59
+ #output {
60
+ height: 500px;
61
+ overflow: auto;
62
+ border: 1px solid #ccc;
63
+ }
64
+ """
65
+
66
+ with gr.Blocks(css=css) as demo:
67
+ gr.Markdown(DESCRIPTION)
68
+ with gr.Tab(label="MiMo-VL-7B Input"):
69
  with gr.Row():
70
+ with gr.Column():
71
+ input_img = gr.Image(label="Input Picture")
72
+ model_selector = gr.Dropdown(choices=list(models.keys()),
73
+ label="Model",
74
+ value="XiaomiMiMo/MiMo-VL-7B-SFT")
75
+ text_input = gr.Textbox(label="Text Prompt")
76
+ submit_btn = gr.Button(value="Submit")
77
+ with gr.Column():
78
+ output_text = gr.Textbox(label="Output Text")
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
+ submit_btn.click(run_example, [input_img, text_input, model_selector], [output_text])
81
+
82
+ demo.queue(api_open=False)
83
+ demo.launch(debug=True)
84
 
 
 
 
85