Spaces:
Running
Running
File size: 17,555 Bytes
dacef8c |
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 |
from starlette.responses import PlainTextResponse, JSONResponse
from starlette.applications import Starlette
from starlette.routing import Route
from starlette.middleware import Middleware
from starlette.middleware.cors import CORSMiddleware
"""Prompt templates for LLM"""
from env import LLM_API_KEY
import prompt
from time import time
from re import split, match
from PIL import Image
import requests
import json
import pypandoc
import cv2
from io import BytesIO
import numpy as np
import os
import pytesseract
import lang
from secrets import SystemRandom
from random import randint, sample
from enum import Enum
from re import sub
from flashcard_util import tldr, get_definitions_from_words, fetch_img_for_words
class QType(Enum):
WH = 0
STMT = 3
FILL = 6
routes = ...
temp_files = {}
middleware = [
Middleware(
CORSMiddleware,
allow_origins=['http://localhost:8100'],
allow_methods =['*'],
),
]
sys_random = SystemRandom()
# TODO: Change to environment variable in prod.
#pytesseract.pytesseract.tesseract_cmd = r"C:\Users\Admin\AppData\Local\Programs\Tesseract-OCR\tesseract.exe"
async def __mltest(request):
pass
async def __save_temp(request):
file_id = sys_random.randbytes(16).hex()
content = ""
# async with request.form(max_fields=3) as form:
form = await request.json()
content = form['content']
title = form['title']
keywords = form['keywords']
temp_files[file_id] = [title, content, keywords]
print(file_id)
return PlainTextResponse(file_id, 200)
async def __get_temp(request, entry = 1):
return JSONResponse(temp_files.get(request.path_params['id'], [None, None, None]))
async def __remove_temp(request):
try:
del temp_files[request.path_params['id']]
except:
return PlainTextResponse("", 500)
return PlainTextResponse("", 200)
def __convert2md(inp):
# Use gfm-raw_html to strip styling data from source file
return pypandoc.convert_text(inp, "gfm-raw_html", "html")
def __convert2plain(inp):
return pypandoc.convert_text(inp, "plain", "html")
def convert2md(req):
pass
async def __parse_paragraphs (content: str, batching: bool = False):
_p = ""
_rp = content
_rp = __convert2md(_rp).replace('\r','')
# remove empty lines and headers
_p = [_x.strip() for _x in _rp.split('\n\n') if len(_x)!=0 and _x.strip().count('#') != len(_x)]
_p_json = []
h_cnt =0
header=""
for _n in _p:
__h_cnt =0
prev_h = ""
# parse header for each paragraphs
try:
for _c in _n:
if _c == '#': __h_cnt+=1
else: break
if (__h_cnt >= 1 and len(_n) > __h_cnt):
header=_n
h_cnt = __h_cnt
# print(_n, len(_n))
elif (len(_n.replace('#',''))):
# remove accidental /n's in converted HTML content
if (batching and len(_p_json) >= 1):
if (header == _p_json[-1]['header']):
# print(header)
_p_json[-1]['content'] += '\n'
_p_json[-1]['content'] += _n.replace('\n', ' ')
_p_json[-1]['count']+=1
continue
_p_json.append({'header': header, 'h_cnt': h_cnt, 'content': _n.replace('\n',' '), 'count': 1})
except:
continue
return _p_json
async def __query_ml_predict(qtype:QType, content: str, header: str, token_limit: int, num_qs=5, l=lang.VI_VN):
"""Get prediction from a third-party Llama3-8B-Instruct deployment"""
stopwatch = time()
match qtype:
case QType.WH:
# Make request to Awan LLM endpoint
_r = requests.post(
url="https://api.awanllm.com/v1/chat/completions",
headers={'Content-Type': 'application/json', 'Authorization': f'Bearer {LLM_API_KEY}'},
data=json.dumps({
"model": "Meta-Llama-3-8B-Instruct",
"messages": [
{"role": "user", "content": prompt.gen_prompt_wh(content=content, header=header, num_qs=num_qs, lang=l)}
],
"max_tokens": max(token_limit, 4096),
"presence_penalty":0.3,
"temperature":0.55
})
)
print(time() - stopwatch)
return {"content": _r.json()['choices'][0]['message']['content'], "style": QType.WH}
case QType.STMT:
# Make request to Awan LLM endpoint
_r = requests.post(
url="https://api.awanllm.com/v1/chat/completions",
headers={'Content-Type': 'application/json', 'Authorization': f'Bearer {LLM_API_KEY}'},
data=json.dumps({
"model": "Meta-Llama-3-8B-Instruct",
"messages": [
{"role": "user", "content": prompt.gen_prompt_statements(content=content, header=header, num_qs=num_qs, lang=l)}
],
"max_tokens": max(token_limit, 4096),
})
)
_r_content = _r.json()['choices'][0]['message']['content'].split('\n\n',1)[1]
_w = requests.post(
url="https://api.awanllm.com/v1/chat/completions",
headers={'Content-Type': 'application/json', 'Authorization': f'Bearer {LLM_API_KEY}'},
data=json.dumps({
"model": "Meta-Llama-3-8B-Instruct",
"messages": [
{"role": "user", "content": prompt.gen_prompt_statements_false(content=_r_content, lang=l)}
],
"max_tokens": max(token_limit, 4096),
})
)
_w_content = _w.json()['choices'][0]['message']['content'].split('\n\n',1)[1]
print(time() - stopwatch)
return {"content": f"{_r_content}\n{_w_content}", "style": QType.STMT}
async def parse_wh_question(raw_qa_list, pgph_i):
__ANS_KEY_MAPPING = {'A': 1, 'B':2, 'C':3,'D':4}
__parsed_outputs = []
for x in raw_qa_list:
try:
segments = [r for r in x.split('\n') if r.__len__()]
raw_key = segments[5].strip()
raw_key = 'A' if 'A' in raw_key else 'B' if 'B' in raw_key else 'D' if 'D' in raw_key else 'C'
except:
print("invalid: ", x)
continue
# print(segments)
match randint(0, 3):
case 0 | 1:
__parsed_outputs.append(
{
"pgph_i": pgph_i,
"prompt": segments[0],
"type": "MCQ",
"choices": segments[1:5],
"keys": [segments[__ANS_KEY_MAPPING[raw_key]],],
}
)
case 2 | 3:
__parsed_outputs.append(
{
"pgph_i": pgph_i,
"prompt": segments[0],
"type": "OPEN",
# Cleaning up ML output
"keys": [segments[__ANS_KEY_MAPPING[raw_key]].split(' ',1)[1]],
"choices": [segments[__ANS_KEY_MAPPING[raw_key]]]
}
)
return __parsed_outputs
async def parse_stmt_question(stmts: list[str], pgph_i, __lang:str):
print("starting inference...")
if (stmts[0].__contains__('True: ') or stmts[0].__contains__('False: ')):
__correct_stmts = [r[5:].strip() for r in stmts if r.__contains__('True: ')]
__false_stmts = [r[5:].strip() for r in stmts if r.__contains__('False: ')]
else:
__correct_stmts = stmts[:len(stmts)//2]
__false_stmts = stmts[len(stmts)//2:]
__parsed_outputs = []
# while len(__correct_stmts) >= 2:
for c in range(0, len(__correct_stmts), 2):
match randint(0, 6):
case 6:
try:
__parsed_outputs.append(
{
"pgph_i": pgph_i,
"prompt": prompt.USER_PROMPTS['AMEND'] if __lang==lang.VI_VN else prompt.USER_PROMPTS_EN['AMEND'],
"type": "AMEND",
"keys": __correct_stmts[c],
"choices": [__false_stmts[c]]
}
)
__parsed_outputs.append(
{
"pgph_i": pgph_i,
"prompt": prompt.USER_PROMPTS['AMEND'] if __lang==lang.VI_VN else prompt.USER_PROMPTS_EN['AMEND'],
"type": "AMEND",
"keys": __correct_stmts[c+1],
"choices": [__false_stmts[c+1]]
}
)
except:
continue
case 2|4:
__c = __correct_stmts[c:c+2]
# print(min(2, len(__false_stmts)))
try:
__parsed_outputs.append(
{
"pgph_i": pgph_i,
"prompt": prompt.USER_PROMPTS['MULT'] if __lang==lang.VI_VN else prompt.USER_PROMPTS_EN['MULT'],
"type": "MULT",
"keys": __c,
"choices": sample([*__c, *sample( __false_stmts, min(2, len(__false_stmts)) )], min(2, len(__false_stmts)) + len(__c))
}
)
except:
continue
case 3|5:
try:
__c = sample(__false_stmts, 2)
__parsed_outputs.append(
{
"pgph_i": pgph_i,
"prompt": prompt.USER_PROMPTS['MULT_INV'] if __lang==lang.VI_VN else prompt.USER_PROMPTS_EN['MULT_INV'],
"type": "MULT",
"keys": __c,
"choices": sample([*__c, __correct_stmts[0], __correct_stmts[1]], 2+len(__c))
}
)
except:
continue
case 0|1:
for aa in range(2):
try:
_prompt = __correct_stmts[c+aa]
except:
continue
# print(_prompt)
# FIXME: To circumvent some quirky 3rd party lib bugs around chunking phrases with quote, strip them from the sentences for the time being.
_prompt = _prompt.replace("\"", "").replace("\'", "")
_content_w = []
if __lang == lang.VI_VN:
_, _content_w = prompt.parse_content_words([_prompt])
else:
_, _content_w = prompt.parse_content_words_nltk([_prompt])
# print(_proper_n)
for i, ns in enumerate(_content_w, 1):
try:
initials = "...".join([w[0] for w in ns.split(" ") if w])
except:
initials = "..."
_prompt = _prompt.replace(ns, f"({initials}...)", 1)
__parsed_outputs.append(
{
"pgph_i": pgph_i,
"prompt": _prompt,
"type": "OPEN",
"keys": _content_w,
"choices": []
}
)
return __parsed_outputs
async def generate_questions(request):
# parse paragraphs from document file
try:
__ps = await __parse_paragraphs(temp_files[request.path_params['id']][1],batching=True)
except:
return JSONResponse({}, 500)
# Map asyncronous ML prediction function over list of paragraphs
ptasks = []
__raw_outputs = []
__parsed_outputs = []
# print(__ps)
for z, _p in enumerate(__ps):
ptasks.append(__query_ml_predict(qtype=(QType.STMT if z%2==1 else QType.WH), content=_p['content'], header=_p['header'], l=request.path_params.get('lang', lang.VI_VN), num_qs=request.path_params.get('num_qs', 5 * _p.get('count', 1)), token_limit = int(1024 * _p.get('count', 1))))
__raw_outputs = [await p for p in ptasks]
for pgph_i, o in enumerate(__raw_outputs):
# print(o)
print(pgph_i)
# TODO: Parse ML output to JSON
if (o['style'] == QType.WH):
raw_qa_list = []
raw_segmented: list[str] = list(filter(lambda x: (len(x)>0), o['content'].split("\n\n")))[1:]
print(raw_segmented)
for i in range(len(raw_segmented)):
if (len(raw_segmented[i]) and raw_segmented[i].count('\n') < 5):
raw_segmented[i] += f'\n{raw_segmented[i+1]}'
raw_segmented[i+1] = ""
print(raw_segmented)
__parsed_outputs.extend(await parse_wh_question(raw_segmented, pgph_i))
elif (o['style'] == QType.STMT):
print(o['content'])
# remove_after_dash_and_parentheses
stmts = [ sub(r" - .*| \(.*\)", "", x.split('. ',1)[1]) for x in o['content'].split('\n') if bool(match("^\d+\.", x))]
# print(stmts)
__parsed_outputs.extend(await parse_stmt_question(stmts, pgph_i, request.path_params.get('lang', lang.VI_VN)))
# Return the question data
return JSONResponse({"questions": __parsed_outputs, "paragraphs": __ps})
async def scan2OCR(request):
content = b''
ret = []
async with request.form(max_files=10, max_fields=20) as form:
for i in range(int(form['uploads'])):
# Get random file ID
file_id = sys_random.randbytes(12).hex()
# Load image using PIL and convert to opencv grayscale format
im = Image.open(BytesIO(await form[f'upload_{i}'].read()))
# Perform image preprocessing
processed_im = cv2.cvtColor(np.array(im), cv2.COLOR_RGB2GRAY)
cv2.imwrite(f"{file_id}.png", processed_im)
out = pytesseract.image_to_string(f"{file_id}.png", lang="vie")
os.remove(f"{file_id}.png")
# adapt the output text to the HTML-based rich text editor
ret.append({"content": out.replace('\n','<br/>')})
return JSONResponse(ret, 200)
async def convert2html(request):
content = b''
filename = ""
output = ""
files = []
rets = []
async with request.form(max_files=10, max_fields=20) as form:
print(form['uploads'])
for i in range(int(form['uploads'])):
# Get random file ID
filename = sys_random.randbytes(12).hex()
ext = form[f'upload_{i}'].filename.split(".")[-1]
content = await form[f'upload_{i}'].read()
with open(f"{filename}.{ext}", 'wb') as o:
o.write(content)
files.append(f"{filename}.{ext}")
for file in files:
try:
output = pypandoc.convert_file(file, "html")
except:
return JSONResponse({"detail": ""}, status_code=422)
# Extract image sources from document
imgs = []
start = -1
for i in range(len(output)):
if output[i:i+4] == "<img":
start = i
if output[i:i+2] == "/>" and start != -1:
img_tag = output[start:i+2]
imgs.append(img_tag)
start = -1
for x in imgs:
output = output.replace(x, " ")
# Remove upload file
os.remove(file)
rets.append({"content": output, "resources": imgs})
return JSONResponse(rets)
async def get_flashcards(request):
# [title, content, keywords]
__content = temp_files[request.path_params['id']][1]
__lang = request.path_params['lang']
__keywords = [r.strip() for r in temp_files[request.path_params['id']][2] if len(r) > 0]
__tldr = await tldr(__content, __lang)
print(__tldr)
__definitions = await get_definitions_from_words(__keywords, __tldr)
print(__definitions)
return JSONResponse({"tldr": __tldr, "defs": __definitions, "imgs": await fetch_img_for_words(__keywords)})
app = Starlette(debug=True,routes=[
Route('/getFlashcards/{id}/{lang}', get_flashcards, methods=['GET']),
Route('/convert2html',convert2html, methods=['POST']),
Route('/scan2ocr', scan2OCR, methods=['POST']),
Route('/temp', __save_temp, methods=['POST']),
Route('/temp/{id}', __get_temp, methods=['GET']),
Route('/temp/{id}', __remove_temp, methods=['DELETE']),
# Route('/generateQuiz/{id}', generate_questions, methods=['GET']),
Route('/generateQuiz/{id}/{lang}', generate_questions, methods=['GET']),
Route('/convert2md', convert2md, methods=['POST']),
Route('/mltest', __mltest, methods=['GET'])
],
middleware=middleware)
|