From f1f4d84f7c06498e8bbe4b94341a3130f4966b84 Mon Sep 17 00:00:00 2001 From: wzx <17839623189@163.com> Date: Fri, 8 Nov 2024 14:43:41 +0800 Subject: [PATCH] update --- img2text.py | 62 -------------------- img2text2.py | 57 ------------------ main.py | 11 ++++ readme.md | 12 ++++ src/__init__.py | 0 src/__pycache__/__init__.cpython-36.pyc | Bin 0 -> 132 bytes src/__pycache__/img2text.cpython-36.pyc | Bin 0 -> 1867 bytes src/img2text.py | 73 ++++++++++++++++++++++++ src/img2text2.py | 61 ++++++++++++++++++++ 9 files changed, 157 insertions(+), 119 deletions(-) delete mode 100644 img2text.py delete mode 100644 img2text2.py create mode 100644 main.py create mode 100644 src/__init__.py create mode 100644 src/__pycache__/__init__.cpython-36.pyc create mode 100644 src/__pycache__/img2text.cpython-36.pyc create mode 100644 src/img2text.py create mode 100644 src/img2text2.py diff --git a/img2text.py b/img2text.py deleted file mode 100644 index c9204b9..0000000 --- a/img2text.py +++ /dev/null @@ -1,62 +0,0 @@ -# usr/bin/python3 -import requests -import json - -# 需填写appkey -appkey = "app-XXX" - -# 上传图片 -def upload_img(imgpath="test.jpg"): - - headers = { - 'Authorization': f'Bearer {appkey}', - } - - files = { - 'file': (imgpath, open(imgpath, 'rb'), 'image/jpg'), # 使用实际文件路径和类型替换 - } - - response = requests.post(url = 'http://sl.vrgon.com:6062/v1/files/upload', headers=headers, files=files, data={'user': 'robot'}) - - img_id = json.loads(response.content)["id"] - - return img_id - -# 计算 -def img2text(path="./img/test.jpg"): - img_id = upload_img(path) - payload = { - "inputs": {}, - "query": "请问这道数学题的答案是多少,请直接给出数字答案", - "response_mode": "blocking", - "conversation_id": "", - "user": "robot", - "files": [ - { - "type": "image", - "transfer_method": "local_file", - "upload_file_id": img_id - } - ] - } - - headers = { - 'Authorization': f'Bearer {appkey}', - 'Content-Type': 'application/json', - } - - response = requests.post(url = "http://sl.vrgon.com:6062/v1/chat-messages", headers=headers, data=json.dumps(payload)) - - ans = "" - if response.status_code == 200: - for line in response.iter_lines(): - # print(line.decode('unicode_escape')) - temp = json.loads(line) - ans += temp["answer"] - - return ans - - - -if __name__=="__main__": - print(img2text(path="./img/2.jpg")) \ No newline at end of file diff --git a/img2text2.py b/img2text2.py deleted file mode 100644 index d921b26..0000000 --- a/img2text2.py +++ /dev/null @@ -1,57 +0,0 @@ -# usr/bin/python2.7 -import requests -import json - -# 需填写appkey -appkey = "app-XXX" - -# 上传图片 -def upload_img(imgpath="test.jpg"): - headers = { - 'Authorization': 'Bearer {0}'.format(appkey), - } - - with open(imgpath, 'rb') as f: - files = {'file': (imgpath, f, 'image/jpg')} - - response = requests.post(url = 'http://sl.vrgon.com:6062/v1/files/upload', headers=headers, files=files, data={'user': 'robot'}) - - img_id = json.loads(response.content)["id"] - - return img_id - -# 计算 -def img2text(path="./img/test.jpg"): - img_id = upload_img(path) - payload = { - "inputs": {}, - "query": "请问这道数学题的答案是多少,请直接给出数字答案", - "response_mode": "blocking", - "conversation_id": "", - "user": "robot", - "files": [ - { - "type": "image", - "transfer_method": "local_file", - "upload_file_id": img_id - } - ] - } - - headers = { - 'Authorization': 'Bearer {0}'.format(appkey), - 'Content-Type': 'application/json', - } - - response = requests.post(url = "http://sl.vrgon.com:6062/v1/chat-messages", headers=headers, data=json.dumps(payload)) - - ans = "" - if response.status_code == 200: - for line in response.iter_lines(): - temp = json.loads(line) - ans += temp["answer"] - - return ans - -if __name__=="__main__": - print img2text(path="./img/2.jpg") \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..1fb7797 --- /dev/null +++ b/main.py @@ -0,0 +1,11 @@ +#usr/bin/python2.7 + +from src.img2text2 import Chatbot + +if __name__ == "__main__": + base_url = "" + # Appkey is required + appkey = "app-" + + cb = Chatbot(appkey, base_url) + print cb.img2text(path="./img/2.jpg") diff --git a/readme.md b/readme.md index e6ce808..c4a304e 100644 --- a/readme.md +++ b/readme.md @@ -4,6 +4,18 @@ ## img2text2.py 为python2 版本 +1.下载包 +git clone http://sl.vrgon.com:3000/wangzixiang/img2textforrobot.git + +2.安装依赖包 +pip install requests + +3.测试运行 +python main.py + +4.使用 +将src 放入同级目录下,然后根据main.py 方式调用即可 + # sample ![](docs/0bec4563f67e5c03c7bf8c75a1618dd.png) diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/__pycache__/__init__.cpython-36.pyc b/src/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..3c74812ebb5a5d7499010b85db57d482435d0689 GIT binary patch literal 132 zcmXr!<>lhspqtJB1dl-k3@`#24nSPY0whuxf*CX!{Z=v*frJsnFGFXmn9$`HlvT8OLgW@b*T~l3CU0uIl^}blG z26t~yb%amIpX8y(2K_h;^&SXLIPH;eJkyZAO*rG$I^otD3z@LEy~N}@+zD;&hK^vu z5jL#2!V_-S=N|Vj(ltsU$|=&QY(qrSYNOnFEKrWaP|tx7B&3{#j8o3mNoeta+uT{F z5Pbq;f#&cEM4B|-%wnwPGu_4j_~6W7%)n4}5CttrL<$BHg2G3%)nLYr^L$CH8h<`k zA{xkE!!ouKy#|s#yZK; zq4$P}rI1e>dm-tGkjeRBCF#c<(S#EXt4*}3<{C(O^(f&b@?(L3m|%Q}2v#TH zy(AW~>h^BN=s{Me`&pe0AKiiXALo0u=_Y*B{140(h=QYp`EyVZ;sSbP#F7$n4n4&k z?iP%D*R2la{&ja`@e24+qyqHB#C0~Z9|C?7fVZ{NbqfoCt6sE54z~b2r|a<=^yh_@ zL!kR>tjJF2Y?}#;IBmGU^Lp8xL#u>xo`~O~Vb{ zB+Um}g%0pRu7=ee1fzb&Apl^$46G_iieOH{jwRL)7(1*(|M!x1>C!~PG?)y)Z8$kI zCd|}n$Yoe-w&8x6>IwnNmQUXvh+!?gkB!6BAdtC?n9FWI~p6m z3T-X=`M>$<1L?{u>M8J6&wwB_0IuHuSvc3+fcn+}X48PySU`cn4=$*k&sz;Dk)|+V zq=t)ETN?L86qz8xat{EL$V^7j*+JYJuSobSA>&dULJcp$hXgj1#On?lQsJUceJ9vm z+CqrmkCQZthWi$^&W|;lsyDqXJ6Ss2&icoWzHszNbNTRaKj1fY>fPnpcE5Y_|Y(K7pqH%h~v^jL-WT SCa;hPkT|Qo>N6XD0sR|Xu<$7W literal 0 HcmV?d00001 diff --git a/src/img2text.py b/src/img2text.py new file mode 100644 index 0000000..25e6529 --- /dev/null +++ b/src/img2text.py @@ -0,0 +1,73 @@ +# usr/bin/python3 +import requests +import json + +class Chatbot: + def __init__(self, appkey, base_url): + self.appkey = appkey + self.base_url = base_url + +# 上传图片 + def upload_img(self, imgpath="test.jpg"): + + headers = { + 'Authorization': f'Bearer {self.appkey}', + } + + files = { + 'file': (imgpath, open(imgpath, 'rb'), 'image/jpg'), # 使用实际文件路径和类型替换 + } + try: + response = requests.post(url = f'{self.base_url}/files/upload', headers=headers, files=files, data={'user': 'robot'}) + + except Exception as e: + print("error", e) + + img_id = json.loads(response.content)["id"] + + return img_id + + # 计算 + def img2text(self, path="./img/test.jpg"): + img_id = self.upload_img(path) + payload = { + "inputs": {}, + "query": "请问这道数学题的答案是多少,请直接给出数字答案", + "response_mode": "blocking", + "conversation_id": "", + "user": "robot", + "files": [ + { + "type": "image", + "transfer_method": "local_file", + "upload_file_id": img_id + } + ] + } + + headers = { + 'Authorization': f'Bearer {self.appkey}', + 'Content-Type': 'application/json', + } + + response = requests.post(url = f"{self.base_url}/chat-messages", headers=headers, data=json.dumps(payload)) + + ans = "" + if response.status_code == 200: + for line in response.iter_lines(): + # print(line.decode('unicode_escape')) + temp = json.loads(line) + ans += temp["answer"] + + return ans + + + +if __name__=="__main__": + + base_url = "http://sl.vrgon.com:6062/v1" + # 需填写appkey + appkey = "app-SKvWcmjJJk5PoTumsegFNd1N" + + cb = Chatbot(appkey, base_url) + print(cb.img2text(path="./img/2.jpg")) \ No newline at end of file diff --git a/src/img2text2.py b/src/img2text2.py new file mode 100644 index 0000000..4429561 --- /dev/null +++ b/src/img2text2.py @@ -0,0 +1,61 @@ +#usr/bin/python2.7 + +import requests +import json + +class Chatbot: + def init(self, appkey, base_url): + self.appkey = appkey + self.base_url = base_url + + # Upload pictures + def upload_img(self, imgpath="test.jpg"): + headers = { + 'Authorization': 'Bearer {}'.format(self.appkey), + } + + files = { + 'file': (imgpath, open(imgpath, 'rb'), 'image/jpg'), # Replace with the actual file path and type + } + try: + response = requests.post(url=self.base_url + '/files/upload', headers=headers, files=files, data={'user': 'robot'}) + except Exception as e: + print("error", e) + + img_id = json.loads(response.content)["id"] + + return img_id + + # 计算 + def img2text(self, path="./img/test.jpg"): + img_id = self.upload_img(path) + payload = { + "inputs": {}, + "query": "What is the answer to this math problem? Please give the numerical answer directly.", + "response_mode": "blocking", + "conversation_id": "", + "user": "robot", + "files": [ + { + "type": "image", + "transfer_method": "local_file", + "upload_file_id": img_id + } + ] + } + + headers = { + 'Authorization': 'Bearer {}'.format(self.appkey), + 'Content-Type': 'application/json', + } + + response = requests.post(url=self.base_url + '/chat-messages', headers=headers, data=json.dumps(payload)) + + ans = "" + if response.status_code == 200: + for line in response.iter_lines(): + if line: # Make sure the row is not empty + temp = json.loads(line) + ans += temp["answer"] + + return ans \ No newline at end of file