Spaces:
Runtime error
Runtime error
mametchi
commited on
Commit
·
ed18de2
1
Parent(s):
a24bdae
Moving files from a local PyCharm project
Browse files- app.py +23 -0
- requirements.txt +6 -0
- static/dino1.svg +11 -0
- static/dino2.svg +15 -0
- static/puhekupla.svg +16 -0
- static/style.css +0 -0
- templates/index.html +135 -0
app.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request
|
2 |
+
import random
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
pipe = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fi")
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
+
|
9 |
+
@app.route('/')
|
10 |
+
def home():
|
11 |
+
return render_template('index.html')
|
12 |
+
|
13 |
+
|
14 |
+
@app.route('/submit', methods=['POST'])
|
15 |
+
def submit():
|
16 |
+
translation_request = request.form["translation_request" ]
|
17 |
+
|
18 |
+
random_number = random.randint(0,100)
|
19 |
+
|
20 |
+
return f'{translation_request}. Ja satunnainen luku: {random_number}'
|
21 |
+
|
22 |
+
if __name__ == '__main__':
|
23 |
+
app.run(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi==0.74.*
|
2 |
+
requests==2.27.*
|
3 |
+
sentencepiece==0.1.*
|
4 |
+
torch==1.11.*
|
5 |
+
transformers==4.*
|
6 |
+
uvicorn[standard]==0.17.*
|
static/dino1.svg
ADDED
|
static/dino2.svg
ADDED
|
static/puhekupla.svg
ADDED
|
static/style.css
ADDED
File without changes
|
templates/index.html
ADDED
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<html>
|
2 |
+
<head>
|
3 |
+
<style>
|
4 |
+
html, body {
|
5 |
+
height: 100%;
|
6 |
+
margin: 0;
|
7 |
+
}
|
8 |
+
|
9 |
+
|
10 |
+
#bottom_filler {
|
11 |
+
height: 1.5em;
|
12 |
+
margin-bottom: 1em;
|
13 |
+
}
|
14 |
+
|
15 |
+
#message-box, #result-box {
|
16 |
+
|
17 |
+
display: flex;
|
18 |
+
justify-content: space-between;
|
19 |
+
align-items: center;
|
20 |
+
flex-direction: column;
|
21 |
+
border: 2px solid #000;
|
22 |
+
border-radius: 10px;
|
23 |
+
}
|
24 |
+
|
25 |
+
#message, #result {
|
26 |
+
width: 100%;
|
27 |
+
height: 50%;
|
28 |
+
max-height: 50%;
|
29 |
+
max-width: 100%;
|
30 |
+
min-height: 50%;
|
31 |
+
min-width: 100%;
|
32 |
+
padding: 1em;
|
33 |
+
border: none;
|
34 |
+
box-sizing: border-box;
|
35 |
+
overflow-y: scroll;
|
36 |
+
white-space: normal;
|
37 |
+
overflow-wrap: break-word;
|
38 |
+
display: inline-block;
|
39 |
+
font-family: Arial, sans-serif;
|
40 |
+
font-size: 1em;
|
41 |
+
resize: none;
|
42 |
+
}
|
43 |
+
|
44 |
+
#message:focus {
|
45 |
+
outline: none !important;
|
46 |
+
border: none;
|
47 |
+
box-shadow: none;
|
48 |
+
}
|
49 |
+
|
50 |
+
#message::-webkit-resizer {
|
51 |
+
display: none;
|
52 |
+
}
|
53 |
+
|
54 |
+
body {
|
55 |
+
display: flex;
|
56 |
+
justify-content: center;
|
57 |
+
align-items: center;
|
58 |
+
}
|
59 |
+
|
60 |
+
.container {
|
61 |
+
flex-direction: row;
|
62 |
+
display: flex;
|
63 |
+
justify-content: center;
|
64 |
+
align-items: center;
|
65 |
+
width: 70%;
|
66 |
+
height: 40%;
|
67 |
+
box-sizing: border-box;
|
68 |
+
margin-top: -10%;
|
69 |
+
}
|
70 |
+
|
71 |
+
.image {
|
72 |
+
max-width: 15%;
|
73 |
+
}
|
74 |
+
|
75 |
+
.child {
|
76 |
+
max-height: 600px;
|
77 |
+
width: 45%;
|
78 |
+
height: 100%;
|
79 |
+
flex-shrink: 0;
|
80 |
+
box-sizing: border-box;
|
81 |
+
|
82 |
+
margin: 0 2.5em; /* Add margin here */
|
83 |
+
|
84 |
+
}
|
85 |
+
</style>
|
86 |
+
<title>Flask Form Submission without Page Reload</title>
|
87 |
+
<link rel="stylesheet" type="text/css" href="static/style.css">
|
88 |
+
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
|
89 |
+
<script>
|
90 |
+
$(document).ready(function () {
|
91 |
+
|
92 |
+
$("#message").keypress(function (e) {
|
93 |
+
if (e.which === 13 && !e.shiftKey) {
|
94 |
+
e.preventDefault();
|
95 |
+
$(this).closest("form").submit();
|
96 |
+
}
|
97 |
+
});
|
98 |
+
|
99 |
+
$('#message-box').on('submit', function (e) {
|
100 |
+
e.preventDefault(); // prevent the form from submitting
|
101 |
+
|
102 |
+
// get the form data
|
103 |
+
var formData = {
|
104 |
+
translation_request: $('#message').val()
|
105 |
+
};
|
106 |
+
|
107 |
+
// send the POST request using AJAX
|
108 |
+
$.ajax({
|
109 |
+
type: 'POST',
|
110 |
+
url: '/submit',
|
111 |
+
data: formData,
|
112 |
+
success: function (response) {
|
113 |
+
$('#result').text(response); // display the response in the result div
|
114 |
+
}
|
115 |
+
});
|
116 |
+
});
|
117 |
+
});
|
118 |
+
</script>
|
119 |
+
</head>
|
120 |
+
<body>
|
121 |
+
<div class="container">
|
122 |
+
<form id="message-box" class="child">
|
123 |
+
<span id="suomi_label">Suomeksi</span>
|
124 |
+
<textarea id="message" name="message" autofocus="autofocus" spellcheck="false"></textarea>
|
125 |
+
<button type="submit" id="bottom_filler">Submit</button>
|
126 |
+
</form>
|
127 |
+
<img src="{{ url_for('static', filename='dino1.svg') }}" class="child image" alt="dino_image">
|
128 |
+
<div id="result-box" class="child">
|
129 |
+
<span id="enkku_label">Englanniksi:</span>
|
130 |
+
<div id="result"></div>
|
131 |
+
<span id="bottom_filler"></span>
|
132 |
+
</div>
|
133 |
+
</div>
|
134 |
+
</body>
|
135 |
+
</html>
|