Skip to content

Instantly share code, notes, and snippets.

@hemanth
Created September 26, 2024 00:35
Show Gist options
  • Save hemanth/e7245b170b3a6ad6e999652d81c9842a to your computer and use it in GitHub Desktop.
Save hemanth/e7245b170b3a6ad6e999652d81c9842a to your computer and use it in GitHub Desktop.
Explain XKCD with TogetherAI
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