generated from kyegomez/Python-Package-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kye
committed
Mar 19, 2024
1 parent
4263ec3
commit b41fe30
Showing
4 changed files
with
61 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,54 @@ | ||
import requests | ||
import asyncio | ||
import base64 | ||
from PIL import Image | ||
from io import BytesIO | ||
|
||
import httpx | ||
from httpx import Timeout | ||
from PIL import Image | ||
|
||
|
||
# Convert image to Base64 | ||
def image_to_base64(image_path): | ||
async def image_to_base64(image_path): | ||
with Image.open(image_path) as image: | ||
buffered = BytesIO() | ||
image.save(buffered, format="JPEG") | ||
image.save(buffered, format="JPEG", quality=85) | ||
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8") | ||
return img_str | ||
|
||
|
||
# Replace 'image.jpg' with the path to your image | ||
base64_image = image_to_base64("test.jpg") | ||
text_data = {"type": "text", "text": "Describe what is in the image"} | ||
image_data = { | ||
"type": "image_url", | ||
"image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}, | ||
} | ||
|
||
# Construct the request data | ||
request_data = { | ||
"model": "cogvlm-chat-17b", | ||
"messages": [{"role": "user", "content": [text_data, image_data]}], | ||
"temperature": 0.8, | ||
"top_p": 0.9, | ||
"max_tokens": 1024, | ||
} | ||
|
||
# Specify the URL of your FastAPI application | ||
url = "https://api.swarms.world/v1/chat/completions" | ||
|
||
# Send the request | ||
response = requests.post(url, json=request_data) | ||
# Print the response from the server | ||
print(response.text) | ||
async def send_request(base64_image): | ||
text_data = {"type": "text", "text": "Describe what is in the image"} | ||
image_data = { | ||
"type": "image_url", | ||
"image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}, | ||
} | ||
|
||
# Construct the request data | ||
request_data = { | ||
"model": "cogvlm-chat-17b", | ||
"messages": [{"role": "user", "content": [text_data, image_data]}], | ||
"temperature": 0.8, | ||
"top_p": 0.9, | ||
"max_tokens": 1024, | ||
} | ||
|
||
# Specify the URL of your FastAPI application | ||
url = "https://api.swarms.world/v1/chat/completions" | ||
|
||
# Timeout | ||
timeout = Timeout(10.0, read=30.0) | ||
|
||
# Use httpx.AsyncClient for asynchronous requests | ||
async with httpx.AsyncClient(timeout=timeout, http2=True) as client: | ||
response = await client.post(url, json=request_data) | ||
print(response.text) | ||
|
||
|
||
async def main(): | ||
# Replace 'image.jpg' with the path to your image | ||
base64_image = await image_to_base64("test.jpg") | ||
await send_request(base64_image) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
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,16 @@ | ||
import openai | ||
|
||
# Set the API key | ||
client = openai.OpenAI( | ||
base_url="https://api.swarms.world", api_key="sk-1g3Z3y2c1f2z3d4b5p6w7c8b9v0n1m2" | ||
) | ||
|
||
# Create a client object to interact with the OpenAI API | ||
# Make a chat completion request | ||
chat_completion = client.chat.completions.create( | ||
model="cogvlm-chat-17b", | ||
messages=[{"role": "user", "content": "Write me a love song"}], | ||
temperature=0.7, | ||
) | ||
|
||
# Send a message to the chat model and get a completion response |
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