Update main.py
Browse files
main.py
CHANGED
@@ -14,12 +14,16 @@ client = Client("Ashrafb/moondream1", hf_token=hf_token)
|
|
14 |
|
15 |
# Function to generate captions for uploaded images
|
16 |
def get_caption(image, additional_context):
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
23 |
|
24 |
@app.post("/uploadfile/")
|
25 |
async def upload_file(image: UploadFile = File(...), additional_context: str = Form(...)):
|
@@ -32,14 +36,9 @@ async def upload_file(image: UploadFile = File(...), additional_context: str = F
|
|
32 |
image_data = image_file.read()
|
33 |
result = get_caption(image_data, additional_context)
|
34 |
return {"result": result}
|
35 |
-
except HTTPException as http_exc:
|
36 |
-
# Handle HTTPException raised by FastAPI
|
37 |
-
raise http_exc
|
38 |
except Exception as e:
|
39 |
-
|
40 |
-
|
41 |
-
detail_msg = str(e)
|
42 |
-
raise HTTPException(status_code=500, detail={"error": error_msg, "detail": detail_msg})
|
43 |
|
44 |
# Serve static files
|
45 |
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
|
|
14 |
|
15 |
# Function to generate captions for uploaded images
|
16 |
def get_caption(image, additional_context):
|
17 |
+
try:
|
18 |
+
if additional_context.strip():
|
19 |
+
context = additional_context
|
20 |
+
else:
|
21 |
+
context = "What is this image? Describe this image to someone who is visually impaired."
|
22 |
+
result = client.predict(image, context, api_name="/predict")
|
23 |
+
return result
|
24 |
+
except Exception as e:
|
25 |
+
error_msg = "Error occurred while generating caption: {}".format(str(e))
|
26 |
+
raise HTTPException(status_code=500, detail={"error": error_msg})
|
27 |
|
28 |
@app.post("/uploadfile/")
|
29 |
async def upload_file(image: UploadFile = File(...), additional_context: str = Form(...)):
|
|
|
36 |
image_data = image_file.read()
|
37 |
result = get_caption(image_data, additional_context)
|
38 |
return {"result": result}
|
|
|
|
|
|
|
39 |
except Exception as e:
|
40 |
+
error_msg = "Error occurred while processing image: {}".format(str(e))
|
41 |
+
raise HTTPException(status_code=500, detail={"error": error_msg})
|
|
|
|
|
42 |
|
43 |
# Serve static files
|
44 |
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|