bambadij commited on
Commit
49fa9a8
·
1 Parent(s): 193ecd5

Add application file

Browse files
Files changed (3) hide show
  1. Dockerfile +12 -0
  2. app.py +97 -0
  3. requirements.txt +11 -0
Dockerfile ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /app
4
+
5
+ COPY . /app/
6
+
7
+ RUN pip install --no-cache-dir -r requirements.txt
8
+
9
+ RUN pip install fastapi uvicorn
10
+
11
+ EXPOSE 8080
12
+ CMD ["uvicorn", "app:app", "--reload", "--host", "0.0.0.0", "--port", "8080"]
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #load package
2
+ from transformers import pipeline
3
+ from fastapi import FastAPI,HTTPException,status,UploadFile,File
4
+ from pydantic import BaseModel
5
+ import uvicorn
6
+ import logging
7
+ from PIL import Image
8
+ import pytesseract
9
+ from io import BytesIO
10
+
11
+ #Additional information
12
+
13
+ Informations = """
14
+ -text : Texte à resumé
15
+
16
+ output:
17
+ - Text summary : texte resumé
18
+ """
19
+
20
+
21
+
22
+ app =FastAPI(
23
+ title='Text Summary',
24
+ description =Informations
25
+ )
26
+
27
+ #class to define the input text
28
+ logging.basicConfig(level=logging.INFO)
29
+ logger =logging.getLogger(__name__)
30
+ summarize =pipeline('summarization', model="facebook/bart-large-cnn")
31
+ generated_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large")
32
+ translation_pieline =pipeline("translation_en_to_fr",model="Helsinki-NLP/opus-mt-en-fr")
33
+ classify_zero_shot = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
34
+
35
+ class TextSummary(BaseModel):
36
+ text:str
37
+
38
+ #ENDPOINT
39
+
40
+ @app.get("/")
41
+ async def home():
42
+ return 'STN BIG DATA'
43
+
44
+
45
+ @app.post("/summary")
46
+ async def summary_text_bart(input:TextSummary):
47
+ "add text to summarize"
48
+ try:
49
+ summary = summarize(input.text,do_sample=False)
50
+ summary_text =summary[0].get('summary_text')
51
+ result = classify_zero_shot(
52
+ summary_text,
53
+ candidate_labels=["En Cours", "Non traiter", "Terminer"],
54
+ hypothesis_template="Cet Resumé est sur {}."
55
+ )
56
+ print(f"[iNFO] Input data as text:\n{summary_text}")
57
+ logger.info(f"[INFO] input data:{input.text}")
58
+ logger.info(f"[INFO] Summary:{summary}")
59
+ formatted_result = [
60
+ f"{label}: {score:.2f}" for label, score in zip(result['labels'], result['scores']*100)
61
+ ]
62
+ return {
63
+ "summary_text" :summary_text,
64
+ "Statut":formatted_result,
65
+ # "format":formatted_result,
66
+ "len_input" : len(input.text),
67
+ "len_output" :len(summary_text)
68
+
69
+ }
70
+
71
+
72
+ except ValueError as e:
73
+ logger.error(f"valueError:{e}")
74
+ return {"error ER":str(e)}
75
+
76
+ except Exception as e:
77
+ raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,detail="Could not summarize the input text.")
78
+
79
+ @app.post("/upload-image/")
80
+ async def upload_image(image: UploadFile = File(...)):
81
+ contents = await image.read()
82
+ try:
83
+ image_open = Image.open(BytesIO(contents))
84
+ raw_text = pytesseract.image_to_string(image_open,lang='fra')
85
+ summary = summarize(raw_text,do_sample=False)
86
+ summary_text =summary[0].get('summary_text')
87
+ return {
88
+ # "text": raw_text,
89
+ "summary":summary_text}
90
+ except Exception as e:
91
+ return {"error": str(e)}
92
+ if __name__ == "__main__":
93
+ uvicorn.run("main:app",host="0.0.0.0",port=8000,reload=True)
94
+
95
+
96
+
97
+
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ fastapi==0.111.0
2
+ torch==2.3.1
3
+ transformers==4.41.2
4
+ uvicorn==0.30.1
5
+ pydantic==2.7.4
6
+ pillow==10.3.0
7
+ numpy==1.23.5
8
+ scipy==1.11.3
9
+ sentencepiece==0.2.0
10
+ pytesseract==0.3.10
11
+ Pillow==8.0.0