Update main.py
Browse files
main.py
CHANGED
@@ -14,6 +14,15 @@ app = FastAPI()
|
|
14 |
hf_token = os.environ.get('HF_TOKEN')
|
15 |
client = Client("Ashrafb/moondream_captioning", hf_token=hf_token)
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
# Function to make API prediction
|
18 |
def predict_image_answer(file_path, Question):
|
19 |
result = client.predict(file_path, Question, api_name="/get_caption")
|
@@ -26,10 +35,13 @@ async def answer(image: UploadFile = File(...), Question: str = Form(...)):
|
|
26 |
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
27 |
shutil.copyfileobj(image.file, temp_file)
|
28 |
temp_file_path = temp_file.name
|
|
|
29 |
answer = predict_image_answer(temp_file_path, Question)
|
|
|
30 |
os.unlink(temp_file_path)
|
31 |
return answer
|
32 |
except Exception as e:
|
|
|
33 |
raise HTTPException(status_code=500, detail=str(e))
|
34 |
|
35 |
# Serving static files
|
@@ -39,3 +51,4 @@ app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
|
39 |
@app.get("/")
|
40 |
def index() -> FileResponse:
|
41 |
return FileResponse(path="/app/static/index.html", media_type="text/html")
|
|
|
|
14 |
hf_token = os.environ.get('HF_TOKEN')
|
15 |
client = Client("Ashrafb/moondream_captioning", hf_token=hf_token)
|
16 |
|
17 |
+
# Function to make API prediction
|
18 |
+
import logging
|
19 |
+
|
20 |
+
# Configure logging
|
21 |
+
logging.basicConfig(level=logging.DEBUG)
|
22 |
+
|
23 |
+
# FastAPI setup
|
24 |
+
app = FastAPI()
|
25 |
+
|
26 |
# Function to make API prediction
|
27 |
def predict_image_answer(file_path, Question):
|
28 |
result = client.predict(file_path, Question, api_name="/get_caption")
|
|
|
35 |
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
36 |
shutil.copyfileobj(image.file, temp_file)
|
37 |
temp_file_path = temp_file.name
|
38 |
+
logging.debug("Received file: %s", temp_file_path)
|
39 |
answer = predict_image_answer(temp_file_path, Question)
|
40 |
+
logging.debug("Prediction result: %s", answer)
|
41 |
os.unlink(temp_file_path)
|
42 |
return answer
|
43 |
except Exception as e:
|
44 |
+
logging.error("An error occurred: %s", e)
|
45 |
raise HTTPException(status_code=500, detail=str(e))
|
46 |
|
47 |
# Serving static files
|
|
|
51 |
@app.get("/")
|
52 |
def index() -> FileResponse:
|
53 |
return FileResponse(path="/app/static/index.html", media_type="text/html")
|
54 |
+
|