-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
231 lines (196 loc) · 11.2 KB
/
Copy pathtest.py
File metadata and controls
231 lines (196 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# from langchain_openai import ChatOpenAI
# from langchain_core.messages import HumanMessage
# from glob import glob
# from conf.Tools import Tools
# import os, json
# def run_langchin():
# os.environ['OPENAI_API_BASE'] = "https://fast.xeduapi.com/v1"
# # os.environ['OPENAI_API_BASE'] = "https://api.xeduapi.com"
# OPENAI_API_KEY = "sk-jz0shLgMJY9HBVnLC3Fe3dCaA5204a418e67003f637f1eFf"
# MODEL_NAME = 'gpt-4o'
# llm = ChatOpenAI(model=MODEL_NAME, api_key=OPENAI_API_KEY)
# # response = llm.invoke("What can you do?")
# human_message = HumanMessage(content=[
# { "type": "text", "text": "请将图中的流程图转换成DOT格式,使用DOT语言来描述流程图。" },
# { "type": "image_url", "image_url": { "url": "https://github.com/Ace-bb/Image_server/blob/main/flowchart/%E5%86%85%E5%88%86%E6%B3%8C%E7%A7%91/POEMS%20%E7%BB%BC%E5%90%88%E5%BE%81/1_1.png?raw=true" }}
# ])
# response = llm.invoke([ human_message ])
# print(response.content)
# def copy_images():
# from conf.Tools import Tools
# import shutil
# tools = Tools()
# datas = tools.read_json("/root/LLM-based-graph-tool/data/datasets/Flowchart2DotDatasets/dotV1/datasets/flowchart2dot_eval.json")
# for item in datas:
# copy_save_path = "/root/LLM-based-graph-tool/repo/Image_server/flowchart/" + item["image"].replace("/root/LLM-based-graph-tool/data/datasets/Flowchart2DotDatasets/dotV1/FlowchartImages/", '')
# base_dir = os.path.dirname(copy_save_path)
# if not os.path.exists(base_dir): os.makedirs(base_dir)
# shutil.copyfile(item["image"], copy_save_path)
# def run_openai():
# from openai import OpenAI
# import base64
# os.environ['OPENAI_API_BASE'] = "https://fast.xeduapi.com/v1"
# # os.environ['OPENAI_API_BASE'] = "https://api.xeduapi.com"
# OPENAI_API_KEY = "sk-jz0shLgMJY9HBVnLC3Fe3dCaA5204a418e67003f637f1eFf"
# with open("/root/LLM-based-graph-tool/data/datasets/Flowchart2DotDatasets/dotV1/FlowchartImages/妇产科/产后关节响/0_1.png", 'rb') as f:
# base64_img = base64.b64encode(f.read()).decode('utf-8')
# client = OpenAI(
# # This is the default and can be omitted
# api_key= OPENAI_API_KEY,
# base_url="https://fast.xeduapi.com/v1"
# )
# img_type = 'image/png'
# img_url = "https://github.com/Ace-bb/Image_server/blob/main/flowchart/%E5%86%85%E5%88%86%E6%B3%8C%E7%A7%91/POEMS%20%E7%BB%BC%E5%90%88%E5%BE%81/1_1.png?raw=true"
# response = client.chat.completions.create(
# model="gpt-4o",
# messages=[
# {
# "role": "user",
# "content": [
# {"type": "text", "text": "请将图中的流程图转换成DOT格式,使用DOT语言来描述流程图。"},
# {
# "type": "image_url",
# "image_url": {"url": f"data:{img_type};base64,{base64_img}"}, # {"url": f"{img_url}"},
# },
# ],
# }
# ]
# )
# print(response.choices[0].message.content)
# def test():
# import re
# s = '''啊沙发沙发大事发生
# ```dot
# digraph G {
# node [shape=box];
# 节点1[label="产后关节有 \n 何痛怎么办?"];
# 节点2[label="调整站姿,\n保护关节"];
# 节点3[shape=oval,label="停止哺乳并\n 休息"];
# 节点1 -> 节点2;
# 节点1 -> 节点3;
# }
# ```阿迪斯发啊实打实打算
# '''
# dot_contents = re.findall(r'```dot(.*?)```', s, re.DOTALL)
# print(dot_contents[0])
# def mermaid():
# # Creating a simple flowchart diagram
# from python_mermaid.diagram import (
# MermaidDiagram,
# Node,
# Link
# )
# # Family members
# meg = Node("Meg")
# jo = Node("Jo")
# beth = Node("Beth")
# amy = Node("Amy")
# robert = Node("Robert March")
# the_march_family = [meg, jo, beth, amy, robert]
# # Create links
# family_links = [
# Link(robert, meg),
# Link(robert, jo),
# Link(robert, beth),
# Link(robert, amy),
# ]
# chart = MermaidDiagram(
# title="Little Women",
# nodes=the_march_family,
# links=family_links
# )
# print(chart)
# def log():
# from rich.console import Console
# # 设置Console将日志输出到文件中保存,同时在控制台输出
# console = Console(record=True)
# console.print("Hello, World!")
# def save_jsonl():
# import json
# with open("./data/datasets/Flowchart2DotDatasets/dotV2/datasets/flowchart2dot_train.json", "r", encoding="utf-8") as f:
# base_graph_data = json.load(f)
# with open("./data/datasets/Flowchart2DotDatasets/dotV2/datasets/flowchart2dot_train.jsonl", "a", encoding="utf-8") as f:
# for item in base_graph_data:
# f.write(json.dumps(item)+"\n")
# def merge():
# from conf.Tools import Tools
# tools = Tools()
# llm_output = tools.read_json("output/flowchart/graphviz/gpt-4o.json")
# ground_data = tools.read_json("data/FlowchartDatasets/FlowchartDatasets/test.json")
# for key, response in llm_output.items():
# # Extract text representation (mermaid, graphviz, or plantuml) from response
# ground_data[key]["llm"] = response
# tools.write_2_json(ground_data, "output/flowchart/graphviz/gpt-4o.json")
# def tansform_dot2json():
# from Evaluation.flowchart_eval import transform_dot_2_json
# from conf.Tools import Tools
# tools = Tools()
# res = transform_dot_2_json("digraph G {\n rankdir=TB;\n node [shape=box style=rounded];\n\n A [label=\"Upper extremity pain\" shape=box style=\"rounded,filled\" fillcolor=lightblue];\n B [label=\"Where is the point of maximal pain?\" shape=box style=\"rounded,filled\" fillcolor=gold];\n C [label=\"Forearm or elbow\"];\n D [label=\"Wrist\"];\n E [label=\"Hand\"];\n F [label=\"Is the pain over the epicondyles?\"];\n G [label=\"Lateral or medial epicondylitis\" shape=box style=\"rounded,filled\" fillcolor=plum];\n H [label=\"Is there erythema and pain over the olecranon process?\"];\n I [label=\"Olecranon bursitis\" shape=box style=\"rounded,filled\" fillcolor=plum];\n J [label=\"Is the pain like 'hitting your funny bone'?\"];\n K [label=\"Cubital tunnel syndrome\" shape=box style=\"rounded,filled\" fillcolor=plum];\n L [label=\"Consider alternative diagnoses such as RSD/CRPS or referred pain\" shape=box style=\"rounded,filled\" fillcolor=orange];\n M [label=\"Is there pain and loss of function of the first 2 or 3 digits of the hand?\"];\n N [label=\"Carpal tunnel syndrome\" shape=box style=\"rounded,filled\" fillcolor=plum];\n O [label=\"Did the pain start on the thumb side of the forearm and then spread?\"];\n P [label=\"de Quervain tenosynovitis\" shape=box style=\"rounded,filled\" fillcolor=plum];\n Q [label=\"Consider alternative diagnoses such as intersection syndrome\" shape=box style=\"rounded,filled\" fillcolor=orange];\n R [label=\"Do the fingers change color with exposure to cold?\"];\n S [label=\"Raynaud's disorder\" shape=box style=\"rounded,filled\" fillcolor=plum];\n T [label=\"Is there clicking and pain when the digit is bent or straightened?\"];\n U [label=\"Trigger finger\" shape=box style=\"rounded,filled\" fillcolor=plum];\n V [label=\"Is the pain associated with repetitive motion of the arms held overhead?\"];\n W [label=\"Thoracic outlet syndrome\" shape=box style=\"rounded,filled\" fillcolor=orange];\n X [label=\"Consider osteoarthritis versus rheumatoid arthritis\" shape=box style=\"rounded,filled\" fillcolor=orange];\n\n A -> B;\n B -> C;\n B -> D;\n B -> E;\n C -> F;\n F -> G [label=\"Yes\"];\n F -> H [label=\"No\"];\n H -> I [label=\"Yes\"];\n H -> J [label=\"No\"];\n J -> K [label=\"Yes\"];\n J -> L [label=\"No\"];\n D -> M;\n M -> N [label=\"Yes\"];\n M -> O [label=\"No\"];\n O -> P [label=\"Yes\"];\n O -> Q [label=\"No\"];\n E -> R;\n R -> S [label=\"Yes\"];\n R -> T [label=\"No\"];\n T -> U [label=\"Yes\"];\n T -> V [label=\"No\"];\n V -> W [label=\"Yes\"];\n V -> X [label=\"No\"];\n}")
# tools.write_2_json(res, "output/test/graphviz.json")
# def trans_flowchart2dot():
# import glob
# from flowchart_construct.utils import json_2_dot
# from conf.Tools import Tools
# from tqdm import tqdm
# tools = Tools()
# dir_path = "data/FlowchartDatasets/FlowchartDatasets/Json"
# flowchart_files = glob.glob(f"{dir_path}/**/*.json", recursive=True)
# for flow_file in tqdm(flowchart_files):
# flowchart_json = tools.read_json(flow_file)
# data = {"nodes":[], "edges":[]}
# for node in flowchart_json["nodes"]:
# data["nodes"].append({
# "id": str(node["id"]),
# "label": node["Name"],
# "attributes": {
# "label": node["Name"].replace('\n', '')
# }
# })
# for edge in flowchart_json["edges"]:
# data["edges"].append({
# "source": edge["sourceNode"],
# "target": edge["targetNode"],
# "attributes": {
# "label": edge["label"]
# }
# })
# flowchart_dot = json_2_dot(None, data)
# tools.write_2_txt(flowchart_dot, flow_file.replace("Json", "Dot").replace(".json", ".dot"))
# def write2jsonl():
# tools = Tools()
# data = tools.read_json("data/FlowchartDatasets/TrainDatasets/Image2DotV1/datasets/flowchart2dot_train.json")
# # 写jsonl文件
# with open("data/FlowchartDatasets/TrainDatasets/Image2DotV1/datasets/flowchart2dot_train.jsonl", "w", encoding="utf-8") as f:
# for item in data:
# item['image'] = item['image'].replace("data/FlowchartDatasets/TrainDatasets/Image2DotV1/Images//", "")
# f.write(json.dumps(item, ensure_ascii=False)+"\n")
# def test_lmdeploy():
# from lmdeploy_test import pipeline, TurbomindEngineConfig
# from lmdeploy.vl import load_image
# model = 'work_dirs/internvl_chat_v2_5/internvl2_5_8b_dynamic_res_2nd_finetune_lora_unfreeze_llm_backbone_mlp_v2'
# image = load_image('https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg')
# pipe = pipeline(model, backend_config=TurbomindEngineConfig(session_len=8192))
# response = pipe(('describe this image', image))
# print(response.text)
# def count():
# files = os.listdir("data/datasets/DXDiseases")
# tools = Tools()
# all_num = 0
# for f in files:
# data = tools.read_json(f"data/datasets/DXDiseases/{f}")
# print(f, len(data))
# all_num += len(data)
# print(all_num)
if __name__=="__main__":
# tansform_dot2json()
import torch
# 检查PyTorch版本
print(torch.__version__)
# 检查CUDA是否可用
print(torch.cuda.is_available())
# 查看可用的CUDA设备数量
print(torch.cuda.device_count())
# 查看PyTorch对应的CUDA版本
print(torch.version.cuda)
# files = glob("data/datasets/Flowchart2DotDatasets/dotV2/Dot/**/*.dot", recursive=True)
# print(len(files))