Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- Dockerfile +13 -0
- app.py +89 -0
- requirements.txt +10 -0
Dockerfile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9
|
| 2 |
+
|
| 3 |
+
RUN useradd -m -u 1000 user
|
| 4 |
+
USER user
|
| 5 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 6 |
+
|
| 7 |
+
WORKDIR /app
|
| 8 |
+
|
| 9 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 10 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 11 |
+
|
| 12 |
+
COPY --chown=user . /app
|
| 13 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import logging
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import io
|
| 7 |
+
from docx import Document
|
| 8 |
+
import fitz # PyMuPDF
|
| 9 |
+
|
| 10 |
+
# Configure logging
|
| 11 |
+
logging.basicConfig(level=logging.INFO)
|
| 12 |
+
logger = logging.getLogger(__name__)
|
| 13 |
+
|
| 14 |
+
app = FastAPI()
|
| 15 |
+
|
| 16 |
+
# Load a multimodal model for image captioning and visual question answering
|
| 17 |
+
multimodal_pipeline = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 18 |
+
|
| 19 |
+
# Load a text-based model for summarization and text question answering
|
| 20 |
+
text_pipeline = pipeline("text2text-generation", model="t5-small")
|
| 21 |
+
|
| 22 |
+
@app.get("/")
|
| 23 |
+
def read_root():
|
| 24 |
+
return {"message": "Welcome to the AI-Powered Web Application!"}
|
| 25 |
+
|
| 26 |
+
@app.post("/analyze")
|
| 27 |
+
async def analyze(file: UploadFile = File(...)):
|
| 28 |
+
logger.info(f"Received file for analysis: {file.filename}")
|
| 29 |
+
try:
|
| 30 |
+
if file.filename.endswith((".pdf", ".docx")):
|
| 31 |
+
# Summarize document
|
| 32 |
+
text = await extract_text_from_file(file)
|
| 33 |
+
summary = text_pipeline(f"summarize: {text}", max_length=100)
|
| 34 |
+
return {"summary": summary[0]['generated_text']}
|
| 35 |
+
elif file.filename.endswith((".jpg", ".jpeg", ".png")):
|
| 36 |
+
# Caption image
|
| 37 |
+
image = Image.open(io.BytesIO(await file.read()))
|
| 38 |
+
caption = multimodal_pipeline(image)
|
| 39 |
+
return {"caption": caption[0]['generated_text']}
|
| 40 |
+
else:
|
| 41 |
+
raise HTTPException(status_code=400, detail="Unsupported file format")
|
| 42 |
+
except Exception as e:
|
| 43 |
+
logger.error(f"Error during analysis: {e}")
|
| 44 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 45 |
+
|
| 46 |
+
@app.post("/ask")
|
| 47 |
+
async def ask(file: UploadFile = File(...), question: str = ""):
|
| 48 |
+
logger.info(f"Received file for question answering: {file.filename}")
|
| 49 |
+
logger.info(f"Received question: {question}")
|
| 50 |
+
try:
|
| 51 |
+
if file.filename.endswith((".pdf", ".docx")):
|
| 52 |
+
# Answer question from document
|
| 53 |
+
text = await extract_text_from_file(file)
|
| 54 |
+
answer = text_pipeline(f"question: {question} context: {text}")
|
| 55 |
+
return {"answer": answer[0]['generated_text']}
|
| 56 |
+
elif file.filename.endswith((".jpg", ".jpeg", ".png")):
|
| 57 |
+
# Answer question about image
|
| 58 |
+
image = Image.open(io.BytesIO(await file.read()))
|
| 59 |
+
answer = multimodal_pipeline(image, question=question)
|
| 60 |
+
return {"answer": answer[0]['generated_text']}
|
| 61 |
+
else:
|
| 62 |
+
raise HTTPException(status_code=400, detail="Unsupported file format")
|
| 63 |
+
except Exception as e:
|
| 64 |
+
logger.error(f"Error during question answering: {e}")
|
| 65 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 66 |
+
|
| 67 |
+
# Helper function to extract text from files
|
| 68 |
+
async def extract_text_from_file(file: UploadFile):
|
| 69 |
+
try:
|
| 70 |
+
if file.filename.endswith(".pdf"):
|
| 71 |
+
doc = fitz.open(stream=await file.read(), filetype="pdf")
|
| 72 |
+
text = ""
|
| 73 |
+
for page in doc:
|
| 74 |
+
text += page.get_text()
|
| 75 |
+
return text
|
| 76 |
+
elif file.filename.endswith(".docx"):
|
| 77 |
+
doc = Document(io.BytesIO(await file.read()))
|
| 78 |
+
text = "\n".join([para.text for para in doc.paragraphs])
|
| 79 |
+
return text
|
| 80 |
+
else:
|
| 81 |
+
raise ValueError("Unsupported file format. Please upload a PDF or DOCX file.")
|
| 82 |
+
except Exception as e:
|
| 83 |
+
logger.error(f"Error extracting text from file: {e}")
|
| 84 |
+
raise HTTPException(status_code=400, detail=str(e))
|
| 85 |
+
|
| 86 |
+
# Hugging Face Spaces expects the app to be served on port 7860
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
import uvicorn
|
| 89 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.74.*
|
| 2 |
+
requests==2.31.*
|
| 3 |
+
uvicorn
|
| 4 |
+
sentencepiece
|
| 5 |
+
torch
|
| 6 |
+
transformers
|
| 7 |
+
pillow
|
| 8 |
+
python-multipart
|
| 9 |
+
pymupdf
|
| 10 |
+
python-docx
|