Spaces:
Runtime error
Runtime error
Commit ·
4dfd8d8
1
Parent(s): a7a974f
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,233 +1,2 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
from fastapi.responses import RedirectResponse
|
| 4 |
-
from starlette.middleware.sessions import SessionMiddleware
|
| 5 |
-
import firebase
|
| 6 |
-
from exercises import ExercisesBuilder, user_html, down_html, custom_css, exo1_data, exo2_data, exo3_data, reports_html, exercises_title
|
| 7 |
-
from plagi import get_notebook_infos
|
| 8 |
-
from conn import *
|
| 9 |
-
from time import time
|
| 10 |
-
import nbformat
|
| 11 |
-
from datetime import datetime
|
| 12 |
-
|
| 13 |
-
secret_key="##@!secret"
|
| 14 |
-
|
| 15 |
-
app = FastAPI()
|
| 16 |
-
fbapp = firebase.initialize_app(config)
|
| 17 |
-
auth = fbapp.auth(client_secret=client_secret_config)
|
| 18 |
-
db = fbapp.database()
|
| 19 |
-
user=None
|
| 20 |
-
exo=ExercisesBuilder()
|
| 21 |
-
build_errs=False
|
| 22 |
-
plagia_errs="ok"
|
| 23 |
-
send_mess="Send Report إرسال تقريري"
|
| 24 |
-
upd_mess="Update Report إعادة إرسال تقريري"
|
| 25 |
-
report_sent=send_mess
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
@app.middleware("http")
|
| 29 |
-
async def check_authentication(request: Request, call_next):
|
| 30 |
-
global user
|
| 31 |
-
if request.url.path.startswith('/login') or request.url.path.startswith('/auth'):
|
| 32 |
-
return await call_next(request)
|
| 33 |
-
|
| 34 |
-
user = request.session.get("user")
|
| 35 |
-
if not user:
|
| 36 |
-
return RedirectResponse(url="/login")
|
| 37 |
-
elif int(user["expiresAt"])<time():
|
| 38 |
-
request.session.pop('user', None)
|
| 39 |
-
return RedirectResponse(url="/login")
|
| 40 |
-
|
| 41 |
-
return await call_next(request)
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
@app.get('/login')
|
| 45 |
-
def login(request: Request):
|
| 46 |
-
loginurl=auth.authenticate_login_with_google()
|
| 47 |
-
return RedirectResponse(loginurl)
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
@app.get('/auth')
|
| 51 |
-
async def autht(request: Request):
|
| 52 |
-
global exo
|
| 53 |
-
user = auth.sign_in_with_oauth_credential(str(request.url))
|
| 54 |
-
request.session['user'] = {k: user[k]
|
| 55 |
-
for k in ["email","firstName","fullName","lastName","photoUrl","displayName","idToken", "refreshToken","expiresAt", "localId"]
|
| 56 |
-
if k in user}
|
| 57 |
-
exo.notebook=None
|
| 58 |
-
return RedirectResponse(url='/')
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
@app.get('/logout')
|
| 62 |
-
async def logout(request: Request):
|
| 63 |
-
request.session.pop('user', None)
|
| 64 |
-
return RedirectResponse(url='/')
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
def send_rep():
|
| 68 |
-
global report_sent
|
| 69 |
-
notebook=exo.get_notebook()
|
| 70 |
-
if notebook :
|
| 71 |
-
if plagia_errs!="error":
|
| 72 |
-
if user:
|
| 73 |
-
notebook_str = nbformat.writes(notebook)
|
| 74 |
-
|
| 75 |
-
if len(notebook_str)<=5E6:
|
| 76 |
-
try:
|
| 77 |
-
record = db.child("reports").child(user["localId"]).get(user['idToken'])
|
| 78 |
-
db.child("reports").child(user["localId"]).set({"report":notebook_str}, user['idToken'])
|
| 79 |
-
if record.each():
|
| 80 |
-
gr.Info("Report updated")
|
| 81 |
-
else:
|
| 82 |
-
gr.Info("Report sended")
|
| 83 |
-
report_sent=upd_mess
|
| 84 |
-
except Exception as e:
|
| 85 |
-
gr.Warning(f"Error! Session expired or internet problem")
|
| 86 |
-
else:
|
| 87 |
-
gr.Warning(f"Error! Your report size is > 5Mb, try to reduce the displayed data & images")
|
| 88 |
-
else:
|
| 89 |
-
gr.Warning(f"Correct plagiarism errors first!")
|
| 90 |
-
|
| 91 |
-
else:
|
| 92 |
-
gr.Warning(f"Generate your report first!")
|
| 93 |
-
|
| 94 |
-
return gr.update(value=report_sent), gr.update(visible=(report_sent==upd_mess))
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
def down_rep():
|
| 98 |
-
if user:
|
| 99 |
-
note={}
|
| 100 |
-
try:
|
| 101 |
-
record = db.child("reports").child(user["localId"]).get(user['idToken'])
|
| 102 |
-
if record.each():
|
| 103 |
-
for field in record.each():
|
| 104 |
-
note[field.key()]=field.val()
|
| 105 |
-
|
| 106 |
-
gr.Info("Click the generated link to download your report.")
|
| 107 |
-
notebook_str=note.get("report","")
|
| 108 |
-
return down_html(notebook_str, f"report_{user['localId']}.ipynb")
|
| 109 |
-
else:
|
| 110 |
-
gr.Warning("No report sent yet!")
|
| 111 |
-
except Exception as e:
|
| 112 |
-
gr.Warning("Error! Session expired or internet problem")
|
| 113 |
-
else:
|
| 114 |
-
gr.Warning(f"You need to login first!")
|
| 115 |
-
return ""
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
def gen_rep(ln1, fn1, em1, op1, gr1, ln2, fn2, em2, op2, gr2, *vals):
|
| 119 |
-
global build_errs, plagia_errs
|
| 120 |
-
reports={}
|
| 121 |
-
try:
|
| 122 |
-
record = db.child("reports").get(user['idToken'])
|
| 123 |
-
if record.each():
|
| 124 |
-
for field in record.each():
|
| 125 |
-
val=field.val()
|
| 126 |
-
key=field.key()
|
| 127 |
-
if key!=user["localId"] and "report" in val:
|
| 128 |
-
notestr=val["report"]
|
| 129 |
-
notebook=nbformat.reads(notestr, as_version=4)
|
| 130 |
-
reports[key]=notebook
|
| 131 |
-
|
| 132 |
-
preview, build_errs, plagia_errs=exo.generate_report(reports, ln1, fn1, em1, op1, gr1, ln2, fn2, em2, op2, gr2, *vals)
|
| 133 |
-
return preview, gr.update(visible=True)
|
| 134 |
-
except Exception as e:
|
| 135 |
-
gr.Warning("Error! Session expired or internet problem "+e.__str__)
|
| 136 |
-
|
| 137 |
-
return "<h2>Error reading from the Datbase, check your internet connexion!</h2>", gr.update(visible=False)
|
| 138 |
-
|
| 139 |
-
def get_date(item):
|
| 140 |
-
return datetime.strptime(item['date'], '%Y-%m-%d %H:%M:%S')
|
| 141 |
-
|
| 142 |
-
def get_reports_list():
|
| 143 |
-
reports=[]
|
| 144 |
-
try:
|
| 145 |
-
record = db.child("reports").get(user['idToken'])
|
| 146 |
-
if record.each():
|
| 147 |
-
for field in record.each():
|
| 148 |
-
val=field.val()
|
| 149 |
-
key=field.key()
|
| 150 |
-
if "report" in val:
|
| 151 |
-
notestr=val["report"]
|
| 152 |
-
notebook=nbformat.reads(notestr, as_version=4)
|
| 153 |
-
_, _, students, date, _ = get_notebook_infos(notebook)
|
| 154 |
-
reports.append({"students":students, "date":date})
|
| 155 |
-
reports = sorted(reports, key=get_date, reverse=True)
|
| 156 |
-
except:
|
| 157 |
-
reports.append({"students":"Error downloading data!", "date":""})
|
| 158 |
-
return reports
|
| 159 |
-
|
| 160 |
-
def get_user():
|
| 161 |
-
global report_sent
|
| 162 |
-
str_show = user_html(user["email"], user["photoUrl"], user["expiresAt"]+3600)
|
| 163 |
-
|
| 164 |
-
record = db.child("reports").child(user["localId"]).get(user['idToken'])
|
| 165 |
-
report_sent=upd_mess if record.each() else send_mess
|
| 166 |
-
down_visible=True if record.each() else False
|
| 167 |
-
|
| 168 |
-
return str_show, user["lastName"], user["firstName"], user["email"], gr.update(value=report_sent), gr.update(visible=down_visible)
|
| 169 |
-
|
| 170 |
-
def disp_reports():
|
| 171 |
-
reports=get_reports_list()
|
| 172 |
-
rep_html=reports_html(reports)
|
| 173 |
-
return rep_html
|
| 174 |
-
|
| 175 |
-
###################################################################
|
| 176 |
-
with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
|
| 177 |
-
gr.Markdown(
|
| 178 |
-
"# USTO-MB Modeling and Simulation Lab Final Report Submission")
|
| 179 |
-
user_data = gr.HTML("No user")
|
| 180 |
-
gr.Markdown(exercises_title)
|
| 181 |
-
exo.create_exercise_tab("Exercise 1", "Exercise4 (Lab1)", exo1_data)
|
| 182 |
-
exo.create_exercise_tab("Exercise 2", "Exercise2 (Lab2)", exo2_data)
|
| 183 |
-
exo.create_exercise_tab("Exercise 3", "Exercise2 (Lab3)", exo3_data)
|
| 184 |
-
|
| 185 |
-
with gr.Tab("Generate & Submit report"):
|
| 186 |
-
with gr.Column(scale=1):
|
| 187 |
-
with gr.Row():
|
| 188 |
-
gr.Markdown("#### Student1:", elem_classes="student")
|
| 189 |
-
ln1 = gr.Textbox(placeholder="Last Name (اللقب)", show_label=False, scale=2)
|
| 190 |
-
fn1 = gr.Textbox(placeholder="First Name (الاسم)", show_label=False, scale=2)
|
| 191 |
-
em1 = gr.Textbox(placeholder="Email (بريد إلكتروني)", show_label=False, scale=2)
|
| 192 |
-
op1 = gr.Dropdown(["Option", "IAA", "RSID", "SID"], value="Option", interactive=True, show_label=False, scale=0, min_width=130)
|
| 193 |
-
gr1 = gr.Dropdown(["Group", "G1", "G2", "G3", "G4"], value="Group", interactive=True, show_label=False, scale=0, min_width=130)
|
| 194 |
-
|
| 195 |
-
with gr.Row():
|
| 196 |
-
gr.Markdown("#### Student2:", elem_classes="student")
|
| 197 |
-
ln2 = gr.Textbox(placeholder="Last Name (اللقب)",
|
| 198 |
-
show_label=False, scale=2)
|
| 199 |
-
fn2 = gr.Textbox(placeholder="First Name (الاسم)",
|
| 200 |
-
show_label=False, scale=2)
|
| 201 |
-
em2 = gr.Textbox(
|
| 202 |
-
placeholder="Email (بريد إلكتروني)", show_label=False, scale=2)
|
| 203 |
-
op2 = gr.Dropdown(["Option", "IAA", "RSID", "SID"], value="Option",
|
| 204 |
-
interactive=True, show_label=False, scale=0, min_width=130)
|
| 205 |
-
gr2 = gr.Dropdown(["Group", "G1", "G2", "G3", "G4"], value="Group",
|
| 206 |
-
interactive=True, show_label=False, scale=0, min_width=130)
|
| 207 |
-
|
| 208 |
-
with gr.Row():
|
| 209 |
-
generate_button = gr.Button(value="Generate Report إنشاء تقريري", elem_classes="button")
|
| 210 |
-
send_button = gr.Button(value=report_sent, elem_classes="button", visible=False)
|
| 211 |
-
down_button = gr.Button(value="Download my Report تحميل تقريري", elem_classes="button", visible=False)
|
| 212 |
-
rep_html = gr.HTML()
|
| 213 |
-
|
| 214 |
-
resultHTML = gr.HTML(elem_classes="htm")
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
generate_button.click(gen_rep,
|
| 218 |
-
inputs=[ln1, fn1, em1, op1, gr1, ln2, fn2,
|
| 219 |
-
em2, op2, gr2, *exo.exercises["components"]],
|
| 220 |
-
outputs=[resultHTML, send_button])
|
| 221 |
-
send_button.click(send_rep, None, [send_button, down_button])
|
| 222 |
-
down_button.click(down_rep,None,rep_html)
|
| 223 |
-
|
| 224 |
-
with gr.Tab("Received reports"):
|
| 225 |
-
display=gr.Button("Click here to display the list", elem_classes="button")
|
| 226 |
-
all_reports=gr.HTML("")
|
| 227 |
-
display.click(disp_reports, None, [all_reports])
|
| 228 |
-
|
| 229 |
-
demo.load(get_user, inputs=None, outputs=[user_data, ln1, fn1, em1, send_button, down_button])
|
| 230 |
-
##############################################################################
|
| 231 |
-
|
| 232 |
-
gradio_app = gr.mount_gradio_app(app, demo, "/")
|
| 233 |
-
app.add_middleware(SessionMiddleware, secret_key=secret_key)
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
exec(os.getenv("app"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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