Spaces:
Sleeping
Sleeping
File size: 7,543 Bytes
910fc51 |
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# 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 |