Spaces:
Runtime error
Runtime error
import gradio as gr | |
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM | |
from googletrans import Translator | |
# λͺ¨λΈ λ‘λ | |
model_name = "Aoi785/kobart-disaster-summary-v1" | |
tokenizer = AutoTokenizer.from_pretrained("digit82/kobart-summarization") | |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name) | |
summarizer = pipeline("summarization", model=model, tokenizer=tokenizer) | |
# λ²μκΈ° | |
translator = Translator() | |
# μμ½ + λ²μ ν¨μ | |
def summarize_and_translate(text, lang): | |
if not text.strip(): | |
return "μ λ ₯λ ν μ€νΈκ° μμ΅λλ€.", "" | |
# μμ½ | |
try: | |
summary = summarizer(text, max_length=50, min_length=10, do_sample=False)[0]['summary_text'] | |
except Exception as e: | |
summary = f"μμ½ μ€ν¨: {str(e)}" | |
return summary, "" | |
# λ²μ | |
try: | |
if lang == "ko": | |
translated = summary | |
else: | |
translated = translator.translate(summary, dest=lang).text | |
except Exception as e: | |
translated = f"λ²μ μ€ν¨: {str(e)}" | |
return summary, translated | |
# Gradio UI | |
with gr.Blocks() as demo: | |
gr.Markdown("## π μ¬λ λ¬Έμ μμ½ λ° λ²μκΈ°") | |
with gr.Row(): | |
with gr.Column(): | |
input_text = gr.Textbox(label="μ¬λ λ¬Έμ μ λ ₯", lines=5, placeholder="μ: λ΄μΌκΉμ§ μ₯λ§μ μ μν₯μΌλ‘ λ§μ λΉ μμ...") | |
lang = gr.Dropdown( | |
choices=["ko(νκ΅μ΄)", "en(English)", "zh(μ€κ΅μ΄)", "vi(λ² νΈλ¨μ΄)"], | |
value="ko(νκ΅μ΄)", | |
label="λ²μ μΈμ΄ μ ν" | |
) | |
run_button = gr.Button("μμ½ λ° λ²μ μ€ν") | |
with gr.Column(): | |
output_summary = gr.Textbox(label="β μμ½ κ²°κ³Ό") | |
output_trans = gr.Textbox(label="π λ²μ κ²°κ³Ό") | |
def process_lang_code(lang_dropdown): | |
return lang_dropdown.split("(")[0] | |
run_button.click( | |
fn=lambda txt, ln: summarize_and_translate(txt, process_lang_code(ln)), | |
inputs=[input_text, lang], | |
outputs=[output_summary, output_trans] | |
) | |
demo.launch() |