asFrants commited on
Commit
28a5857
1 Parent(s): 957c035

update new pages with verification

Browse files
.gitignore CHANGED
@@ -1,3 +1,4 @@
1
  __pycache__
2
  .venv
 
3
  flagged
 
1
  __pycache__
2
  .venv
3
+ .env
4
  flagged
app.py CHANGED
@@ -46,7 +46,7 @@ DEFAULT_RU_TEXT = """В результате взрыва на заправке,
46
  """
47
 
48
 
49
- class Request(BaseModel):
50
  text: str
51
 
52
 
@@ -121,7 +121,7 @@ class Summarizer:
121
  }
122
  return summary[lang], sentiment[lang]
123
 
124
- def summarize(self, req: Request, lang: str = "en") -> Result:
125
  sum_pipe, sent_pipe = self.get_pipe(lang)
126
  response_summary = sum_pipe(req.text)
127
  logger.info(response_summary)
@@ -134,9 +134,12 @@ class Summarizer:
134
  )
135
  return result
136
 
137
- def summ(self, req: Request, lang: str = "en") -> str:
138
  return self.summarize(req, lang).to_str()
139
 
 
 
 
140
 
141
  if __name__ == "__main__":
142
  pipe = Summarizer()
 
46
  """
47
 
48
 
49
+ class TextRequest(BaseModel):
50
  text: str
51
 
52
 
 
121
  }
122
  return summary[lang], sentiment[lang]
123
 
124
+ def summarize(self, req: TextRequest, lang: str = "en") -> Result:
125
  sum_pipe, sent_pipe = self.get_pipe(lang)
126
  response_summary = sum_pipe(req.text)
127
  logger.info(response_summary)
 
134
  )
135
  return result
136
 
137
+ def summ(self, req: TextRequest, lang: str = "en") -> str:
138
  return self.summarize(req, lang).to_str()
139
 
140
+ def verified_summarize(self, req: TextRequest, lang: str = "en") -> str:
141
+ return "verified"
142
+
143
 
144
  if __name__ == "__main__":
145
  pipe = Summarizer()
main.py CHANGED
@@ -1,6 +1,13 @@
 
1
  import gradio as gr
2
- from fastapi import FastAPI
3
- from app import Summarizer, Request, Result
 
 
 
 
 
 
4
  from app import (
5
  EN_SENTIMENT_MODEL,
6
  EN_SUMMARY_MODEL,
@@ -9,18 +16,58 @@ from app import (
9
  )
10
  from app import DEFAULT_EN_TEXT, DEFAULT_RU_TEXT
11
 
 
 
 
 
 
 
 
12
  app = FastAPI()
13
  pipe = Summarizer()
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  @app.post("/summ_ru", response_model=Result)
17
- async def ru_summ_api(request: Request):
18
  results = pipe.summarize(request.text, lang="ru")
19
  return results
20
 
21
 
22
  @app.post("/summ_en", response_model=Result)
23
- async def en_summ_api(request: Request):
24
  results = pipe.summarize(request.text, lang="en")
25
  return results
26
 
 
1
+ import os
2
  import gradio as gr
3
+ from pathlib import Path
4
+ from fastapi import FastAPI, Request
5
+ from fastapi.staticfiles import StaticFiles
6
+ from fastapi.responses import HTMLResponse
7
+ from fastapi.templating import Jinja2Templates
8
+ from dotenv import load_dotenv
9
+
10
+ from app import Summarizer, TextRequest, Result
11
  from app import (
12
  EN_SENTIMENT_MODEL,
13
  EN_SUMMARY_MODEL,
 
16
  )
17
  from app import DEFAULT_EN_TEXT, DEFAULT_RU_TEXT
18
 
19
+ load_dotenv()
20
+
21
+ SITE_KEY = os.getenv("SITE_KEY")
22
+ SECRET_KEY = os.getenv("SECRET_KEY")
23
+ VERIFY_URL = "https://www.google.com/recaptcha/api/siteverify"
24
+
25
+ # create FastAPI app
26
  app = FastAPI()
27
  pipe = Summarizer()
28
 
29
+ # create a static directory to store the static files
30
+ static_dir = Path("./static")
31
+ static_dir.mkdir(parents=True, exist_ok=True)
32
+
33
+ # mount FastAPI StaticFiles server
34
+ app.mount("/static", StaticFiles(directory=static_dir), name="static")
35
+ templates = Jinja2Templates(directory="templates")
36
+
37
+
38
+ @app.get("/verify_page", response_class=HTMLResponse)
39
+ async def verify_page(request: Request):
40
+ return templates.TemplateResponse(
41
+ request=request, name="verification.html", context={"site_key": SITE_KEY}
42
+ )
43
+
44
+
45
+ @app.get("/bad_request", response_class=HTMLResponse)
46
+ async def bad_request(request: Request):
47
+ return templates.TemplateResponse("bad_request.html", {"request": request})
48
+
49
+
50
+ @app.post("/verify")
51
+ async def verify(request: Request):
52
+ # verify_response = requests.post(
53
+ # url=VERIFY_URL,
54
+ # data={
55
+ # "secret": SECRET_KEY,
56
+ # "response": request.form["g-recaptcha-response"],
57
+ # },
58
+ # )
59
+ # print(verify_response.json())
60
+ return templates.TemplateResponse("bad_request.html", {"request": request})
61
+
62
 
63
  @app.post("/summ_ru", response_model=Result)
64
+ async def ru_summ_api(request: TextRequest):
65
  results = pipe.summarize(request.text, lang="ru")
66
  return results
67
 
68
 
69
  @app.post("/summ_en", response_model=Result)
70
+ async def en_summ_api(request: TextRequest):
71
  results = pipe.summarize(request.text, lang="en")
72
  return results
73
 
poetry.lock CHANGED
@@ -723,14 +723,14 @@ testing = ["pytest (>=6)", "pytest-black (>=0.3.7)", "pytest-checkdocs (>=2.4)",
723
 
724
  [[package]]
725
  name = "jinja2"
726
- version = "3.1.2"
727
  description = "A very fast and expressive template engine."
728
  category = "main"
729
  optional = false
730
  python-versions = ">=3.7"
731
  files = [
732
- {file = "Jinja2-3.1.2-py3-none-any.whl", hash = "sha256:6088930bfe239f0e6710546ab9c19c9ef35e29792895fed6e6e31a023a182a61"},
733
- {file = "Jinja2-3.1.2.tar.gz", hash = "sha256:31351a702a408a9e7595a8fc6150fc3f43bb6bf7e319770cbc0db9df9437e852"},
734
  ]
735
 
736
  [package.dependencies]
@@ -1701,6 +1701,21 @@ files = [
1701
  [package.dependencies]
1702
  six = ">=1.5"
1703
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1704
  [[package]]
1705
  name = "python-multipart"
1706
  version = "0.0.6"
@@ -2835,4 +2850,4 @@ dev = ["black (>=19.3b0)", "pytest (>=4.6.2)"]
2835
  [metadata]
2836
  lock-version = "2.0"
2837
  python-versions = "^3.10"
2838
- content-hash = "5648f003dcb355b02c351874f25f204380d23b153110cec7f6177e81fd260dbf"
 
723
 
724
  [[package]]
725
  name = "jinja2"
726
+ version = "3.1.3"
727
  description = "A very fast and expressive template engine."
728
  category = "main"
729
  optional = false
730
  python-versions = ">=3.7"
731
  files = [
732
+ {file = "Jinja2-3.1.3-py3-none-any.whl", hash = "sha256:7d6d50dd97d52cbc355597bd845fabfbac3f551e1f99619e39a35ce8c370b5fa"},
733
+ {file = "Jinja2-3.1.3.tar.gz", hash = "sha256:ac8bd6544d4bb2c9792bf3a159e80bba8fda7f07e81bc3aed565432d5925ba90"},
734
  ]
735
 
736
  [package.dependencies]
 
1701
  [package.dependencies]
1702
  six = ">=1.5"
1703
 
1704
+ [[package]]
1705
+ name = "python-dotenv"
1706
+ version = "1.0.1"
1707
+ description = "Read key-value pairs from a .env file and set them as environment variables"
1708
+ category = "main"
1709
+ optional = false
1710
+ python-versions = ">=3.8"
1711
+ files = [
1712
+ {file = "python-dotenv-1.0.1.tar.gz", hash = "sha256:e324ee90a023d808f1959c46bcbc04446a10ced277783dc6ee09987c37ec10ca"},
1713
+ {file = "python_dotenv-1.0.1-py3-none-any.whl", hash = "sha256:f7b63ef50f1b690dddf550d03497b66d609393b40b564ed0d674909a68ebf16a"},
1714
+ ]
1715
+
1716
+ [package.extras]
1717
+ cli = ["click (>=5.0)"]
1718
+
1719
  [[package]]
1720
  name = "python-multipart"
1721
  version = "0.0.6"
 
2850
  [metadata]
2851
  lock-version = "2.0"
2852
  python-versions = "^3.10"
2853
+ content-hash = "e9de680b1e2f7d363af6cedea3cbb3873dddd31a084cffe71ff63cb146b7ed22"
pyproject.toml CHANGED
@@ -18,6 +18,8 @@ protobuf = "^4.25.1"
18
  loguru = "^0.7.2"
19
  fastapi = "^0.109.0"
20
  uvicorn = "^0.27.0"
 
 
21
 
22
 
23
  [tool.poetry.group.dev.dependencies]
 
18
  loguru = "^0.7.2"
19
  fastapi = "^0.109.0"
20
  uvicorn = "^0.27.0"
21
+ python-dotenv = "^1.0.1"
22
+ jinja2 = "^3.1.3"
23
 
24
 
25
  [tool.poetry.group.dev.dependencies]
templates/{home.html → bad_request.html} RENAMED
@@ -2,10 +2,11 @@
2
  <html lang="en">
3
  <head>
4
  <meta charset="UTF-8"/>
 
5
  <link rel="stylesheet" href="{{ url_for('static',path='/css/style.css')}}"/>
6
- <title>Home Page</title>
7
  </head>
8
  <body>
9
- <h1>Welcome!</h1>
10
  </body>
11
  </html>
 
2
  <html lang="en">
3
  <head>
4
  <meta charset="UTF-8"/>
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
6
  <link rel="stylesheet" href="{{ url_for('static',path='/css/style.css')}}"/>
7
+ <title>Bad request</title>
8
  </head>
9
  <body>
10
+ <h1>Sorry, your request not allowed</h1>
11
  </body>
12
  </html>
templates/error.html DELETED
@@ -1,11 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8" />
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
- <title>Error</title>
7
- </head>
8
- <body>
9
- <h2>{{error}}</h2>
10
- </body>
11
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
templates/verification.html ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <meta http-equiv="X-UA-Compatible" content="ie=edge">
7
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css"/>
8
+ <link rel="stylesheet" href="{{ url_for('static',path='/css/style.css')}}"/>
9
+ <script async src="https://www.google.com/recaptcha/api.js"></script>
10
+ <script>
11
+ function onSubmit(token) {
12
+ document.getElementById("verify_form").submit();
13
+ }
14
+ </script>
15
+ <script>
16
+ function onClick() {
17
+ grecaptcha.ready(function() {
18
+ grecaptcha.execute('{{ site_key }}', {action: 'submit'}).then(function(token) {
19
+ // Add your logic to submit to your backend server here.
20
+ });
21
+ });
22
+ }
23
+ </script>
24
+ <title>Verification Page</title>
25
+ </head>
26
+ <body>
27
+ <div class="row justify-content-center col-md-4">
28
+ <div class="align-self-center">
29
+ <h1 class="text-primary">Verification Page</h1>
30
+ <form id="verify_form" action="{{ url_for('verify') }}" method="post">
31
+ <div class="form-group my-3">
32
+ <input type="text" class="form-control" name="captcha" placeholder="Type captcha">
33
+ </div>
34
+ <button type="submit" class="btn btn-primary">Submit</button>
35
+
36
+ <!-- reCaptcha v3 -->
37
+ <!-- <button type="submit" class="g-recaptcha btn btn-primary"
38
+ data-sitekey="{{ site_key }}" data-callback="onSubmit"
39
+ data-action="submit">Submit
40
+ </button> -->
41
+ <!-- reCaptcha v2 -->
42
+ <!-- <div class="g-recaptcha" data-sitekey="{{ site_key }}"></div> -->
43
+ <!-- <div id="html_element"></div> -->
44
+ <!-- <button type="submit" class="btn btn-primary" onclick='onClick()'>Submit</button> -->
45
+ </form>
46
+ </div>
47
+ </div>
48
+ <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css"></script>
49
+ </body>
50
+ </html>