Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,74 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def summarize(text):
|
| 8 |
-
|
| 9 |
-
payload = {"inputs": text}
|
| 10 |
-
|
| 11 |
try:
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
# Handle different response formats
|
| 17 |
-
if isinstance(result, list) and len(result) > 0:
|
| 18 |
-
return result[0].get('summary_text', result[0].get('generated_text', str(result[0])))
|
| 19 |
-
elif isinstance(result, dict):
|
| 20 |
-
return result.get('summary_text', result.get('generated_text', str(result)))
|
| 21 |
-
else:
|
| 22 |
-
return str(result)
|
| 23 |
except Exception as e:
|
| 24 |
return f"Error: {str(e)}"
|
| 25 |
|
| 26 |
# Create Gradio interface
|
| 27 |
demo = gr.Interface(
|
| 28 |
fn=summarize,
|
| 29 |
-
inputs=gr.Textbox(
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
title="SummLlama3.2-3B Summarization",
|
| 32 |
-
description="Test the DISLab/SummLlama3.2-3B model
|
|
|
|
|
|
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
os.environ["SPACES_ZERO_GPU"] = "false"
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from transformers import pipeline, AutoTokenizer
|
| 6 |
+
import torch
|
| 7 |
|
| 8 |
+
# Load model and tokenizer
|
| 9 |
+
model_name = "DISLab/SummLlama3.2-3B"
|
| 10 |
+
print(f"Loading model: {model_name}")
|
| 11 |
+
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 13 |
+
pipe = pipeline(
|
| 14 |
+
"text-generation",
|
| 15 |
+
model=model_name,
|
| 16 |
+
tokenizer=tokenizer,
|
| 17 |
+
device_map="auto",
|
| 18 |
+
torch_dtype=torch.float16
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
print("Model loaded successfully!")
|
| 22 |
+
|
| 23 |
+
def format_chat_template(document):
|
| 24 |
+
"""Format input using the recommended template from model card"""
|
| 25 |
+
instruction = "Please summarize the input document."
|
| 26 |
+
row_json = [{
|
| 27 |
+
"role": "user",
|
| 28 |
+
"content": f"Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n### Instruction:\n{instruction}\n\n### Input:\n{document}\n\n### Response:\n"
|
| 29 |
+
}]
|
| 30 |
+
return tokenizer.apply_chat_template(row_json, tokenize=False, add_generation_prompt=False)
|
| 31 |
|
| 32 |
def summarize(text):
|
| 33 |
+
"""Generate summary using the model"""
|
|
|
|
|
|
|
| 34 |
try:
|
| 35 |
+
# Format input with recommended template
|
| 36 |
+
formatted_input = format_chat_template(text)
|
| 37 |
+
|
| 38 |
+
# Generate summary
|
| 39 |
+
output = pipe(
|
| 40 |
+
formatted_input,
|
| 41 |
+
max_new_tokens=512,
|
| 42 |
+
do_sample=True,
|
| 43 |
+
temperature=0.3,
|
| 44 |
+
top_p=0.9,
|
| 45 |
+
return_full_text=False
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Extract the generated summary
|
| 49 |
+
summary = output[0]['generated_text'].strip()
|
| 50 |
+
return summary
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
except Exception as e:
|
| 53 |
return f"Error: {str(e)}"
|
| 54 |
|
| 55 |
# Create Gradio interface
|
| 56 |
demo = gr.Interface(
|
| 57 |
fn=summarize,
|
| 58 |
+
inputs=gr.Textbox(
|
| 59 |
+
lines=10,
|
| 60 |
+
placeholder="Enter text to summarize...",
|
| 61 |
+
label="Input Text"
|
| 62 |
+
),
|
| 63 |
+
outputs=gr.Textbox(
|
| 64 |
+
label="Summary",
|
| 65 |
+
lines=5
|
| 66 |
+
),
|
| 67 |
title="SummLlama3.2-3B Summarization",
|
| 68 |
+
description="Test the DISLab/SummLlama3.2-3B model - a specialized summarization model trained with DPO",
|
| 69 |
+
examples=[
|
| 70 |
+
["Artificial intelligence has made remarkable progress in recent years, particularly in natural language processing. Large language models can now understand context, generate human-like text, and perform complex reasoning tasks. These advances have enabled applications ranging from chatbots to code generation tools, transforming how we interact with technology."]
|
| 71 |
+
]
|
| 72 |
)
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|