naman1102 commited on
Commit
85d8e61
·
1 Parent(s): a14b206

Update tools.py

Browse files
Files changed (1) hide show
  1. tools.py +50 -61
tools.py CHANGED
@@ -88,67 +88,56 @@ def image_tool(task_id: str) -> str:
88
  headers = {"Authorization": f"Bearer {hf_token}"}
89
  print("DEBUG: HF token found, proceeding with API calls")
90
 
91
- # 4) Call HF's vision-ocr to extract text
92
- ocr_text = ""
93
- try:
94
- print("DEBUG: Calling HF OCR API...")
95
- ocr_resp = requests.post(
96
- "https://api-inference.huggingface.co/models/microsoft/trocr-base-printed",
97
- headers=headers,
98
- files={"file": image_bytes},
99
- timeout=30
100
- )
101
- print(f"DEBUG: OCR API response status: {ocr_resp.status_code}")
102
- ocr_resp.raise_for_status()
103
- ocr_json = ocr_resp.json()
104
- print(f"DEBUG: OCR API response: {ocr_json}")
105
-
106
- # Handle different response formats
107
- if isinstance(ocr_json, list) and len(ocr_json) > 0:
108
- # If it's a list, take the first result
109
- ocr_text = ocr_json[0].get("generated_text", "").strip()
110
- elif isinstance(ocr_json, dict):
111
- ocr_text = ocr_json.get("generated_text", "").strip()
112
-
113
- if not ocr_text:
114
- ocr_text = "(no visible text detected)"
115
- print(f"DEBUG: Extracted OCR text: {ocr_text}")
116
- except Exception as e:
117
- ocr_text = f"Error during HF OCR: {e}"
118
- print(f"DEBUG: OCR failed: {e}")
119
-
120
- # 5) Call HF's image-captioning to get a brief description
121
- caption = ""
122
- try:
123
- print("DEBUG: Calling HF Image Captioning API...")
124
- cap_resp = requests.post(
125
- "https://api-inference.huggingface.co/models/Salesforce/blip-image-captioning-base",
126
- headers=headers,
127
- files={"file": image_bytes},
128
- timeout=30
129
- )
130
- print(f"DEBUG: Captioning API response status: {cap_resp.status_code}")
131
- cap_resp.raise_for_status()
132
- cap_json = cap_resp.json()
133
- print(f"DEBUG: Captioning API response: {cap_json}")
134
-
135
- # Handle different response formats
136
- if isinstance(cap_json, list) and len(cap_json) > 0:
137
- caption = cap_json[0].get("generated_text", "").strip()
138
- elif isinstance(cap_json, dict):
139
- caption = cap_json.get("generated_text", "").strip()
140
-
141
- if not caption:
142
- caption = "(no caption generated)"
143
- print(f"DEBUG: Generated caption: {caption}")
144
- except Exception as e:
145
- caption = f"Error during HF captioning: {e}"
146
- print(f"DEBUG: Captioning failed: {e}")
147
-
148
- # 6) Combine OCR + caption
149
- combined = f"OCR text:\n{ocr_text}\n\nImage caption:\n{caption}"
150
- print(f"DEBUG: Final result: {combined}")
151
- return combined
152
 
153
  @tool
154
  def excel_tool(task_id: str) -> str:
 
88
  headers = {"Authorization": f"Bearer {hf_token}"}
89
  print("DEBUG: HF token found, proceeding with API calls")
90
 
91
+ # Try different HF models for image analysis
92
+ models_to_try = [
93
+ "nlpconnect/vit-gpt2-image-captioning",
94
+ "Salesforce/blip-image-captioning-large",
95
+ "microsoft/git-base-coco",
96
+ "microsoft/git-large-coco"
97
+ ]
98
+
99
+ result_text = ""
100
+ success = False
101
+
102
+ for model_name in models_to_try:
103
+ try:
104
+ print(f"DEBUG: Trying model: {model_name}")
105
+ resp = requests.post(
106
+ f"https://api-inference.huggingface.co/models/{model_name}",
107
+ headers=headers,
108
+ files={"file": image_bytes},
109
+ timeout=30
110
+ )
111
+ print(f"DEBUG: {model_name} response status: {resp.status_code}")
112
+
113
+ if resp.status_code == 200:
114
+ resp_json = resp.json()
115
+ print(f"DEBUG: {model_name} response: {resp_json}")
116
+
117
+ # Handle different response formats
118
+ if isinstance(resp_json, list) and len(resp_json) > 0:
119
+ result_text = resp_json[0].get("generated_text", "").strip()
120
+ elif isinstance(resp_json, dict):
121
+ result_text = resp_json.get("generated_text", "").strip()
122
+
123
+ if result_text:
124
+ print(f"DEBUG: Successfully got result from {model_name}: {result_text}")
125
+ success = True
126
+ break
127
+ else:
128
+ print(f"DEBUG: {model_name} failed with status {resp.status_code}")
129
+
130
+ except Exception as e:
131
+ print(f"DEBUG: {model_name} failed with error: {e}")
132
+ continue
133
+
134
+ if not success or not result_text:
135
+ result_text = "Unable to analyze image - all HuggingFace models failed or returned empty results"
136
+
137
+ # Format the result
138
+ final_result = f"Image Analysis Result:\n{result_text}"
139
+ print(f"DEBUG: Final result: {final_result}")
140
+ return final_result
 
 
 
 
 
 
 
 
 
 
 
141
 
142
  @tool
143
  def excel_tool(task_id: str) -> str: