Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -116,19 +116,47 @@ def polish_prompt_hf(prompt, img_list):
|
|
| 116 |
api_key=api_key,
|
| 117 |
)
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
# Format the messages for the chat completions API
|
| 120 |
-
sys_promot = "you are a helpful assistant, you should provide useful answers to users."
|
| 121 |
messages = [
|
| 122 |
-
{"role": "system", "content":
|
| 123 |
-
{
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
# Call the API
|
| 130 |
completion = client.chat.completions.create(
|
| 131 |
-
model="Qwen/
|
| 132 |
messages=messages,
|
| 133 |
)
|
| 134 |
|
|
@@ -149,11 +177,6 @@ def polish_prompt_hf(prompt, img_list):
|
|
| 149 |
|
| 150 |
polished_prompt = polished_prompt.strip().replace("\n", " ")
|
| 151 |
return polished_prompt
|
| 152 |
-
|
| 153 |
-
except Exception as e:
|
| 154 |
-
print(f"Error during API call to Hugging Face: {e}")
|
| 155 |
-
# Fallback to original prompt if enhancement fails
|
| 156 |
-
return prompt
|
| 157 |
|
| 158 |
|
| 159 |
|
|
|
|
| 116 |
api_key=api_key,
|
| 117 |
)
|
| 118 |
|
| 119 |
+
image_url = None
|
| 120 |
+
if img is not None:
|
| 121 |
+
# If img is a PIL Image
|
| 122 |
+
if hasattr(img, 'save'): # Check if it's a PIL Image
|
| 123 |
+
buffered = BytesIO()
|
| 124 |
+
img.save(buffered, format="PNG")
|
| 125 |
+
img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 126 |
+
image_url = f"data:image/png;base64,{img_base64}"
|
| 127 |
+
# If img is already a file path (string)
|
| 128 |
+
elif isinstance(img, str):
|
| 129 |
+
with open(img, "rb") as image_file:
|
| 130 |
+
img_base64 = base64.b64encode(image_file.read()).decode('utf-8')
|
| 131 |
+
image_url = f"data:image/png;base64,{img_base64}"
|
| 132 |
+
else:
|
| 133 |
+
print(f"Warning: Unexpected image type: {type(img)}")
|
| 134 |
+
return original_prompt
|
| 135 |
+
|
| 136 |
# Format the messages for the chat completions API
|
|
|
|
| 137 |
messages = [
|
| 138 |
+
{"role": "system", "content": system_prompt},
|
| 139 |
+
{
|
| 140 |
+
"role": "user",
|
| 141 |
+
"content": [
|
| 142 |
+
{
|
| 143 |
+
"type": "text",
|
| 144 |
+
"text": original_prompt
|
| 145 |
+
},
|
| 146 |
+
{
|
| 147 |
+
"type": "image_url",
|
| 148 |
+
"image_url": {
|
| 149 |
+
"url": image_url
|
| 150 |
+
}
|
| 151 |
+
}
|
| 152 |
+
]
|
| 153 |
+
}
|
| 154 |
+
]
|
| 155 |
+
|
| 156 |
|
| 157 |
# Call the API
|
| 158 |
completion = client.chat.completions.create(
|
| 159 |
+
model="Qwen/Qwen2.5-VL-72B-Instruct",
|
| 160 |
messages=messages,
|
| 161 |
)
|
| 162 |
|
|
|
|
| 177 |
|
| 178 |
polished_prompt = polished_prompt.strip().replace("\n", " ")
|
| 179 |
return polished_prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
|
| 182 |
|