Spaces:
Runtime error
Runtime error
Kashif Ahmed commited on
Commit ·
2b3344e
1
Parent(s): 0721f89
22
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
|
| 3 |
from fastapi.responses import JSONResponse
|
| 4 |
from typing import Optional
|
| 5 |
import logging
|
|
@@ -14,8 +14,10 @@ app = FastAPI()
|
|
| 14 |
|
| 15 |
|
| 16 |
@app.post("/predict")
|
| 17 |
-
async def predict(file: Optional[UploadFile] = File(None), text: Optional[str] = Form(None)):
|
| 18 |
logger.info(f"Received request: file={file is not None}, text={text is not None}")
|
|
|
|
|
|
|
| 19 |
try:
|
| 20 |
if file is None and text is None:
|
| 21 |
logger.warning("No file or text provided")
|
|
@@ -29,13 +31,16 @@ async def predict(file: Optional[UploadFile] = File(None), text: Optional[str] =
|
|
| 29 |
'message': "File received",
|
| 30 |
'filename': file.filename
|
| 31 |
})
|
| 32 |
-
|
| 33 |
logger.info(f"Text received: {text}")
|
| 34 |
# Here you would process the text
|
| 35 |
return JSONResponse({
|
| 36 |
'message': "Text received",
|
| 37 |
'text': text
|
| 38 |
})
|
|
|
|
|
|
|
|
|
|
| 39 |
except HTTPException as he:
|
| 40 |
logger.error(f"HTTP Exception: {str(he.detail)}")
|
| 41 |
raise he
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from fastapi import FastAPI, File, UploadFile, Form, HTTPException, Request
|
| 3 |
from fastapi.responses import JSONResponse
|
| 4 |
from typing import Optional
|
| 5 |
import logging
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
@app.post("/predict")
|
| 17 |
+
async def predict(request: Request, file: Optional[UploadFile] = File(None), text: Optional[str] = Form(None)):
|
| 18 |
logger.info(f"Received request: file={file is not None}, text={text is not None}")
|
| 19 |
+
logger.info(f"Request content type: {request.headers.get('content-type')}")
|
| 20 |
+
|
| 21 |
try:
|
| 22 |
if file is None and text is None:
|
| 23 |
logger.warning("No file or text provided")
|
|
|
|
| 31 |
'message': "File received",
|
| 32 |
'filename': file.filename
|
| 33 |
})
|
| 34 |
+
elif text:
|
| 35 |
logger.info(f"Text received: {text}")
|
| 36 |
# Here you would process the text
|
| 37 |
return JSONResponse({
|
| 38 |
'message': "Text received",
|
| 39 |
'text': text
|
| 40 |
})
|
| 41 |
+
else:
|
| 42 |
+
logger.warning("Unexpected input")
|
| 43 |
+
raise HTTPException(status_code=400, detail="Unexpected input. Please provide either a file or text.")
|
| 44 |
except HTTPException as he:
|
| 45 |
logger.error(f"HTTP Exception: {str(he.detail)}")
|
| 46 |
raise he
|