Spaces:
Running
Running
File size: 2,786 Bytes
97308e2 7ab594e 97308e2 7ab594e 97308e2 7ab594e c6714eb d98a217 f584171 97308e2 f584171 0a8d481 97308e2 f584171 97308e2 7ab594e c6714eb 7ab594e f584171 d98a217 c6714eb 17962cc d98a217 c6714eb 17962cc c6714eb d98a217 |
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 |
from fastapi import FastAPI, Request
from pydantic import BaseModel
from jinja2 import TemplateNotFound
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from youtube import get_youtube_caption
from inference import predict_emotions, predict_summarization
MAX_ITER_SIZE = 3000
app = FastAPI()
app.mount("/files", StaticFiles(directory="files"), name="files")
templates = Jinja2Templates(directory="static")
@app.exception_handler(TemplateNotFound)
async def not_found_exception_handler(request: Request, exc: TemplateNotFound):
return templates.TemplateResponse("404.html", {"request": request},
status_code=404)
@app.get("/", response_class=HTMLResponse)
async def read_homepage(request: Request):
return templates.TemplateResponse(f"index.html", {"request": request,
"page": "index"})
@app.get("/{page}", response_class=HTMLResponse)
async def read_html(request: Request, page: str = 'index'):
if page.endswith(".html"):
page = page[:-5]
return templates.TemplateResponse(f"{page}.html", {"request": request,
"page": page})
class EmotionRequest(BaseModel):
sum_type: str
text: str
@app.post('/predict_emotion')
async def predict_emo(request: EmotionRequest):
if request.sum_type == 'sum-video':
text = get_youtube_caption(request.text)
if not text:
return 'Invalid Link'
elif text == 'err':
return 'Something goes wrong...'
elif text == 'no-cap':
return "Unfortunately, this youtube video doesn't contain captions"
else:
text = request.text
return predict_emotions(text)
@app.post('/predict_summarization')
async def predict_sum(request: EmotionRequest):
if request.sum_type == 'sum-video':
text = get_youtube_caption(request.text)
if not text:
return 'Invalid Link'
elif text == 'err':
return 'Something goes wrong...'
elif text == 'no-cap':
return "Unfortunately, this youtube video doesn't contain captions"
else:
text = request.text
try:
if len(text) < MAX_ITER_SIZE:
return predict_summarization(text)
arr = []
for i in range(min(len(text), 20_000) // MAX_ITER_SIZE):
res = predict_summarization(
text[MAX_ITER_SIZE*i:MAX_ITER_SIZE*(i+1)]).replace('\n', ' ')
res = f'{res[0].upper()}{res[1:]}'
arr.append(res)
return '\n\n'.join(arr)
except Exception as e:
print('ERR:', e)
return 'Something goes wrong...'
|