Spaces:
Running
Running
| import json | |
| import os | |
| from openai import OpenAI | |
| openai_api_key = os.environ.get("OPENAI_API_KEY") | |
| if openai_api_key: | |
| print("OPENAI_API_KEY found.") | |
| # print(openai_api_key) | |
| else: | |
| print("OPENAI_API_KEY not found.") | |
| client = OpenAI( | |
| api_key=openai_api_key | |
| ) | |
| def gpt_process_image(input_image64, model_name, prompt, system_instruction="", temperatura=0.0): | |
| if input_image64 is None: | |
| return None, "No objects detected." | |
| # print (prompt) | |
| print("model_name:", model_name) | |
| print("Temperatura:", temperatura) | |
| response = client.chat.completions.create( | |
| model=model_name, | |
| messages=[ | |
| {"role": "system", "content": f"{system_instruction}"}, | |
| {"role": "user", "content": [ | |
| {"type": "text", "text": f"{prompt}"}, | |
| {"type": "image_url", "image_url": { | |
| "url": f"data:image/jpg;base64,{input_image64}"} | |
| } | |
| ]} | |
| ], | |
| temperature=temperatura, | |
| response_format={"type": "json_object"} | |
| ) | |
| json_content = json.loads(response.choices[0].message.content) | |
| json_content['input_tokens'] = response.usage.prompt_tokens | |
| json_content['output_tokens'] = response.usage.completion_tokens | |
| json_content['total_tokens'] = response.usage.total_tokens | |
| print(json_content) | |
| return json.dumps(json_content, indent=4) | |