Update app.py
Browse files
app.py
CHANGED
|
@@ -113,7 +113,7 @@ def get_base64_image(image):
|
|
| 113 |
return f"data:image/jpeg;base64,{img_str}"
|
| 114 |
|
| 115 |
@spaces.GPU(duration=120)
|
| 116 |
-
def extract_vocabulary(pdf_text, images, translit_lang, translit_format, target_lang, max_text_char=1500):
|
| 117 |
"""Use Transformers to extract vocabulary from text and images."""
|
| 118 |
global model, processor
|
| 119 |
|
|
@@ -187,7 +187,7 @@ Text:
|
|
| 187 |
streamer=streamer,
|
| 188 |
max_new_tokens=2048*16,
|
| 189 |
do_sample=True,
|
| 190 |
-
repetition_penalty=
|
| 191 |
)
|
| 192 |
|
| 193 |
if len(images) > 0:
|
|
@@ -231,7 +231,7 @@ Text:
|
|
| 231 |
print(f"Error parsing JSON: {e}\nRaw output: {output_text}")
|
| 232 |
yield output_text, []
|
| 233 |
|
| 234 |
-
def translate_vocabulary(korean_words, translit_lang, translit_format, target_lang):
|
| 235 |
"""Use Transformers text-only inference to translate/transliterate Korean words."""
|
| 236 |
global model, processor
|
| 237 |
|
|
@@ -282,7 +282,7 @@ Korean words:
|
|
| 282 |
# top_p=0.95,
|
| 283 |
temperature=1.0, top_p=0.95, top_k=20, min_p=0.0,
|
| 284 |
# presence_penalty=1.5,
|
| 285 |
-
repetition_penalty=
|
| 286 |
do_sample=True
|
| 287 |
)
|
| 288 |
|
|
@@ -331,7 +331,7 @@ def hash_file(filepath):
|
|
| 331 |
return hashlib.md5(f.read(1024*1024)).hexdigest()
|
| 332 |
|
| 333 |
@spaces.GPU(duration=120)
|
| 334 |
-
def process_pdf(pdf_file, url_input, translit_lang, translit_format, target_lang, max_text_char, last_source_hash, last_korean_words, progress=gr.Progress()):
|
| 335 |
global tts, voice_style
|
| 336 |
|
| 337 |
# Clean language choices from "Family - Language" to just "Language"
|
|
@@ -359,7 +359,7 @@ def process_pdf(pdf_file, url_input, translit_lang, translit_format, target_lang
|
|
| 359 |
# progress(0.2, desc="Translating previously extracted vocabulary...")
|
| 360 |
# korean_words = [item.get("korean") for item in last_korean_words if item.get("korean")]
|
| 361 |
# for attempt in range(1, 4):
|
| 362 |
-
# vocab_list = translate_vocabulary(korean_words, translit_lang, translit_format, target_lang)
|
| 363 |
# if vocab_list:
|
| 364 |
# break
|
| 365 |
# else:
|
|
@@ -383,7 +383,7 @@ def process_pdf(pdf_file, url_input, translit_lang, translit_format, target_lang
|
|
| 383 |
stream_text = ""
|
| 384 |
for attempt in range(1, 4):
|
| 385 |
progress(0.2, desc=f"Extracting vocabulary (Attempt {attempt}/3)...")
|
| 386 |
-
for stream_t, v_list in extract_vocabulary(content_text, images, translit_lang, translit_format, target_lang, max_text_char):
|
| 387 |
stream_text = stream_t
|
| 388 |
if v_list is not None:
|
| 389 |
vocab_list = v_list
|
|
@@ -758,6 +758,7 @@ def create_demo():
|
|
| 758 |
value="Indo-European - English"
|
| 759 |
)
|
| 760 |
max_text_char_input = gr.Slider(minimum=1000, maximum=30000, step=1000, value=1500, label="Max Input Text Length (Characters)")
|
|
|
|
| 761 |
|
| 762 |
with gr.Row():
|
| 763 |
submit_btn = gr.Button("✨ Generate Flashcards ✨", variant="primary")
|
|
@@ -776,7 +777,7 @@ def create_demo():
|
|
| 776 |
|
| 777 |
generate_event = submit_btn.click(
|
| 778 |
fn=process_pdf,
|
| 779 |
-
inputs=[pdf_input, url_input, translit_lang, translit_format, target_lang, max_text_char_input, last_source_state, last_korean_words_state],
|
| 780 |
outputs=[output_html, last_source_state, last_korean_words_state, stream_box, extracted_text_box, extracted_images_gallery]
|
| 781 |
)
|
| 782 |
|
|
|
|
| 113 |
return f"data:image/jpeg;base64,{img_str}"
|
| 114 |
|
| 115 |
@spaces.GPU(duration=120)
|
| 116 |
+
def extract_vocabulary(pdf_text, images, translit_lang, translit_format, target_lang, max_text_char=1500, repetition_penalty_val=1.1):
|
| 117 |
"""Use Transformers to extract vocabulary from text and images."""
|
| 118 |
global model, processor
|
| 119 |
|
|
|
|
| 187 |
streamer=streamer,
|
| 188 |
max_new_tokens=2048*16,
|
| 189 |
do_sample=True,
|
| 190 |
+
repetition_penalty=repetition_penalty_val,
|
| 191 |
)
|
| 192 |
|
| 193 |
if len(images) > 0:
|
|
|
|
| 231 |
print(f"Error parsing JSON: {e}\nRaw output: {output_text}")
|
| 232 |
yield output_text, []
|
| 233 |
|
| 234 |
+
def translate_vocabulary(korean_words, translit_lang, translit_format, target_lang, repetition_penalty_val=1.1):
|
| 235 |
"""Use Transformers text-only inference to translate/transliterate Korean words."""
|
| 236 |
global model, processor
|
| 237 |
|
|
|
|
| 282 |
# top_p=0.95,
|
| 283 |
temperature=1.0, top_p=0.95, top_k=20, min_p=0.0,
|
| 284 |
# presence_penalty=1.5,
|
| 285 |
+
repetition_penalty=repetition_penalty_val,
|
| 286 |
do_sample=True
|
| 287 |
)
|
| 288 |
|
|
|
|
| 331 |
return hashlib.md5(f.read(1024*1024)).hexdigest()
|
| 332 |
|
| 333 |
@spaces.GPU(duration=120)
|
| 334 |
+
def process_pdf(pdf_file, url_input, translit_lang, translit_format, target_lang, max_text_char, repetition_penalty_val, last_source_hash, last_korean_words, progress=gr.Progress()):
|
| 335 |
global tts, voice_style
|
| 336 |
|
| 337 |
# Clean language choices from "Family - Language" to just "Language"
|
|
|
|
| 359 |
# progress(0.2, desc="Translating previously extracted vocabulary...")
|
| 360 |
# korean_words = [item.get("korean") for item in last_korean_words if item.get("korean")]
|
| 361 |
# for attempt in range(1, 4):
|
| 362 |
+
# vocab_list = translate_vocabulary(korean_words, translit_lang, translit_format, target_lang, repetition_penalty_val)
|
| 363 |
# if vocab_list:
|
| 364 |
# break
|
| 365 |
# else:
|
|
|
|
| 383 |
stream_text = ""
|
| 384 |
for attempt in range(1, 4):
|
| 385 |
progress(0.2, desc=f"Extracting vocabulary (Attempt {attempt}/3)...")
|
| 386 |
+
for stream_t, v_list in extract_vocabulary(content_text, images, translit_lang, translit_format, target_lang, max_text_char, repetition_penalty_val):
|
| 387 |
stream_text = stream_t
|
| 388 |
if v_list is not None:
|
| 389 |
vocab_list = v_list
|
|
|
|
| 758 |
value="Indo-European - English"
|
| 759 |
)
|
| 760 |
max_text_char_input = gr.Slider(minimum=1000, maximum=30000, step=1000, value=1500, label="Max Input Text Length (Characters)")
|
| 761 |
+
repetition_penalty_input = gr.Slider(minimum=0.1, maximum=2.0, step=0.1, value=1.2, label="Repetition Penalty")
|
| 762 |
|
| 763 |
with gr.Row():
|
| 764 |
submit_btn = gr.Button("✨ Generate Flashcards ✨", variant="primary")
|
|
|
|
| 777 |
|
| 778 |
generate_event = submit_btn.click(
|
| 779 |
fn=process_pdf,
|
| 780 |
+
inputs=[pdf_input, url_input, translit_lang, translit_format, target_lang, max_text_char_input, repetition_penalty_input, last_source_state, last_korean_words_state],
|
| 781 |
outputs=[output_html, last_source_state, last_korean_words_state, stream_box, extracted_text_box, extracted_images_gallery]
|
| 782 |
)
|
| 783 |
|