Created
September 26, 2024 00:35
-
-
Save hemanth/e7245b170b3a6ad6e999652d81c9842a to your computer and use it in GitHub Desktop.
Explain XKCD with TogetherAI
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
import os | |
import argparse | |
from together import Together | |
def analyze_image(image_url): | |
client = Together(api_key=os.environ.get('TOGETHER_API_KEY')) | |
response = client.chat.completions.create( | |
model="meta-llama/Llama-Vision-Free", | |
messages=[ | |
{ | |
"role": "user", | |
"content": [ | |
{ | |
"type": "text", | |
"text": "explain this image" | |
}, | |
{ | |
"type": "image_url", | |
"image_url": { | |
"url": image_url | |
} | |
} | |
] | |
} | |
], | |
max_tokens=512, | |
temperature=0.7, | |
top_p=0.7, | |
top_k=50, | |
repetition_penalty=1, | |
stop=["<|eot_id|>","<|eom_id|>"], | |
stream=True | |
) | |
full_response = "" | |
for chunk in response: | |
if chunk.choices[0].delta.content is not None: | |
full_response += chunk.choices[0].delta.content | |
return full_response | |
def main(): | |
parser = argparse.ArgumentParser(description="Analyze an image using the Together AI API") | |
parser.add_argument("image_url", help="URL of the image to analyze") | |
args = parser.parse_args() | |
try: | |
result = analyze_image(args.image_url) | |
print(result) | |
except Exception as e: | |
print(f"An error occurred: {e}") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment