JabrilJacobs commited on
Commit
1fc43d8
·
verified ·
1 Parent(s): 4e9948c

Update tools.py

Browse files
Files changed (1) hide show
  1. tools.py +41 -1
tools.py CHANGED
@@ -14,4 +14,44 @@ def get_hub_stats(author: str) -> str:
14
  else:
15
  return f"No models found for author {author}."
16
  except Exception as e:
17
- return f"Error fetching models for {author}: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  else:
15
  return f"No models found for author {author}."
16
  except Exception as e:
17
+ return f"Error fetching models for {author}: {str(e)}"
18
+
19
+
20
+ def analyze_image(image_path: str, question: str = "What do you see in this image?") -> str:
21
+ """Analyze an image using LangChain's ChatOpenAI with vision"""
22
+
23
+ def encode_image_to_base64(image_path: str) -> str:
24
+ """Convert image file to base64 string"""
25
+ try:
26
+ with open(image_path, "rb") as image_file:
27
+ return base64.b64encode(image_file.read()).decode('utf-8')
28
+ except Exception as e:
29
+ raise Exception(f"Error encoding image: {e}")
30
+
31
+ try:
32
+ # Create vision-capable LLM
33
+ vision_llm = ChatOpenAI(model="gpt-4o", max_tokens=1000)
34
+
35
+ # Encode the image
36
+ base64_image = encode_image_to_base64(image_path)
37
+
38
+ # Create message with image
39
+ message = HumanMessage(
40
+ content=[
41
+ {"type": "text", "text": question},
42
+ {
43
+ "type": "image_url",
44
+ "image_url": {
45
+ "url": f"data:image/jpeg;base64,{base64_image}",
46
+ "detail": "high"
47
+ }
48
+ }
49
+ ]
50
+ )
51
+
52
+ # Get response
53
+ response = vision_llm.invoke([message])
54
+ return response.content
55
+
56
+ except Exception as e:
57
+ return f"Error analyzing image: {e}"