Update app.py
Browse files
app.py
CHANGED
@@ -475,7 +475,6 @@ from transformers import AutoModelForCausalLM, AutoProcessor, AutoTokenizer
|
|
475 |
import tempfile
|
476 |
import os
|
477 |
import base64
|
478 |
-
from openai import OpenAI
|
479 |
|
480 |
def encode_image(image_path):
|
481 |
try:
|
@@ -486,53 +485,6 @@ def encode_image(image_path):
|
|
486 |
print(f"Error in encode_image: {str(e)}")
|
487 |
raise
|
488 |
|
489 |
-
client = OpenAI(
|
490 |
-
api_key=os.getenv("DASHSCOPE_API_KEY", 'sk-5d71cf15539f46ef9ea9283a821f7ee7'),
|
491 |
-
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
|
492 |
-
)
|
493 |
-
|
494 |
-
def inference_with_api(image_path, prompt, sys_prompt="You are a helpful visual analysis assistant that specializes in determining how products and people should be positioned on a canvas for optimal visual presentation.", model_id="qwen2.5-vl-72b-instruct", min_pixels=512*28*28, max_pixels=2048*28*28):
|
495 |
-
try:
|
496 |
-
base64_image = encode_image(image_path)
|
497 |
-
messages = [
|
498 |
-
{
|
499 |
-
"role": "system",
|
500 |
-
"content": [{"type": "text", "text": sys_prompt}]
|
501 |
-
},
|
502 |
-
{
|
503 |
-
"role": "user",
|
504 |
-
"content": [
|
505 |
-
{
|
506 |
-
"type": "image_url",
|
507 |
-
"min_pixels": min_pixels,
|
508 |
-
"max_pixels": max_pixels,
|
509 |
-
"image_url": {"url": f"data:image/jpeg;base64,{base64_image}"},
|
510 |
-
},
|
511 |
-
{"type": "text", "text": prompt},
|
512 |
-
],
|
513 |
-
}
|
514 |
-
]
|
515 |
-
retries = 3
|
516 |
-
for attempt in range(retries):
|
517 |
-
try:
|
518 |
-
completion = client.chat.completions.create(
|
519 |
-
model=model_id,
|
520 |
-
messages=messages,
|
521 |
-
timeout=30 # Meningkatkan timeout dari 15 ke 30 detik
|
522 |
-
)
|
523 |
-
return completion.choices[0].message.content
|
524 |
-
except Exception as inner_e:
|
525 |
-
# If the error message contains "Connection error", retry
|
526 |
-
if "Connection error" in str(inner_e):
|
527 |
-
print(f"Connection error on attempt {attempt+1}: {inner_e}. Retrying in 2 seconds...")
|
528 |
-
time.sleep(2)
|
529 |
-
else:
|
530 |
-
raise
|
531 |
-
raise Exception("Failed to complete API call after multiple retries due to connection errors.")
|
532 |
-
except Exception as e:
|
533 |
-
print(f"Error in inference_with_api: {str(e)}")
|
534 |
-
raise
|
535 |
-
|
536 |
def classify_image(image_path, unique_items):
|
537 |
try:
|
538 |
image = Image.open(image_path).convert("RGB")
|
|
|
475 |
import tempfile
|
476 |
import os
|
477 |
import base64
|
|
|
478 |
|
479 |
def encode_image(image_path):
|
480 |
try:
|
|
|
485 |
print(f"Error in encode_image: {str(e)}")
|
486 |
raise
|
487 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
488 |
def classify_image(image_path, unique_items):
|
489 |
try:
|
490 |
image = Image.open(image_path).convert("RGB")
|