File size: 2,275 Bytes
52d4ec9
50a4735
8e16424
 
e65d3fb
52d4ec9
b3d218a
 
 
e65d3fb
8e16424
 
 
 
490b63e
8e16424
793bade
e65d3fb
8e16424
 
 
 
 
e65d3fb
8e16424
 
e65d3fb
8e16424
 
e65d3fb
 
 
 
 
 
302df45
8e16424
 
 
 
 
 
302df45
 
e65d3fb
302df45
 
 
 
53d3bea
302df45
 
 
53d3bea
 
302df45
 
 
 
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
59
60
61
62
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

# Set page config at the very top, before anything else
st.set_page_config(page_title="DialogSum Few-Shot Summarizer", page_icon="🧠")

# 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."),
    ("human", "Summarize the following dialog:\n\n{dialogue}")
])

# Streamlit UI setup
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:
    # Prepare messages for the final prompt (include few-shot examples here directly)
    formatted_prompt = few_shot_prompt.format_messages(dialogue=user_input)
    
    # Add formatted examples to the final prompt
    formatted_prompt = final_prompt.format_messages(dialogue=user_input)
    formatted_prompt = formatted_prompt + formatted_prompt

    # Get response from model with correct explicit parameters
    llm = HuggingFaceEndpoint(
        repo_id="google/pegasus-xsum",
        task="text2text-generation",
        temperature=0.3,  # Explicitly passing temperature here
        max_new_tokens=128  # Explicitly passing max_new_tokens here
    )
    response = llm(formatted_prompt)

    # Output the summary
    st.subheader("πŸ“Œ Summary:")
    st.write(response)