Refineverse / Refineverse.py
Nik97's picture
Update Refineverse.py
c17f447
# Plugin will run on this file & Strictly only contains codes for routing between files!
from flask import Flask, render_template, request, flash, g
from TextSummarizationFeature import summarize, getTextSumContents, insertTextSumRow
from BreakdownFeature import breakdown, getBreakdownContents, insertBreakdownRow
from TranslationFeature import translate_text, switch, getTranslatedContents, insertTranslationRow
app = Flask(__name__)
app.secret_key = 'refineverseAdmin' # Used to encrypt cookies & sessions
# Routing to Main Dashboard file
@app.route('/')
def index():
return render_template('RefineverseDashboardUI.html')
# Routing to text summarization file
@app.route('/text_summarization', methods=["POST", "GET"])
def text_summarization():
if request.method == "POST":
try:
# Grab the user story text from the textarea in html form
Entered_story = request.form["input_text"]
# The results are stored into a dictionary variable
summarizedStory = summarize(Entered_story)
flash("Your user story has been summarized!")
# Insert into TextSummarization table in Refineverse.db
insertTextSumRow(Entered_story, summarizedStory)
#Can consider to add View Summarized History feature
# Render and display summarized user story
return render_template('TextSummarizationUI.html', summarizedStory=summarizedStory)
except ValueError as e:
if str(e) == "Empty input!":
flash("The input text cannot be empty! Please enter a user story.", 'error')
return render_template('TextSummarizationUI.html')
elif str(e) == "Incorrect format!":
flash("Incorrect user story format! Please enter in the right format.", 'error')
return render_template('TextSummarizationUI.html')
elif str(e) == "Invalid length!":
flash("Your inputted user story is too short to summarize. Please enter a longer story!", 'error')
return render_template('TextSummarizationUI.html')
else:
flash("An error of type '{}' occurred: {}".format(type(e).__name__, str(e)), 'error')
return render_template('TextSummarizationUI.html')
# Not sure in what use-case will a KeyError be hit, but I'll leave this here for now
except KeyError:
flash("Please enter a valid user story!")
return render_template('TextSummarizationUI.html')
# Catch-all exception handling
except Exception as e:
flash("An error of type '{}' occurred: {}".format(type(e).__name__, str(e)), 'error')
return render_template('TextSummarizationUI.html')
else:
return render_template('TextSummarizationUI.html')
# Routing to Project Task Breakdown file
@app.route("/project_breakdown", methods=["POST", "GET"]) # This tells flask the route to get to the page
def project_breakdown():
if request.method == "POST":
try:
# Grab the user story contents
userStory = request.form["user-story-text"]
# The results are stored into a dictionary variable
processedLabel = breakdown(userStory)
# Display success popup message
flash("Your user story has been allocated as a " + processedLabel + " task!")
insertBreakdownRow(userStory, processedLabel) # Inserts data into the Breakdown table
rows = getBreakdownContents() # Grab all contents inside the Breakdown table
return render_template('ProjectBreakdownUI.html', rows=rows)
# The first matched error will be executed, and all other exception blocks will be skipped.
# Thus, please order exceptions from most specific to most general!
except KeyError:
flash("Please enter a valid user story!", 'error')
rows = getBreakdownContents()
return render_template('ProjectBreakdownUI.html', row=rows)
# Use this to get the error name and error message, then create a dedicated custom error message!
except Exception as e:
flash("An error of type '{}' occurred: {}".format(type(e).__name__, str(e)), 'error')
rows = getBreakdownContents()
return render_template('ProjectBreakdownUI.html', rows=rows)
else: # For "GET" scenarios (the loading page, etc.)
# To always display the table, we must grab the contents of Breakdown every time the page loads
rows = getBreakdownContents()
return render_template('ProjectBreakdownUI.html', rows=rows)
# Routing to Translation file
@app.route('/language_translation', methods=["POST", "GET"])
def language_translation():
if request.method == "POST":
try:
# Grab all relevant information for processing
input_text = request.form['input'] # Grab user text input
# Grab source language code
source_language = request.form['source_language']
# Grab target language code
target_language = request.form['target_language']
# Generate translated text using custom function
translatedStory = translate_text(input_text, source_language, target_language)
# Display success popup message
flash("Your user story has been translated to " + switch(target_language) + " !")
# Insert into Translation table in Refineverse.db
insertTranslationRow(input_text, translatedStory)
# Can consider to create a function to view translated history feature
# Display the page
return render_template('LanguageTranslationUI.html', input_text=input_text, translatedStory=translatedStory)
# Exception handling. Important to note that it is neccesary to still return render_template!
except ValueError as e:
if str(e) == "Empty input!":
flash("The input text cannot be empty! Please enter a user story.", 'error')
return render_template('LanguageTranslationUI.html')
elif str(e) == "Incorrect format!":
flash("Unable to translate your user story. Please enter in the correct format.", 'error')
return render_template('LanguageTranslationUI.html')
else:
flash("An error of type '{}' occurred: {}".format(type(e).__name__, str(e)), 'error')
return render_template('LanguageTranslationUI.html')
except Exception as e:
flash("An error of type '{}' occurred: {}".format(type(e).__name__, str(e)), 'error')
return render_template('LanguageTranslationUI.html')
else:
return render_template('LanguageTranslationUI.html')
# Used when the application is torn down
# Its purpose is to close the database connection if it has not been closed
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close() # Closes the database connection
# Initialise the app
if __name__ == '__main__':
app.run(host="0.0.0.0", port=7860) # can set to True for debugging