|
|
| import os
|
| import threading
|
| from pathlib import Path
|
|
|
| try:
|
| from huggingface_hub import CommitScheduler
|
| except Exception:
|
| CommitScheduler = None
|
|
|
|
|
| class _LocalScheduler:
|
| """Fallback logger lock when HF_TOKEN is unavailable."""
|
|
|
| def __init__(self):
|
| self.lock = threading.Lock()
|
|
|
|
|
| def make_scheduler(repo_id: str, log_folder: Path):
|
| log_folder.mkdir(parents=True, exist_ok=True)
|
| token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_HUB_TOKEN")
|
| if token and CommitScheduler is not None:
|
| try:
|
| return CommitScheduler(
|
| repo_id=repo_id,
|
| repo_type="dataset",
|
| folder_path=log_folder,
|
| path_in_repo="data",
|
| every=2,
|
| token=token,
|
| )
|
| except Exception as exc:
|
| print(f"CommitScheduler disabled: {exc}")
|
| return _LocalScheduler()
|
|
|
|
|
|
|
| from transformers import pipeline
|
| import torch
|
| import gradio as gr
|
|
|
| from pathlib import Path
|
|
|
| import os
|
| import uuid
|
| import joblib
|
| import json
|
|
|
|
|
|
|
| log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
|
| log_folder = log_file.parent
|
|
|
| scheduler = make_scheduler("text-summarization-logs", log_folder)
|
|
|
|
|
| text_summary = None
|
|
|
| def get_summarizer():
|
| global text_summary
|
| if text_summary is None:
|
| text_summary = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
| return text_summary
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| def summary(input):
|
| output = get_summarizer()(input)
|
|
|
| with scheduler.lock:
|
| with log_file.open("a") as f:
|
| f.write(json.dumps(
|
| {
|
| 'Input Text': input,
|
| 'Summary':output[0]['summary_text']
|
| }
|
| ))
|
| f.write("\n")
|
|
|
| return output[0]['summary_text']
|
|
|
| gr.close_all()
|
|
|
|
|
| demo = gr.Interface(fn=summary,
|
| inputs=[gr.Textbox(label="Input text to summarization", lines=6)],
|
| outputs=[gr.Textbox(label="Summarized text", lines=4)],
|
| title='Text Summarization',
|
| description='This application will be used to summarize the text',
|
| theme=gr.themes.Soft(),
|
| concurrency_limit=16)
|
|
|
| demo.launch(share=True) |