Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,36 +1,33 @@
|
|
| 1 |
-
from fastapi import FastAPI, UploadFile, File
|
| 2 |
-
from fastapi.staticfiles import StaticFiles
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
#
|
| 17 |
-
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
content = parser.from_buffer(await file.read())["content"]
|
| 27 |
-
|
| 28 |
-
summary
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
caption = image_captioner(await file.read())
|
| 35 |
-
return {"caption": caption[0]["generated_text"]}
|
| 36 |
-
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from fastapi.staticfiles import StaticFiles
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
from tika import parser
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
# Mount static folder for frontend
|
| 10 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 11 |
+
|
| 12 |
+
# Define a writable cache directory inside the container
|
| 13 |
+
CACHE_DIR = "/app/cache"
|
| 14 |
+
os.makedirs(CACHE_DIR, exist_ok=True) # Create the directory if it doesn’t exist
|
| 15 |
+
|
| 16 |
+
# Load models with custom cache directory
|
| 17 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn", cache_dir=CACHE_DIR)
|
| 18 |
+
image_captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base", cache_dir=CACHE_DIR)
|
| 19 |
+
|
| 20 |
+
@app.get("/")
|
| 21 |
+
async def root():
|
| 22 |
+
return {"message": "Welcome to AI Web App"}
|
| 23 |
+
|
| 24 |
+
@app.post("/summarize")
|
| 25 |
+
async def summarize_document(file: UploadFile = File(...)):
|
| 26 |
+
content = parser.from_buffer(await file.read())["content"]
|
| 27 |
+
summary = summarizer(content, max_length=130, min_length=30, do_sample=False)
|
| 28 |
+
return {"summary": summary[0]["summary_text"]}
|
| 29 |
+
|
| 30 |
+
@app.post("/caption")
|
| 31 |
+
async def caption_image(file: UploadFile = File(...)):
|
| 32 |
+
caption = image_captioner(await file.read())
|
| 33 |
+
return {"caption": caption[0]["generated_text"]}
|
|
|
|
|
|
|
|
|