-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
51 lines (40 loc) · 1.67 KB
/
app.py
File metadata and controls
51 lines (40 loc) · 1.67 KB
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
"""
This is the main file of the web application.
"""
# Importing the libraries
from flask import Flask, render_template, request
from transformers import T5Tokenizer, T5ForConditionalGeneration
from googletrans import Translator
# Creating an instance of the Flask class
app = Flask(__name__)
# Loading the model and tokenizer
model_name = "t5-small"
model = T5ForConditionalGeneration.from_pretrained(model_name)
tokenizer = T5Tokenizer.from_pretrained(model_name)
translator = Translator()
# Function to summarize the text
def summarize_with_t5(text, language='en'):
if language != 'en':
text = translator.translate(text, dest='en').text
input_text = "summarize: " + text
input_tokenized = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
summary_ids = model.generate(input_tokenized, max_length=100, min_length=5, length_penalty=2.0, num_beams=4, early_stopping=True)
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
if language != 'en':
summary = translator.translate(summary, dest=language).text
return summary
# Defining the routes
@app.route('/')
def index():
return render_template('index.html')
# Defining the routes
@app.route('/summarize', methods=['POST'])
def summarize():
if request.method == 'POST':
user_input = request.form['user_input']
selected_language = request.form['language']
summary = summarize_with_t5(user_input, language=selected_language)
return render_template('result.html', user_input=user_input, summary=summary, selected_language_name=selected_language)
# Running the app
if __name__ == '__main__':
app.run(debug=True)