Spaces:
Sleeping
Sleeping
File size: 1,882 Bytes
52d4ec9 50a4735 8e16424 e65d3fb 52d4ec9 e65d3fb 8e16424 490b63e 8e16424 793bade e65d3fb 8e16424 e65d3fb 8e16424 e65d3fb 8e16424 e65d3fb 8e16424 52d4ec9 490b63e 8e16424 e65d3fb 8e16424 e65d3fb 8e16424 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import streamlit as st
from datasets import load_dataset
from langchain.llms import HuggingFaceEndpoint
from langchain.prompts import FewShotChatMessagePromptTemplate, ChatPromptTemplate
from langchain.schema.messages import SystemMessage
# Load few-shot examples from dialogsum
@st.cache_data
def load_examples(n=3):
dataset = load_dataset("knkarthick/dialogsum", split="train[:20]")
return [{"dialogue": row["dialogue"], "summary": row["summary"]} for row in dataset.select(range(n))]
examples = load_examples()
# Template for each example
example_prompt = ChatPromptTemplate.from_messages([
("human", "Summarize the following dialog:\n\n{dialogue}"),
("ai", "{summary}")
])
# Few-shot prompt template (no prefix/suffix here)
few_shot_prompt = FewShotChatMessagePromptTemplate(
example_prompt=example_prompt,
examples=examples
)
# Now add intro system message + user input separately
final_prompt = ChatPromptTemplate.from_messages([
SystemMessage(content="The following are examples of dialogues and their summaries."),
*few_shot_prompt.messages,
("human", "Summarize the following dialog:\n\n{dialogue}")
])
# Load Pegasus model from HF inference API
llm = HuggingFaceEndpoint(
repo_id="google/pegasus-xsum",
task="text2text-generation",
model_kwargs={"temperature": 0.3, "max_new_tokens": 128}
)
# Streamlit UI
st.set_page_config(page_title="DialogSum Few-Shot Summarizer", page_icon="π§ ")
st.title("π§ Few-Shot Dialog Summarizer")
st.markdown("Uses real examples from `dialogsum` to guide the summary output.")
user_input = st.text_area("βοΈ Paste your dialogue here:", height=200)
if user_input:
# Format messages
messages = final_prompt.format_messages(dialogue=user_input)
# Get response
response = llm(messages)
# Output
st.subheader("π Summary:")
st.write(response)
|