-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from Integration-Automation/dev
Add image upload using URL | File path | base64
- Loading branch information
Showing
7 changed files
with
161 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import base64 | ||
import json | ||
|
||
import aiohttp | ||
|
||
payload = { | ||
"imageInfo": {}, | ||
"knowledgeRequest": { | ||
"invokedSkills": ["ImageById"], | ||
"subscriptionId": "Bing.Chat.Multimodal", | ||
"invokedSkillsRequestData": {"enableFaceBlur": True}, | ||
"convoData": { | ||
"convoid": "", | ||
"convotone": "Balanced" | ||
} | ||
} | ||
} | ||
|
||
|
||
async def upload_image_url(image_url: str, conversation_id: str, proxy: str = None, face_blur: bool = True): | ||
async with aiohttp.ClientSession( | ||
headers={"Referer": "https://www.bing.com/search?q=Bing+AI&showconv=1&FORM=hpcodx"}, | ||
) as session: | ||
url = "https://www.bing.com/images/kblob" | ||
|
||
new_payload = payload | ||
new_payload.get("knowledgeRequest").update( | ||
{"invokedSkillsRequestData": {"enableFaceBlur": face_blur}}) | ||
new_payload.get("imageInfo").update({"url": image_url}) | ||
new_payload.get("knowledgeRequest").get("convoData").update({"convoid": conversation_id}) | ||
data = aiohttp.FormData() | ||
data.add_field('knowledgeRequest', json.dumps(new_payload), content_type="application/json") | ||
async with session.post(url, data=data, proxy=proxy) as resp: | ||
return (await resp.json())["blobId"] | ||
|
||
|
||
async def upload_image(filename: str = None, base64_image: str = None, proxy: str = None, face_blur: bool = True): | ||
async with aiohttp.ClientSession( | ||
headers={"Referer": "https://www.bing.com/search?q=Bing+AI&showconv=1&FORM=hpcodx"}, | ||
) as session: | ||
url = "https://www.bing.com/images/kblob" | ||
|
||
new_payload = payload.get("knowledgeRequest").update( | ||
{"invokedSkillsRequestData": {"enableFaceBlur": face_blur}}) | ||
|
||
if filename is not None: | ||
with open(filename, 'rb') as f: | ||
file_data = f.read() | ||
image_base64 = base64.b64encode(file_data) | ||
elif base64_image is not None: | ||
image_base64 = base64_image | ||
else: | ||
raise Exception('no image provided') | ||
|
||
data = aiohttp.FormData() | ||
data.add_field('knowledgeRequest', json.dumps(new_payload), content_type="application/json") | ||
data.add_field('imageBase64', image_base64, content_type="application/octet-stream") | ||
|
||
async with session.post(url, data=data, proxy=proxy) as resp: | ||
return (await resp.json())["blobId"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import asyncio | ||
import json | ||
from pathlib import Path | ||
|
||
from re_edge_gpt import Chatbot | ||
from re_edge_gpt import ConversationStyle | ||
|
||
|
||
# If you are using jupyter pls install this package | ||
# from nest_asyncio import apply | ||
|
||
|
||
async def test_ask() -> None: | ||
bot = None | ||
try: | ||
cookies: list[dict] = json.loads(open( | ||
str(Path(str(Path.cwd()) + "/bing_cookies.json")), encoding="utf-8").read()) | ||
bot = await Chatbot.create(cookies=cookies) | ||
response = await bot.ask( | ||
prompt="What does this image show?", | ||
conversation_style=ConversationStyle.balanced, | ||
simplify_response=True, | ||
attachment={"image_url": r"https://images.yourstory.com/cs/2/96eabe90392211eb93f18319e8c07a74/Image54nh-1683225460858.jpg"}) | ||
# If you are using non ascii char you need set ensure_ascii=False | ||
print(json.dumps(response, indent=2, ensure_ascii=False)) | ||
# Raw response | ||
# print(response) | ||
assert response | ||
except Exception as error: | ||
raise error | ||
finally: | ||
if bot is not None: | ||
await bot.close() | ||
|
||
|
||
if __name__ == "__main__": | ||
# If you are using jupyter pls use nest_asyncio apply() | ||
# apply() | ||
try: | ||
loop = asyncio.get_running_loop() | ||
except RuntimeError: | ||
loop = asyncio.get_event_loop() | ||
loop.run_until_complete(test_ask()) |