Hadiil commited on
Commit
fd0bb9d
·
verified ·
1 Parent(s): 8c99f7f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -85
app.py CHANGED
@@ -1,13 +1,17 @@
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
6
  import logging
7
  from PIL import Image
8
  import io
9
  from docx import Document
10
  import fitz # PyMuPDF
 
 
11
 
12
  # Configure logging
13
  logging.basicConfig(level=logging.INFO)
@@ -18,104 +22,47 @@ app = FastAPI()
18
  # Serve static files (HTML, CSS, JS)
19
  app.mount("/static", StaticFiles(directory="static"), name="static")
20
 
21
- # Load a multimodal model for image captioning and visual question answering
22
  multimodal_pipeline = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
23
-
24
- # Load a text-based model for summarization and text question answering
25
  text_pipeline = pipeline("text2text-generation", model="t5-small")
26
 
 
 
 
 
 
 
27
  @app.get("/")
28
  def read_root():
29
- # Redirect to the static HTML file
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']}")
54
- return {"summary": summary[0]['generated_text']}
55
  except Exception as e:
56
  logger.error(f"Error during summarization: {e}")
57
- raise HTTPException(status_code=500, detail=str(e))
58
-
59
- @app.post("/caption")
60
- async def caption_image(file: UploadFile = File(...)):
61
- logger.info(f"Received image for captioning: {file.filename}")
62
- try:
63
- # Read the image file
64
- image_data = await file.read()
65
- image = Image.open(io.BytesIO(image_data))
66
-
67
- # Use the multimodal pipeline to generate a caption for the image
68
- caption = multimodal_pipeline(image)
69
- logger.info(f"Generated caption: {caption[0]['generated_text']}")
70
- return {"caption": caption[0]['generated_text']}
71
- except Exception as e:
72
- logger.error(f"Error during image captioning: {e}")
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']}")
98
- return {"answer": answer[0]['generated_text']}
99
- except Exception as e:
100
- logger.error(f"Error during question answering: {e}")
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:
108
- # Read the image file
109
- image_data = await file.read()
110
- image = Image.open(io.BytesIO(image_data))
111
-
112
- # Use the multimodal pipeline to answer the question about the image
113
- answer = multimodal_pipeline(image, question=question)
114
- logger.info(f"Generated answer: {answer[0]['generated_text']}")
115
- return {"answer": answer[0]['generated_text']}
116
- except Exception as e:
117
- logger.error(f"Error during visual question answering: {e}")
118
- raise HTTPException(status_code=500, detail=str(e))
119
 
120
  # Helper function to extract text from files
121
  async def extract_text_from_file(file: UploadFile):
 
1
+ from fastapi import FastAPI, UploadFile, File, Form, HTTPException, BackgroundTasks, WebSocket, Request
 
2
  from fastapi.staticfiles import StaticFiles
3
  from fastapi.responses import RedirectResponse
4
+ from fastapi_cache import FastAPICache
5
+ from fastapi_cache.backends.redis import RedisBackend
6
+ from fastapi_cache.decorator import cache
7
  from transformers import pipeline
8
  import logging
9
  from PIL import Image
10
  import io
11
  from docx import Document
12
  import fitz # PyMuPDF
13
+ from pydantic import BaseModel
14
+ import asyncio
15
 
16
  # Configure logging
17
  logging.basicConfig(level=logging.INFO)
 
22
  # Serve static files (HTML, CSS, JS)
23
  app.mount("/static", StaticFiles(directory="static"), name="static")
24
 
25
+ # Load AI models
26
  multimodal_pipeline = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
 
 
27
  text_pipeline = pipeline("text2text-generation", model="t5-small")
28
 
29
+ # Initialize Redis cache
30
+ @app.on_event("startup")
31
+ async def startup():
32
+ FastAPICache.init(RedisBackend("redis://localhost:6379"))
33
+
34
+ # Root endpoint
35
  @app.get("/")
36
  def read_root():
 
37
  return RedirectResponse(url="/static/index.html")
38
 
39
+ # Summarization endpoint
40
  @app.post("/summarize")
41
+ @cache(expire=300)
42
+ async def summarize_text(file: UploadFile = File(None), text: str = Form(None)):
 
 
43
  if file:
44
+ if not file.filename.endswith((".pdf", ".docx")):
45
+ raise HTTPException(status_code=400, detail="Unsupported file format. Please upload a PDF or DOCX file.")
46
+ text = await extract_text_from_file(file)
47
+ elif not text:
 
 
 
 
 
 
48
  raise HTTPException(status_code=400, detail="No file or text provided")
49
 
50
  try:
 
51
  summary = text_pipeline(f"summarize: {text}", max_length=100)
52
+ logger.info(f"Generated summary: {summary[0]['summary_text']}")
53
+ return {"summary": summary[0]['summary_text']}
54
  except Exception as e:
55
  logger.error(f"Error during summarization: {e}")
56
+ raise HTTPException(status_code=500, detail="An error occurred while processing your request. Please try again.")
57
+
58
+ # WebSocket for real-time updates
59
+ @app.websocket("/ws")
60
+ async def websocket_endpoint(websocket: WebSocket):
61
+ await websocket.accept()
62
+ while True:
63
+ data = await websocket.receive_text()
64
+ summary = text_pipeline(f"summarize: {data}", max_length=100)
65
+ await websocket.send_text(summary[0]['summary_text'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  # Helper function to extract text from files
68
  async def extract_text_from_file(file: UploadFile):