Mahmoud Amiri
add temp cell to print version numberr
62a8987
raw
history blame
4.45 kB
import torch
import gradio as gr
from transformers import pipeline, AutoModelForSeq2SeqLM, AutoTokenizer, AutoConfig
import torch
import gradio as gr
from transformers import pipeline, AutoModelForSeq2SeqLM, AutoTokenizer, AutoConfig
import sys
import pkg_resources
# Log current package versions
for package_name in ["gradio", "transformers", "torch", "sentencepiece"]:
try:
version = pkg_resources.get_distribution(package_name).version
print(f"{package_name}=={version}", file=sys.stderr)
except pkg_resources.DistributionNotFound:
print(f"{package_name} not installed", file=sys.stderr)
# Model name
model_name = "Bocklitz-Lab/lit2vec-tldr-bart-model"
# Placeholder for the summarizer pipeline, tokenizer, and maximum tokens
summarizer = None
tokenizer = None
max_tokens = None
# Automatically load the model at startup
def initialize_model():
global summarizer, tokenizer, max_tokens
try:
summarizer = pipeline("summarization", model=model_name, torch_dtype=torch.float32)
tokenizer = AutoTokenizer.from_pretrained(model_name)
config = AutoConfig.from_pretrained(model_name)
max_tokens = getattr(config, 'max_position_embeddings', 1024)
except Exception as e:
print(f"Model loading failed: {str(e)}")
initialize_model() # Load model at startup
# Example input
example_text = (
"Ultraviolet B (UVB; 290~320nm) irradiation-induced lipid peroxidation induces inflammatory responses that lead to skin wrinkle formation and epidermal thickening. Peroxisome proliferator-activated receptor (PPAR) α/γ dual agonists have the potential to be used as anti-wrinkle agents because they inhibit inflammatory response and lipid peroxidation. In this study, we evaluated the function of 2-bromo-4-(5-chloro-benzo[d]thiazol-2-yl) phenol (MHY 966), a novel synthetic PPAR α/γ dual agonist, and investigated its anti-inflammatory and anti-lipid peroxidation effects. The action of MHY 966 as a PPAR α/γ dual agonist was also determined in vitro by reporter gene assay. Additionally, 8-week-old melanin-possessing hairless mice 2 (HRM2) were exposed to 150 mJ/cm2 UVB every other day for 17 days and MHY 966 was simultaneously pre-treated every day for 17 days to investigate the molecular mechanisms involved. MHY 966 was found to stimulate the transcriptional activities of both PPAR α and γ. In HRM2 mice, we found that the skins of mice exposed to UVB showed significantly increased pro-inflammatory mediator levels (NF-κB, iNOS, and COX-2) and increased lipid peroxidation, whereas MHY 966 co-treatment down-regulated these effects of UVB by activating PPAR α and γ. Thus, the present study shows that MHY 966 exhibits beneficial effects on inflammatory responses and lipid peroxidation by simultaneously activating PPAR α and γ. The major finding of this study is that MHY 966 demonstrates potential as an agent against wrinkle formation associated with chronic UVB exposure."
)
# Function to summarize the input text
def summarize_text(input, min_length, max_length):
if summarizer is None:
return "No model loaded!"
try:
input_tokens = tokenizer.encode(input, return_tensors="pt")
num_tokens = input_tokens.shape[1]
if num_tokens > max_tokens:
return f"Error: Input exceeds the max token limit of {max_tokens}."
min_summary_length = max(10, int(num_tokens * (min_length / 100)))
max_summary_length = min(max_tokens, int(num_tokens * (max_length / 100)))
output = summarizer(input, min_length=min_summary_length, max_length=max_summary_length, truncation=True)
return output[0]['summary_text']
except Exception as e:
return f"Summarization failed: {str(e)}"
# Gradio Interface
with gr.Blocks() as demo:
gr.Markdown("## TL;DR Summarizer")
min_length_slider = gr.Slider(minimum=0, maximum=100, step=1, label="Minimum Summary Length (%)", value=10)
max_length_slider = gr.Slider(minimum=0, maximum=100, step=1, label="Maximum Summary Length (%)", value=50)
input_text = gr.Textbox(label="Input text to summarize", lines=6, value=example_text)
summarize_button = gr.Button("Summarize Text")
output_text = gr.Textbox(label="Summarized text", lines=4)
summarize_button.click(fn=summarize_text, inputs=[input_text, min_length_slider, max_length_slider],
outputs=output_text)
demo.launch()