Spaces:
Runtime error
Runtime error
Kashif Ahmed commited on
Commit ·
794edb5
1
Parent(s): 09bfa96
11
Browse files
app.py
CHANGED
|
@@ -1,32 +1,49 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from fastapi import FastAPI, File, UploadFile
|
| 3 |
from fastapi.responses import JSONResponse
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
|
| 8 |
@app.post("/predict")
|
| 9 |
-
async def predict(file: UploadFile = File(None)):
|
| 10 |
-
if file is None:
|
| 11 |
-
return JSONResponse({
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
# Create Gradio interface
|
| 20 |
-
def gradio_interface(image):
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
iface = gr.Interface(
|
| 25 |
fn=gradio_interface,
|
| 26 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
| 27 |
outputs="text",
|
| 28 |
title="PoultryShieldAI",
|
| 29 |
-
description="Upload an image to predict poultry diseases."
|
| 30 |
)
|
| 31 |
|
| 32 |
# Mount FastAPI app to Gradio
|
|
|
|
| 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 |
+
if file is None and text is None:
|
| 12 |
+
return JSONResponse({
|
| 13 |
+
"error": "No file or text provided. Please upload an image file or provide some text."
|
| 14 |
+
}, status_code=400)
|
| 15 |
+
|
| 16 |
+
if file:
|
| 17 |
+
return JSONResponse({
|
| 18 |
+
'message': "File received",
|
| 19 |
+
'filename': file.filename
|
| 20 |
+
})
|
| 21 |
+
else:
|
| 22 |
+
return JSONResponse({
|
| 23 |
+
'message': "Text received",
|
| 24 |
+
'text': text
|
| 25 |
+
})
|
| 26 |
|
| 27 |
|
| 28 |
# Create Gradio interface
|
| 29 |
+
def gradio_interface(image, text):
|
| 30 |
+
if image is not None:
|
| 31 |
+
return f"Image received: {type(image)}"
|
| 32 |
+
elif text:
|
| 33 |
+
return f"Text received: {text}"
|
| 34 |
+
else:
|
| 35 |
+
return "No input provided"
|
| 36 |
|
| 37 |
|
| 38 |
iface = gr.Interface(
|
| 39 |
fn=gradio_interface,
|
| 40 |
+
inputs=[
|
| 41 |
+
gr.Image(type="pil", label="Upload an image"),
|
| 42 |
+
gr.Textbox(label="Or enter some text")
|
| 43 |
+
],
|
| 44 |
outputs="text",
|
| 45 |
title="PoultryShieldAI",
|
| 46 |
+
description="Upload an image or enter text to predict poultry diseases."
|
| 47 |
)
|
| 48 |
|
| 49 |
# Mount FastAPI app to Gradio
|