Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- Dockerfile +21 -0
- app.py +33 -0
- requirements.txt +5 -0
Dockerfile
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use an official Python runtime as a parent image
|
| 2 |
+
FROM python:3.9-slim
|
| 3 |
+
|
| 4 |
+
# Set the working directory in the container
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy the current directory contents into the container at /app
|
| 8 |
+
COPY . /app
|
| 9 |
+
|
| 10 |
+
# Install any needed packages specified in requirements.txt
|
| 11 |
+
RUN pip install --upgrade pip
|
| 12 |
+
RUN pip install -r requirements.txt
|
| 13 |
+
|
| 14 |
+
# Make port 80 available to the world outside this container
|
| 15 |
+
EXPOSE 80
|
| 16 |
+
|
| 17 |
+
# Define environment variable
|
| 18 |
+
ENV HF_TOKEN ${HF_TOKEN}
|
| 19 |
+
|
| 20 |
+
# Run app.py when the container launches
|
| 21 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "80"]
|
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import uvicorn
|
| 3 |
+
from fastapi import FastAPI
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
import logging
|
| 6 |
+
|
| 7 |
+
# Configure logging
|
| 8 |
+
logging.basicConfig(level=logging.INFO)
|
| 9 |
+
logger = logging.getLogger(__name__)
|
| 10 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
# Create a pipeline object for summarization
|
| 14 |
+
pipe = pipeline("summarization", model="sysresearch101/t5-large-finetuned-xsum-cnn")
|
| 15 |
+
|
| 16 |
+
@app.get("/")
|
| 17 |
+
def read_root():
|
| 18 |
+
return {"message": "Welcome to the Summarization API!"}
|
| 19 |
+
|
| 20 |
+
@app.get("/summarize")
|
| 21 |
+
def summarize_text(text: str):
|
| 22 |
+
logger.info(f"Received text for summarization: {text}")
|
| 23 |
+
try:
|
| 24 |
+
# Use the pipeline to summarize the input text
|
| 25 |
+
summary = pipe(text, max_length=100, num_beams=4, early_stopping=True)
|
| 26 |
+
logger.info(f"Generated summary: {summary[0]['summary_text']}")
|
| 27 |
+
return {"summary": summary[0]['summary_text']}
|
| 28 |
+
except Exception as e:
|
| 29 |
+
logger.error(f"Error during summarization: {e}")
|
| 30 |
+
return {"error": str(e)}
|
| 31 |
+
|
| 32 |
+
if __name__ == "__main__":
|
| 33 |
+
port = int(os.environ.get("PORT", 7860))
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.74.*
|
| 2 |
+
transformers==4.21.3
|
| 3 |
+
torch==1.12.1
|
| 4 |
+
uvicorn==0.18.3
|
| 5 |
+
accelerate>=0.26.0
|