Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
-
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
from fastapi.responses import RedirectResponse
|
| 5 |
from transformers import pipeline
|
|
@@ -30,12 +30,24 @@ def read_root():
|
|
| 30 |
return RedirectResponse(url="/static/index.html")
|
| 31 |
|
| 32 |
@app.post("/summarize")
|
| 33 |
-
async def summarize_text(
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
try:
|
| 36 |
-
# Extract text from the document
|
| 37 |
-
text = await extract_text_from_file(file)
|
| 38 |
-
|
| 39 |
# Use the text pipeline to summarize the text
|
| 40 |
summary = text_pipeline(f"summarize: {text}", max_length=100)
|
| 41 |
logger.info(f"Generated summary: {summary[0]['generated_text']}")
|
|
@@ -61,13 +73,25 @@ async def caption_image(file: UploadFile = File(...)):
|
|
| 61 |
raise HTTPException(status_code=500, detail=str(e))
|
| 62 |
|
| 63 |
@app.post("/answer")
|
| 64 |
-
async def answer_question(
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
try:
|
| 68 |
-
# Extract text from the document
|
| 69 |
-
text = await extract_text_from_file(file)
|
| 70 |
-
|
| 71 |
# Use the text pipeline to answer the question
|
| 72 |
answer = text_pipeline(f"question: {question} context: {text}")
|
| 73 |
logger.info(f"Generated answer: {answer[0]['generated_text']}")
|
|
@@ -77,7 +101,7 @@ async def answer_question(file: UploadFile = File(...), question: str = ""):
|
|
| 77 |
raise HTTPException(status_code=500, detail=str(e))
|
| 78 |
|
| 79 |
@app.post("/vqa")
|
| 80 |
-
async def visual_question_answering(file: UploadFile = File(...), question: str =
|
| 81 |
logger.info(f"Received image for visual question answering: {file.filename}")
|
| 82 |
logger.info(f"Received question: {question}")
|
| 83 |
try:
|
|
|
|
| 1 |
import os
|
| 2 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException, Form
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
from fastapi.responses import RedirectResponse
|
| 5 |
from transformers import pipeline
|
|
|
|
| 30 |
return RedirectResponse(url="/static/index.html")
|
| 31 |
|
| 32 |
@app.post("/summarize")
|
| 33 |
+
async def summarize_text(
|
| 34 |
+
file: UploadFile = File(None), # Optional file upload
|
| 35 |
+
text: str = Form(None) # Optional manual text input
|
| 36 |
+
):
|
| 37 |
+
if file:
|
| 38 |
+
logger.info(f"Received document for summarization: {file.filename}")
|
| 39 |
+
try:
|
| 40 |
+
# Extract text from the document
|
| 41 |
+
text = await extract_text_from_file(file)
|
| 42 |
+
except Exception as e:
|
| 43 |
+
logger.error(f"Error extracting text from file: {e}")
|
| 44 |
+
raise HTTPException(status_code=400, detail=str(e))
|
| 45 |
+
elif text:
|
| 46 |
+
logger.info("Received manual text for summarization")
|
| 47 |
+
else:
|
| 48 |
+
raise HTTPException(status_code=400, detail="No file or text provided")
|
| 49 |
+
|
| 50 |
try:
|
|
|
|
|
|
|
|
|
|
| 51 |
# Use the text pipeline to summarize the text
|
| 52 |
summary = text_pipeline(f"summarize: {text}", max_length=100)
|
| 53 |
logger.info(f"Generated summary: {summary[0]['generated_text']}")
|
|
|
|
| 73 |
raise HTTPException(status_code=500, detail=str(e))
|
| 74 |
|
| 75 |
@app.post("/answer")
|
| 76 |
+
async def answer_question(
|
| 77 |
+
file: UploadFile = File(None), # Optional file upload
|
| 78 |
+
text: str = Form(None), # Optional manual text input
|
| 79 |
+
question: str = Form(...) # Required question
|
| 80 |
+
):
|
| 81 |
+
if file:
|
| 82 |
+
logger.info(f"Received document for question answering: {file.filename}")
|
| 83 |
+
try:
|
| 84 |
+
# Extract text from the document
|
| 85 |
+
text = await extract_text_from_file(file)
|
| 86 |
+
except Exception as e:
|
| 87 |
+
logger.error(f"Error extracting text from file: {e}")
|
| 88 |
+
raise HTTPException(status_code=400, detail=str(e))
|
| 89 |
+
elif text:
|
| 90 |
+
logger.info("Received manual text for question answering")
|
| 91 |
+
else:
|
| 92 |
+
raise HTTPException(status_code=400, detail="No file or text provided")
|
| 93 |
+
|
| 94 |
try:
|
|
|
|
|
|
|
|
|
|
| 95 |
# Use the text pipeline to answer the question
|
| 96 |
answer = text_pipeline(f"question: {question} context: {text}")
|
| 97 |
logger.info(f"Generated answer: {answer[0]['generated_text']}")
|
|
|
|
| 101 |
raise HTTPException(status_code=500, detail=str(e))
|
| 102 |
|
| 103 |
@app.post("/vqa")
|
| 104 |
+
async def visual_question_answering(file: UploadFile = File(...), question: str = Form(...)):
|
| 105 |
logger.info(f"Received image for visual question answering: {file.filename}")
|
| 106 |
logger.info(f"Received question: {question}")
|
| 107 |
try:
|