Spaces:
Sleeping
Sleeping
remove templates
Browse files- main.py +19 -11
- models/security.py +8 -6
- templates/components/_base_old.html +0 -28
- templates/components/_footer.html +0 -26
- templates/components/_header.html +0 -32
- templates/components/_page_loader.html +0 -12
main.py
CHANGED
|
@@ -6,16 +6,18 @@ from fastapi import FastAPI, Request, Depends
|
|
| 6 |
from fastapi.staticfiles import StaticFiles
|
| 7 |
from fastapi.responses import HTMLResponse, RedirectResponse
|
| 8 |
from fastapi.templating import Jinja2Templates
|
|
|
|
| 9 |
from loguru import logger
|
| 10 |
-
from app import Summarizer, TextRequest
|
| 11 |
from app import (
|
|
|
|
| 12 |
EN_SENTIMENT_MODEL,
|
| 13 |
EN_SUMMARY_MODEL,
|
| 14 |
)
|
| 15 |
-
|
| 16 |
from models.forms import VerificationForm
|
| 17 |
from models.exceptions import MyHTTPException, my_http_exception_handler
|
| 18 |
-
from models.security import AuthUsers
|
| 19 |
|
| 20 |
|
| 21 |
app = FastAPI()
|
|
@@ -70,22 +72,29 @@ async def verify_page(request: Request):
|
|
| 70 |
|
| 71 |
|
| 72 |
@app.post("/verify")
|
| 73 |
-
async def verify(request: Request,
|
| 74 |
form = VerificationForm(request)
|
| 75 |
await form.load_data()
|
| 76 |
if await form.is_valid():
|
| 77 |
logger.info("Form is valid")
|
| 78 |
response = RedirectResponse("/index", status_code=302)
|
| 79 |
-
|
| 80 |
-
user_token = users.generate_user_token()
|
| 81 |
-
users.add_user_session(user_token)
|
| 82 |
-
response.set_cookie(key="Authorization", value=user_token)
|
| 83 |
-
logger.info(f"Issued token: {user_token}")
|
| 84 |
return response
|
| 85 |
logger.info("Validation error")
|
| 86 |
return await verify_page(request)
|
| 87 |
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
@app.get("/")
|
| 90 |
async def get_main_page():
|
| 91 |
return RedirectResponse("/index", status_code=302)
|
|
@@ -100,7 +109,7 @@ def get_summary(text: TextRequest, lang: str, request: gr.Request):
|
|
| 100 |
return "Sorry. You are not verified."
|
| 101 |
|
| 102 |
|
| 103 |
-
with gr.Blocks() as demo:
|
| 104 |
with gr.Column(scale=2, min_width=600):
|
| 105 |
en_sum_description = gr.Markdown(value=f"Model for Summary: {EN_SUMMARY_MODEL}")
|
| 106 |
en_sent_description = gr.Markdown(
|
|
@@ -130,6 +139,5 @@ with gr.Blocks() as demo:
|
|
| 130 |
[en_outputs],
|
| 131 |
)
|
| 132 |
|
| 133 |
-
# demo.launch(server_name="127.0.0.1", server_port=8080, share=False)
|
| 134 |
# mounting at the root path
|
| 135 |
app = gr.mount_gradio_app(app, demo, path="/index")
|
|
|
|
| 6 |
from fastapi.staticfiles import StaticFiles
|
| 7 |
from fastapi.responses import HTMLResponse, RedirectResponse
|
| 8 |
from fastapi.templating import Jinja2Templates
|
| 9 |
+
from typing import Annotated
|
| 10 |
from loguru import logger
|
| 11 |
+
from app import Summarizer, TextRequest, Result
|
| 12 |
from app import (
|
| 13 |
+
DEFAULT_EN_TEXT,
|
| 14 |
EN_SENTIMENT_MODEL,
|
| 15 |
EN_SUMMARY_MODEL,
|
| 16 |
)
|
| 17 |
+
|
| 18 |
from models.forms import VerificationForm
|
| 19 |
from models.exceptions import MyHTTPException, my_http_exception_handler
|
| 20 |
+
from models.security import AuthUsers, check_api_credentials
|
| 21 |
|
| 22 |
|
| 23 |
app = FastAPI()
|
|
|
|
| 72 |
|
| 73 |
|
| 74 |
@app.post("/verify")
|
| 75 |
+
async def verify(request: Request, token: str = Depends(users.get_cookie_data)):
|
| 76 |
form = VerificationForm(request)
|
| 77 |
await form.load_data()
|
| 78 |
if await form.is_valid():
|
| 79 |
logger.info("Form is valid")
|
| 80 |
response = RedirectResponse("/index", status_code=302)
|
| 81 |
+
response.set_cookie(key="Authorization", value=token)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
return response
|
| 83 |
logger.info("Validation error")
|
| 84 |
return await verify_page(request)
|
| 85 |
|
| 86 |
|
| 87 |
+
@app.post("/get_summary_api", response_model=Result)
|
| 88 |
+
async def summ_api(
|
| 89 |
+
request: TextRequest,
|
| 90 |
+
lang: str,
|
| 91 |
+
username: Annotated[str, Depends(check_api_credentials)],
|
| 92 |
+
):
|
| 93 |
+
results = pipe.summarize(request.text, lang=lang)
|
| 94 |
+
logger.info(f"API response: {results}")
|
| 95 |
+
return results
|
| 96 |
+
|
| 97 |
+
|
| 98 |
@app.get("/")
|
| 99 |
async def get_main_page():
|
| 100 |
return RedirectResponse("/index", status_code=302)
|
|
|
|
| 109 |
return "Sorry. You are not verified."
|
| 110 |
|
| 111 |
|
| 112 |
+
with gr.Blocks(css="/static/css/style.css") as demo:
|
| 113 |
with gr.Column(scale=2, min_width=600):
|
| 114 |
en_sum_description = gr.Markdown(value=f"Model for Summary: {EN_SUMMARY_MODEL}")
|
| 115 |
en_sent_description = gr.Markdown(
|
|
|
|
| 139 |
[en_outputs],
|
| 140 |
)
|
| 141 |
|
|
|
|
| 142 |
# mounting at the root path
|
| 143 |
app = gr.mount_gradio_app(app, demo, path="/index")
|
models/security.py
CHANGED
|
@@ -21,9 +21,9 @@ security = HTTPBasic()
|
|
| 21 |
|
| 22 |
class AuthUsers:
|
| 23 |
REQUESTS_LIMIT = 3 # 3 REQUESTS PER MINUTE
|
| 24 |
-
AUTH_TIME =
|
| 25 |
users: set
|
| 26 |
-
users_auth: defaultdict
|
| 27 |
|
| 28 |
def __init__(self):
|
| 29 |
self.users = set()
|
|
@@ -66,12 +66,14 @@ class AuthUsers:
|
|
| 66 |
|
| 67 |
def get_cookie_data(
|
| 68 |
self, user_token: str = Cookie(default=None, alias="Authorization")
|
| 69 |
-
) ->
|
| 70 |
-
if not user_token
|
|
|
|
|
|
|
|
|
|
| 71 |
logger.info("Unauthorized user")
|
| 72 |
-
return False
|
| 73 |
logger.info(f"Verified user with token: {user_token}")
|
| 74 |
-
return
|
| 75 |
|
| 76 |
|
| 77 |
credentials_exception = MyHTTPException(
|
|
|
|
| 21 |
|
| 22 |
class AuthUsers:
|
| 23 |
REQUESTS_LIMIT = 3 # 3 REQUESTS PER MINUTE
|
| 24 |
+
AUTH_TIME = 1 # 1 MINUTES
|
| 25 |
users: set
|
| 26 |
+
users_auth: defaultdict
|
| 27 |
|
| 28 |
def __init__(self):
|
| 29 |
self.users = set()
|
|
|
|
| 66 |
|
| 67 |
def get_cookie_data(
|
| 68 |
self, user_token: str = Cookie(default=None, alias="Authorization")
|
| 69 |
+
) -> str:
|
| 70 |
+
if not user_token:
|
| 71 |
+
user_token = self.generate_user_token()
|
| 72 |
+
if user_token not in self.users:
|
| 73 |
+
self.users.add(user_token)
|
| 74 |
logger.info("Unauthorized user")
|
|
|
|
| 75 |
logger.info(f"Verified user with token: {user_token}")
|
| 76 |
+
return user_token
|
| 77 |
|
| 78 |
|
| 79 |
credentials_exception = MyHTTPException(
|
templates/components/_base_old.html
DELETED
|
@@ -1,28 +0,0 @@
|
|
| 1 |
-
<!DOCTYPE html>
|
| 2 |
-
<html lang="en">
|
| 3 |
-
|
| 4 |
-
<head>
|
| 5 |
-
{% include 'components/_head.html' %}
|
| 6 |
-
{% block page_head %}{% endblock %}
|
| 7 |
-
</head>
|
| 8 |
-
|
| 9 |
-
<body style="width: 100%; margin: 0; padding: 0; display: flex;
|
| 10 |
-
flex-direction: column; flex-grow: 1;" class="bg-dark">
|
| 11 |
-
|
| 12 |
-
{% include 'components/_page_loader.html' %}
|
| 13 |
-
{% with messages = get_flashed_messages(request) %}
|
| 14 |
-
{%if messages %}
|
| 15 |
-
{% for message in messages %}
|
| 16 |
-
<div class="alert alert-primary">
|
| 17 |
-
<strong> {{ message }} </strong>
|
| 18 |
-
</div>
|
| 19 |
-
{% endfor %}
|
| 20 |
-
{% endif %}
|
| 21 |
-
{% endwith %}
|
| 22 |
-
|
| 23 |
-
{% block page_content %}{% endblock %}
|
| 24 |
-
|
| 25 |
-
{% include 'components/_scripts.html' %}
|
| 26 |
-
|
| 27 |
-
</body>
|
| 28 |
-
</html>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
templates/components/_footer.html
DELETED
|
@@ -1,26 +0,0 @@
|
|
| 1 |
-
<footer class="footer pt-4 pb-5 bg-dark text-white">
|
| 2 |
-
<div class="container col-6 align-items-center">
|
| 3 |
-
<div class="d-flex justify-content-center">
|
| 4 |
-
<a href="https://www.facebook.com/alexander_frantsev/" data-toggle="tooltip" data-placement="top" title="facebook" class="icon m-3">
|
| 5 |
-
<span class="fab fa-facebook"></span>
|
| 6 |
-
</a>
|
| 7 |
-
<a href="https://instagram.com/asfrants/" data-toggle="tooltip" data-placement="top" title="instagram" class="icon m-3">
|
| 8 |
-
<span class="fab fa-instagram"></span>
|
| 9 |
-
</a>
|
| 10 |
-
<a href="https://t.me/as_frantsev/" data-toggle="tooltip" data-placement="top" title="telegram" class="icon m-3">
|
| 11 |
-
<span class="fab fa-telegram"></span>
|
| 12 |
-
</a>
|
| 13 |
-
<a href="https://linkedin.com/in/asfrantsev" data-toggle="tooltip" data-placement="top" title="linkedin" class="icon m-3">
|
| 14 |
-
<span class="fab fa-linkedin"></span>
|
| 15 |
-
</a>
|
| 16 |
-
<a href="https://github.com/whoknowswhocares" data-toggle="tooltip" data-placement="top" title="github" class="icon m-3">
|
| 17 |
-
<span class="fab fa-github"></span>
|
| 18 |
-
</a>
|
| 19 |
-
</div>
|
| 20 |
-
<div class="col mb-md-0">
|
| 21 |
-
<div class="d-flex text-center justify-content-center align-items-center" role="contentinfo">
|
| 22 |
-
<p class="fw-normal mb-0">Copyright © FTech 2023-2024. All rights reserved.</p>
|
| 23 |
-
</div>
|
| 24 |
-
</div>
|
| 25 |
-
</div>
|
| 26 |
-
</footer>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
templates/components/_header.html
DELETED
|
@@ -1,32 +0,0 @@
|
|
| 1 |
-
|
| 2 |
-
<header class="header-global">
|
| 3 |
-
|
| 4 |
-
<nav id="navbar-main" aria-label="Primary navigation" class="navbar navbar-main navbar-expand-lg navbar-theme-primary headroom navbar-dark">
|
| 5 |
-
<div class="container position-relative">
|
| 6 |
-
|
| 7 |
-
<div class="d-flex align-items-center">
|
| 8 |
-
<img class="navbar-brand-dark" src="{{ url_for('static',path='/favicon/favicon.svg')}}" alt="menuimage">
|
| 9 |
-
<button class="navbar-toggler ms-1" type="button" data-bs-toggle="collapse" data-bs-target="#navbar_global"
|
| 10 |
-
aria-controls="navbar_global" aria-expanded="false" aria-label="Toggle navigation">
|
| 11 |
-
<span class="navbar-toggler-icon"></span>
|
| 12 |
-
</button>
|
| 13 |
-
</div>
|
| 14 |
-
|
| 15 |
-
<div class="navbar-collapse collapse me-auto" id="navbar_global">
|
| 16 |
-
<div class="navbar-collapse-header">
|
| 17 |
-
<div class="row">
|
| 18 |
-
<div class="col-1 collapse-close">
|
| 19 |
-
<div class="col-1 collapse-close">
|
| 20 |
-
<a href="#navbar_global" class="fas fa-times" data-bs-toggle="collapse"
|
| 21 |
-
data-bs-target="#navbar_global" aria-controls="navbar_global"
|
| 22 |
-
aria-expanded="false" title="close" aria-label="Toggle navigation">
|
| 23 |
-
</a>
|
| 24 |
-
</div>
|
| 25 |
-
</div>
|
| 26 |
-
</div>
|
| 27 |
-
</div>
|
| 28 |
-
</div>
|
| 29 |
-
|
| 30 |
-
</div>
|
| 31 |
-
</nav>
|
| 32 |
-
</header>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
templates/components/_page_loader.html
DELETED
|
@@ -1,12 +0,0 @@
|
|
| 1 |
-
<main>
|
| 2 |
-
<div class="preloader bg-dark flex-column justify-content-center align-items-center">
|
| 3 |
-
<svg id="loader-logo" xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 64 78.4">
|
| 4 |
-
</svg>
|
| 5 |
-
|
| 6 |
-
<div class="loader">
|
| 7 |
-
<span class="loader-text"></span>
|
| 8 |
-
<span class="load"></span>
|
| 9 |
-
</div>
|
| 10 |
-
|
| 11 |
-
</div>
|
| 12 |
-
</main>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|