Spaces:
Runtime error
Runtime error
Kashif Ahmed commited on
Commit ·
0721f89
1
Parent(s): 794edb5
222
Browse files
app.py
CHANGED
|
@@ -1,38 +1,65 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from fastapi import FastAPI, File, UploadFile, Form
|
| 3 |
from fastapi.responses import JSONResponse
|
| 4 |
from typing import Optional
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
|
| 9 |
@app.post("/predict")
|
| 10 |
async def predict(file: Optional[UploadFile] = File(None), text: Optional[str] = Form(None)):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
# Create Gradio interface
|
| 29 |
def gradio_interface(image, text):
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
|
| 38 |
iface = gr.Interface(
|
|
@@ -51,4 +78,5 @@ app = gr.mount_gradio_app(app, iface, path="/")
|
|
| 51 |
|
| 52 |
# This is needed for Hugging Face Spaces
|
| 53 |
if __name__ == "__main__":
|
|
|
|
| 54 |
iface.launch()
|
|
|
|
| 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
|
| 6 |
+
import sys
|
| 7 |
+
|
| 8 |
+
# Set up logging
|
| 9 |
+
logging.basicConfig(level=logging.INFO, stream=sys.stdout,
|
| 10 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 11 |
+
logger = logging.getLogger(__name__)
|
| 12 |
|
| 13 |
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")
|
| 22 |
+
raise HTTPException(status_code=400,
|
| 23 |
+
detail="No file or text provided. Please upload an image file or provide some text.")
|
| 24 |
+
|
| 25 |
+
if file:
|
| 26 |
+
logger.info(f"File received: {file.filename}")
|
| 27 |
+
# Here you would process the file
|
| 28 |
+
return JSONResponse({
|
| 29 |
+
'message': "File received",
|
| 30 |
+
'filename': file.filename
|
| 31 |
+
})
|
| 32 |
+
else:
|
| 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
|
| 42 |
+
except Exception as e:
|
| 43 |
+
logger.error(f"Unexpected error: {str(e)}")
|
| 44 |
+
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
|
| 45 |
|
| 46 |
|
| 47 |
# Create Gradio interface
|
| 48 |
def gradio_interface(image, text):
|
| 49 |
+
logger.info("Gradio interface called")
|
| 50 |
+
try:
|
| 51 |
+
if image is not None:
|
| 52 |
+
logger.info("Image received in Gradio interface")
|
| 53 |
+
return f"Image received: {type(image)}"
|
| 54 |
+
elif text:
|
| 55 |
+
logger.info(f"Text received in Gradio interface: {text}")
|
| 56 |
+
return f"Text received: {text}"
|
| 57 |
+
else:
|
| 58 |
+
logger.warning("No input provided to Gradio interface")
|
| 59 |
+
return "No input provided"
|
| 60 |
+
except Exception as e:
|
| 61 |
+
logger.error(f"Error in Gradio interface: {str(e)}")
|
| 62 |
+
return f"An error occurred: {str(e)}"
|
| 63 |
|
| 64 |
|
| 65 |
iface = gr.Interface(
|
|
|
|
| 78 |
|
| 79 |
# This is needed for Hugging Face Spaces
|
| 80 |
if __name__ == "__main__":
|
| 81 |
+
logger.info("Starting application")
|
| 82 |
iface.launch()
|