Download scripts/test_vision_api.py from Fred808/CU2: direct link, hf CLI and curl.
- Browser
- Download file 1.04 kB
-
https://huggingface.co/spaces/Fred808/CU2/resolve/main/scripts/test_vision_api.py
- Command line
-
hf download hf://spaces/Fred808/CU2/scripts/test_vision_api.py
-
curl -L -o test_vision_api.py https://huggingface.co/spaces/Fred808/CU2/resolve/main/scripts/test_vision_api.py
1.04 kB
| import requests | |
| import os | |
| def test_vision_api(image_path, prompt, api_url, output_json_path): | |
| with open(image_path, "rb") as img_file: | |
| files = {"image": img_file} | |
| data = {"prompt": prompt} | |
| response = requests.post(api_url, files=files, data=data) | |
| try: | |
| result = response.json() | |
| except Exception as e: | |
| print("Non-JSON response from API:") | |
| print(response.text) | |
| result = {"error": str(e), "raw_response": response.text} | |
| with open(output_json_path, "w") as f: | |
| import json | |
| json.dump({"frame": os.path.basename(image_path), "result": result}, f, indent=2) | |
| print(f"Logged response to {output_json_path}") | |
| if __name__ == "__main__": | |
| # Example usage | |
| test_vision_api( | |
| image_path="frames/sample_task_0001.png", | |
| prompt="Describe the user's task, app, actions, and the final goal.", | |
| api_url="http://localhost:8000/vision", | |
| output_json_path="annotations/test_vision_api_response.json" | |
| ) | |