-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
168 lines (134 loc) · 6.79 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
from fastapi import FastAPI, Form, Request, status, File, UploadFile
from fastapi.responses import HTMLResponse, FileResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
import uvicorn
import re
from services.language import Language
from services.storage import Storage
from dotenv import load_dotenv
from services.vision import Vision
load_dotenv()
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
@app.get("/", response_class=HTMLResponse)
async def index(request: Request):
print('Request for index page received')
return templates.TemplateResponse('index.html', {"request": request})
@app.get('/favicon.ico')
async def favicon():
file_name = 'favicon.ico'
file_path = './static/' + file_name
return FileResponse(path=file_path, headers={'mimetype': 'image/vnd.microsoft.icon'})
def split_paragraph_into_sentences(paragraph):
# Use regular expression to split by sentence-ending punctuation followed by a space or end of string
sentences = re.split(r'(?<=[.!?]) +', paragraph)
return sentences
# image_analysis
@app.get("/image_analysis", response_class=HTMLResponse)
async def image_analysis_get(request: Request):
print('Request for image_analysis page received')
context = {"request": request, "original_post": "", "summerization": "" }
return templates.TemplateResponse('image_analysis.html', context)
@app.post("/image_analysis", response_class=HTMLResponse)
async def image_analysis( request: Request, name: UploadFile = File(...)):
storage = Storage()
storage_values = storage.upload_file(name)
# sas_token = storage.generate_sas_token(name.filename)
public_url = storage.get_public_url(name.filename)
image_url = public_url
vision = Vision()
summary = vision.image_analysis_from_url(image_url)
context = {"request": request, "original_post": image_url, "summerization": summary }
return templates.TemplateResponse('image_analysis.html', context)
# image_analysis OCR
@app.get("/image_analysis_OCR", response_class=HTMLResponse)
async def image_analysis_OCR_get(request: Request):
print('Request for image_analysis page received')
context = {"request": request, "original_post": "", "summerization": "" }
return templates.TemplateResponse('image_analysis_ocr.html', context)
@app.post("/image_analysis_OCR", response_class=HTMLResponse)
async def image_analysis_OCR( request: Request, name: UploadFile = File(...)):
storage = Storage()
storage_values = storage.upload_file(name)
# sas_token = storage.generate_sas_token(name.filename)
public_url = storage.get_public_url(name.filename)
image_url = public_url
vision = Vision()
summary = vision.image_analysis_OCR_from_url(image_url)
context = {"request": request, "original_post": image_url, "summerization": summary }
return templates.TemplateResponse('image_analysis_ocr.html', context)
#summerization
@app.get("/summerization", response_class=HTMLResponse)
async def summerization_get(request: Request):
print('Request for summerization page received')
context = {"request": request}
return templates.TemplateResponse('summerization.html', context)
@app.post("/summerization", response_class=HTMLResponse)
async def summerization(request: Request, name: str = Form(...)):
if name:
block = []
block.append(name)
lang = Language()
summary = lang.extractive_summarization(block)
context = {"request": request, "original_post": name, "summerization": summary }
return templates.TemplateResponse('summerization.html', context)
else:
print('Request for summerization page received with no value submitted -- redirecting')
return RedirectResponse(request.url_for("index"), status_code=status.HTTP_302_FOUND)
# sentiment_analyze
@app.get("/sentiment_analyze", response_class=HTMLResponse)
async def sentiment_analyze_get(request: Request):
print('Request for sentiment_analyze page received')
context = {"request": request}
return templates.TemplateResponse('sentiment_analyze.html', context)
@app.post("/sentiment_analyze", response_class=HTMLResponse)
async def sentiment_analyze(request: Request, name: str = Form(...)):
if name:
split = split_paragraph_into_sentences(name)
lang = Language()
summary = lang.sentiment_analysis_with_opinion_mining(split)
context = {"request": request, "original_post": name, "summerization": summary }
return templates.TemplateResponse('sentiment_analyze.html', context)
else:
print('Request for sentiment_analyze page received with no value submitted -- redirecting')
return RedirectResponse(request.url_for("index"), status_code=status.HTTP_302_FOUND)
#entity_recognition
@app.get("/entity_recognition", response_class=HTMLResponse)
async def entity_recognition_get(request: Request):
print('Request for entity_recognition page received')
context = {"request": request}
return templates.TemplateResponse('entity_recognition.html', context)
@app.post("/entity_recognition", response_class=HTMLResponse)
async def entity_recognition(request: Request, name: str = Form(...)):
summary = ""
if name:
split = split_paragraph_into_sentences(name)
langage = Language()
results = langage.entity_recognition(split)
context = {"request": request, "original_post": name, "summerization": results }
return templates.TemplateResponse('entity_recognition.html', context)
else:
print('Request for entity_recognition page received with no value submitted -- redirecting')
return RedirectResponse(request.url_for("index"), status_code=status.HTTP_302_FOUND)
#entity linking
@app.get("/extract_key_phrases", response_class=HTMLResponse)
async def extract_key_phrases_get(request: Request):
print('Request for extract_key_phrases page received')
context = {"request": request}
return templates.TemplateResponse('extract_key_phrases.html', context)
@app.post("/extract_key_phrases", response_class=HTMLResponse)
async def extract_key_phrases(request: Request, name: str = Form(...)):
summary = ""
if name:
split = split_paragraph_into_sentences(name)
langage = Language()
results = langage.extract_key_phrases(split)
context = {"request": request, "original_post": name, "summerization": results }
return templates.TemplateResponse('extract_key_phrases.html', context)
else:
print('Request for extract_key_phrases page received with no value submitted -- redirecting')
return RedirectResponse(request.url_for("index"), status_code=status.HTTP_302_FOUND)
if __name__ == '__main__':
uvicorn.run('main:app', host='0.0.0.0', port=8000)