Spaces:
Runtime error
Runtime error
Commit
·
5dd1cbf
1
Parent(s):
f245369
changing endpoint call
Browse files
main.py
CHANGED
|
@@ -86,28 +86,26 @@ async def upload_and_encode_image(file: UploadFile = File(...)):
|
|
| 86 |
raise HTTPException(status_code=400, detail=f"Invalid file: {e}")
|
| 87 |
|
| 88 |
@app.get("/predict")
|
| 89 |
-
def predict(
|
| 90 |
"""
|
| 91 |
Generates a description for an image using the Qwen-2-VL model.
|
| 92 |
|
| 93 |
Args:
|
| 94 |
-
|
| 95 |
prompt (str): The text prompt to guide the model's response.
|
| 96 |
|
| 97 |
Returns:
|
| 98 |
str: The generated description of the image.
|
| 99 |
"""
|
| 100 |
|
| 101 |
-
image = encode_image(image_url)
|
| 102 |
-
|
| 103 |
|
| 104 |
# Create the input message structure
|
| 105 |
messages = [
|
| 106 |
{
|
| 107 |
"role": "user",
|
| 108 |
"content": [
|
| 109 |
-
{"type": "image", "image": f"data:image;base64,{
|
| 110 |
-
{"type": "text", "text": prompt},
|
| 111 |
],
|
| 112 |
}
|
| 113 |
]
|
|
|
|
| 86 |
raise HTTPException(status_code=400, detail=f"Invalid file: {e}")
|
| 87 |
|
| 88 |
@app.get("/predict")
|
| 89 |
+
def predict(data: str = Query(...), prompt: str = Query(...)):
|
| 90 |
"""
|
| 91 |
Generates a description for an image using the Qwen-2-VL model.
|
| 92 |
|
| 93 |
Args:
|
| 94 |
+
data (any): The encoded image and the prompt to be used.
|
| 95 |
prompt (str): The text prompt to guide the model's response.
|
| 96 |
|
| 97 |
Returns:
|
| 98 |
str: The generated description of the image.
|
| 99 |
"""
|
| 100 |
|
|
|
|
|
|
|
| 101 |
|
| 102 |
# Create the input message structure
|
| 103 |
messages = [
|
| 104 |
{
|
| 105 |
"role": "user",
|
| 106 |
"content": [
|
| 107 |
+
{"type": "image", "image": f"data:image;base64,{data.image_base64}"},
|
| 108 |
+
{"type": "text", "text": data.prompt},
|
| 109 |
],
|
| 110 |
}
|
| 111 |
]
|
model.py
CHANGED
|
@@ -1,22 +1,37 @@
|
|
| 1 |
import requests
|
| 2 |
|
| 3 |
-
#
|
| 4 |
-
|
| 5 |
-
# --data-urlencode "prompt=Describe this image."
|
| 6 |
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
}
|
| 15 |
|
| 16 |
-
|
| 17 |
-
response = requests.get(url, params=params)
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
else:
|
| 22 |
-
print("Error:",
|
|
|
|
| 1 |
import requests
|
| 2 |
|
| 3 |
+
# Define base URL for your Hugging Face Space
|
| 4 |
+
BASE_URL = "https://danilohssantana-qwen2-5-vl-api.hf.space"
|
|
|
|
| 5 |
|
| 6 |
+
# Image URL to be encoded
|
| 7 |
+
image_url = "https://cdn.britannica.com/35/238335-050-2CB2EB8A/Lionel-Messi-Argentina-Netherlands-World-Cup-Qatar-2022.jpg"
|
| 8 |
|
| 9 |
+
# Step 1: Download the image
|
| 10 |
+
response = requests.get(image_url)
|
| 11 |
+
if response.status_code != 200:
|
| 12 |
+
print("Error downloading image:", response.status_code, response.text)
|
| 13 |
+
exit()
|
| 14 |
|
| 15 |
+
# Step 2: Send the image to the encode-image endpoint
|
| 16 |
+
files = {"file": ("image.jpg", response.content, "image/jpeg")}
|
| 17 |
+
encode_response = requests.post(f"{BASE_URL}/encode-image/", files=files)
|
| 18 |
+
|
| 19 |
+
if encode_response.status_code != 200:
|
| 20 |
+
print("Error encoding image:", encode_response.status_code, encode_response.text)
|
| 21 |
+
exit()
|
| 22 |
+
|
| 23 |
+
encoded_image = encode_response.json().get("encoded_image")
|
| 24 |
+
|
| 25 |
+
# Step 3: Send the encoded image to the predict endpoint
|
| 26 |
+
predict_payload = {
|
| 27 |
+
"image_base64": encoded_image,
|
| 28 |
+
"prompt": "describe the image",
|
| 29 |
}
|
| 30 |
|
| 31 |
+
predict_response = requests.get(f"{BASE_URL}/predict", params=predict_payload)
|
|
|
|
| 32 |
|
| 33 |
+
# Step 4: Print the response
|
| 34 |
+
if predict_response.status_code == 200:
|
| 35 |
+
print("Response:", predict_response.json())
|
| 36 |
else:
|
| 37 |
+
print("Error:", predict_response.status_code, predict_response.text)
|