Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,52 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
from langchain.prompts.few_shot import FewShotChatMessagePromptTemplate
|
3 |
from langchain.prompts.example_selector import LengthBasedExampleSelector
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
19 |
examples=examples,
|
20 |
-
example_selector=LengthBasedExampleSelector(examples=examples, max_length=
|
21 |
input_variables=["input"],
|
22 |
-
prefix="You are a
|
23 |
-
suffix="Now summarize this
|
24 |
)
|
25 |
|
26 |
# Streamlit UI
|
27 |
-
st.title("π¬ Few-Shot
|
28 |
-
|
29 |
-
input_text = st.text_area("Paste your medical conversation here:")
|
30 |
|
31 |
-
if st.button("Generate
|
32 |
if input_text.strip():
|
33 |
-
|
|
|
34 |
|
35 |
-
st.
|
36 |
-
|
37 |
-
|
38 |
|
39 |
-
|
|
|
|
|
|
|
|
|
40 |
else:
|
41 |
-
st.warning("Please enter some
|
|
|
1 |
import streamlit as st
|
2 |
+
from datasets import load_dataset
|
3 |
+
from transformers import pipeline
|
4 |
from langchain.prompts.few_shot import FewShotChatMessagePromptTemplate
|
5 |
from langchain.prompts.example_selector import LengthBasedExampleSelector
|
6 |
|
7 |
+
# Load dataset (small subset)
|
8 |
+
@st.cache_data
|
9 |
+
def load_examples():
|
10 |
+
dataset = load_dataset("knkarthick/dialogsum", split="train[:5]") # Take only 5 for speed
|
11 |
+
examples = []
|
12 |
+
for example in dataset:
|
13 |
+
examples.append({
|
14 |
+
"input": example["dialogue"],
|
15 |
+
"output": example["summary"]
|
16 |
+
})
|
17 |
+
return examples
|
18 |
+
|
19 |
+
examples = load_examples()
|
20 |
+
|
21 |
+
# Set up the summarization model
|
22 |
+
summarizer = pipeline("summarization", model="t5-small")
|
23 |
+
|
24 |
+
# Few-shot prompt template
|
25 |
+
example_prompt = FewShotChatMessagePromptTemplate.from_examples(
|
26 |
examples=examples,
|
27 |
+
example_selector=LengthBasedExampleSelector(examples=examples, max_length=1000),
|
28 |
input_variables=["input"],
|
29 |
+
prefix="You are a helpful assistant that summarizes dialogues. Examples:",
|
30 |
+
suffix="Now summarize this:\n{input}"
|
31 |
)
|
32 |
|
33 |
# Streamlit UI
|
34 |
+
st.title("π¬ Dialogue Summarizer using Few-Shot Prompt + T5")
|
35 |
+
input_text = st.text_area("π Paste your conversation:")
|
|
|
36 |
|
37 |
+
if st.button("Generate Summary"):
|
38 |
if input_text.strip():
|
39 |
+
# Create prompt
|
40 |
+
messages = example_prompt.format_messages(input=input_text)
|
41 |
|
42 |
+
with st.expander("π Generated Prompt"):
|
43 |
+
for msg in messages:
|
44 |
+
st.markdown(f"**{msg.type.upper()}**:\n```\n{msg.content}\n```")
|
45 |
|
46 |
+
# Generate summary using model
|
47 |
+
full_prompt = "\n".join([m.content for m in messages if m.type == "human"])
|
48 |
+
summary = summarizer("summarize: " + input_text, max_length=80, min_length=15, do_sample=False)[0]['summary_text']
|
49 |
+
st.success("β
Summary:")
|
50 |
+
st.write(summary)
|
51 |
else:
|
52 |
+
st.warning("Please enter some text.")
|