Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,97 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
from fastapi import FastAPI, UploadFile, File
|
3 |
-
from transformers import pipeline
|
4 |
-
import logging
|
5 |
-
from PIL import Image
|
6 |
-
import io
|
7 |
-
|
8 |
-
# Configure logging
|
9 |
-
logging.basicConfig(level=logging.INFO)
|
10 |
-
logger = logging.getLogger(__name__)
|
11 |
-
|
12 |
-
app = FastAPI()
|
13 |
-
|
14 |
-
# Create a pipeline object for summarization
|
15 |
-
summarizer = pipeline("summarization", model="sysresearch101/t5-large-finetuned-xsum-cnn")
|
16 |
-
|
17 |
-
# Create a pipeline object for image captioning
|
18 |
-
image_captioner = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
19 |
-
|
20 |
-
# Create a pipeline object for text question answering
|
21 |
-
qa_pipeline = pipeline("question-answering", model="deepset/roberta-base-squad2")
|
22 |
-
|
23 |
-
# Create a pipeline object for visual question answering
|
24 |
-
vqa_pipeline = pipeline("visual-question-answering", model="dandelin/vilt-b32-finetuned-vqa")
|
25 |
-
|
26 |
-
@app.get("/")
|
27 |
-
def read_root():
|
28 |
-
return {"message": "Welcome to the Summarization, Image Captioning, Question Answering, and Visual Question Answering API!"}
|
29 |
-
|
30 |
-
@app.get("/summarize")
|
31 |
-
def summarize_text(text: str):
|
32 |
-
logger.info(f"Received text for summarization: {text}")
|
33 |
-
try:
|
34 |
-
# Use the pipeline to summarize the input text
|
35 |
-
summary = summarizer(text, max_length=100, num_beams=4, early_stopping=True)
|
36 |
-
logger.info(f"Generated summary: {summary[0]['summary_text']}")
|
37 |
-
return {"summary": summary[0]['summary_text']}
|
38 |
-
except Exception as e:
|
39 |
-
logger.error(f"Error during summarization: {e}")
|
40 |
-
return {"error": str(e)}
|
41 |
-
|
42 |
-
@app.post("/caption")
|
43 |
-
async def caption_image(file: UploadFile = File(...)):
|
44 |
-
logger.info(f"Received image for captioning: {file.filename}")
|
45 |
-
try:
|
46 |
-
# Read the image file
|
47 |
-
image_data = await file.read()
|
48 |
-
image = Image.open(io.BytesIO(image_data))
|
49 |
-
|
50 |
-
# Use the pipeline to generate a caption for the image
|
51 |
-
caption = image_captioner(image)
|
52 |
-
logger.info(f"Generated caption: {caption[0]['generated_text']}")
|
53 |
-
return {"caption": caption[0]['generated_text']}
|
54 |
-
except Exception as e:
|
55 |
-
logger.error(f"Error during image captioning: {e}")
|
56 |
-
return {"error": str(e)}
|
57 |
-
|
58 |
-
@app.get("/answer")
|
59 |
-
def answer_question(question: str, context: str):
|
60 |
-
logger.info(f"Received question: {question}")
|
61 |
-
logger.info(f"Received context: {context}")
|
62 |
-
try:
|
63 |
-
# Use the pipeline to answer the question based on the context
|
64 |
-
result = qa_pipeline(question=question, context=context)
|
65 |
-
logger.info(f"Generated answer: {result['answer']}")
|
66 |
-
return {"answer": result['answer']}
|
67 |
-
except Exception as e:
|
68 |
-
logger.error(f"Error during question answering: {e}")
|
69 |
-
return {"error": str(e)}
|
70 |
-
|
71 |
-
@app.post("/vqa")
|
72 |
-
async def visual_question_answering(file: UploadFile = File(...), question: str = ""):
|
73 |
-
logger.info(f"Received image for visual question answering: {file.filename}")
|
74 |
-
logger.info(f"Received question: {question}")
|
75 |
-
try:
|
76 |
-
# Read the image file
|
77 |
-
image_data = await file.read()
|
78 |
-
image = Image.open(io.BytesIO(image_data))
|
79 |
-
|
80 |
-
# Use the pipeline to answer the question about the image
|
81 |
-
result = vqa_pipeline(image=image, question=question)
|
82 |
-
|
83 |
-
# Check if the result is a list and has at least one element
|
84 |
-
if isinstance(result, list) and len(result) > 0:
|
85 |
-
logger.info(f"Generated answer: {result[0]['answer']}")
|
86 |
-
return {"answer": result[0]['answer']}
|
87 |
-
else:
|
88 |
-
logger.error("No answer found in the result.")
|
89 |
-
return {"error": "No answer found in the result."}
|
90 |
-
except Exception as e:
|
91 |
-
logger.error(f"Error during visual question answering: {e}")
|
92 |
-
return {"error": str(e)}
|
93 |
-
|
94 |
-
# Hugging Face Spaces expects the app to be served on port 7860
|
95 |
-
if __name__ == "__main__":
|
96 |
-
import uvicorn
|
97 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|