Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 2 |
from PIL import Image
|
| 3 |
import gradio as gr
|
|
@@ -17,6 +20,9 @@ import concurrent.futures
|
|
| 17 |
# Load environment variables from .env file
|
| 18 |
load_dotenv()
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
# Salesforce credentials
|
| 21 |
SF_USERNAME = os.getenv('SF_USERNAME')
|
| 22 |
SF_PASSWORD = os.getenv('SF_PASSWORD')
|
|
@@ -36,6 +42,22 @@ model.eval()
|
|
| 36 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 37 |
model.to(device)
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
# Inference function to generate captions dynamically based on image content
|
| 40 |
def generate_captions_from_image(image):
|
| 41 |
if image.mode != "RGB":
|
|
@@ -267,4 +289,6 @@ iface = gr.Interface(
|
|
| 267 |
)
|
| 268 |
|
| 269 |
if __name__ == "__main__":
|
| 270 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 5 |
from PIL import Image
|
| 6 |
import gradio as gr
|
|
|
|
| 20 |
# Load environment variables from .env file
|
| 21 |
load_dotenv()
|
| 22 |
|
| 23 |
+
app = FastAPI()
|
| 24 |
+
|
| 25 |
+
|
| 26 |
# Salesforce credentials
|
| 27 |
SF_USERNAME = os.getenv('SF_USERNAME')
|
| 28 |
SF_PASSWORD = os.getenv('SF_PASSWORD')
|
|
|
|
| 42 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 43 |
model.to(device)
|
| 44 |
|
| 45 |
+
|
| 46 |
+
# FastAPI endpoint to handle image upload and caption generation
|
| 47 |
+
@app.post("/predict/")
|
| 48 |
+
async def predict(image: UploadFile = File(...)):
|
| 49 |
+
try:
|
| 50 |
+
# Read the image from the request
|
| 51 |
+
image_bytes = await image.read()
|
| 52 |
+
image = Image.open(BytesIO(image_bytes))
|
| 53 |
+
|
| 54 |
+
# Generate caption from the image
|
| 55 |
+
caption = generate_captions_from_image(image)
|
| 56 |
+
return {"caption": caption}
|
| 57 |
+
except Exception as e:
|
| 58 |
+
return {"error": str(e)}
|
| 59 |
+
|
| 60 |
+
|
| 61 |
# Inference function to generate captions dynamically based on image content
|
| 62 |
def generate_captions_from_image(image):
|
| 63 |
if image.mode != "RGB":
|
|
|
|
| 289 |
)
|
| 290 |
|
| 291 |
if __name__ == "__main__":
|
| 292 |
+
import uvicorn
|
| 293 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
| 294 |
+
|