File size: 2,661 Bytes
09cf842
 
 
 
 
 
 
 
 
 
d2febe5
 
09cf842
d2febe5
 
09cf842
d2febe5
09cf842
d2febe5
 
09cf842
 
 
 
d2febe5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09cf842
 
d2febe5
 
09cf842
 
d2febe5
 
09cf842
 
d2febe5
09cf842
d2febe5
 
09cf842
d2febe5
 
09cf842
d2febe5
 
 
09cf842
d2febe5
 
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
from flask import Flask, render_template, request, send_file
import os
from ruaccent import RUAccent
import text_split

app = Flask(__name__)

ru_accent = RUAccent()
ru_accent.load()


@app.route("/")
def index():
    return render_template("index.html")


@app.route("/process", methods=["POST"])
def process():
    if request.method == "POST":
        input_text = request.form["input_text"]
        processed_text = ru_accent.process_all(input_text)

        # Create three text files with the same content

        file_name = "accented_text.txt"
        with open(file_name, "w", encoding="utf-8") as file:
            file.write(" ".join(processed_text[0]))

        file_name = "omographs.txt"
        with open(file_name, "w", encoding="utf-8") as file:
            file.write("\n".join(processed_text[1]))

        file_name = "unknown.txt"
        with open(file_name, "w", encoding="utf-8") as file:
            file.write("\n".join(processed_text[2]))

        return render_template("result.html")


@app.route("/upload", methods=["POST"])
def upload():
    # Check if the POST request has a file part
    if "file" not in request.files:
        return "No file part"

    file = request.files["file"]

    # If the user submits an empty form
    if file.filename == "":
        return "No selected file"

    # Check if the file is a text file
    if file and file.filename.endswith(".txt"):
        # Save the uploaded file to the server (you might want to store it in a more secure way)
        file.save(file.filename)

        # Process the file content (replace this with your actual processing logic)
        with open(file.filename, "r", encoding="utf-8") as f:
            content = f.read()

        processed_text = ru_accent.process_all(content)

        # Create three text files with the same content

        file_name = "accented_text.txt"
        with open(file_name, "w", encoding="utf-8") as file:
            file.write(" ".join(processed_text[0]))

        file_name = "omographs.txt"
        with open(file_name, "w", encoding="utf-8") as file:
            file.write("\n".join(processed_text[1]))

        file_name = "unknown.txt"
        with open(file_name, "w", encoding="utf-8") as file:
            file.write("\n".join(processed_text[2]))

        return render_template("result.html")

    else:
        return "Invalid file format. Please upload a text file."


@app.route("/download/<file_name>")
def download(file_name):
    file_name = f"{file_name}"
    return send_file(file_name, as_attachment=True, download_name=f"{file_name}")


if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0", port=7860)