mayankchugh-learning's picture
Fix CommitScheduler helper SyntaxError
e9affde verified
Raw
History Blame Contribute Delete
3.72 kB
import os
import threading
from pathlib import Path
try:
from huggingface_hub import CommitScheduler
except Exception: # pragma: no cover
CommitScheduler = None # type: ignore
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()
# Use a pipeline as a high-level helper
from transformers import pipeline
import torch
import gradio as gr
from pathlib import Path
import os
import uuid
import joblib
import json
# Prepare the logging functionality
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
# model_path = "Model/models--sshleifer--distilbart-cnn-12-6/snapshots/a4f8f3ea906ed274767e9906dbaede7531d660ff"
# text_summary = pipeline("summarization", model=model_path, torch_dtype=torch.bfloat16)
# text="Why You Can Trust Forbes Advisor Small Business \
# The Forbes Advisor Small Business team is committed to bringing you unbiased \
# rankings and information with full editorial independence. We use product data, \
# strategic methodologies and expert insights to inform all of our content and guide \
# you in making the best decisions for your business journey.\
# We reviewed 11 systems to help you find the best blogging platform for your blog or \
# small business. Our ratings looked at factors that included the platform’s starting \
# price (including whether it offered a free trial or free version); useful general features,\
# such as drag-and-drop functionality and search engine optimization (SEO) tools; unique features, \
# how well the blogging platform fared on third-party review sites and a final review by our experts.\
# All ratings are determined solely by our editorial team."
# print(text_summary(text)[0])
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="text",outputs='text',title='Text Summarization Gradio Huggingface')
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)