Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add text summarization.py #249

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/apps/pages/programs/ApiPrograms/text_summarization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import os
import streamlit as st
from langchain_groq import ChatGroq
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import JsonOutputParser
import json

# Function to check if the Groq API key exists
def API_Exists():
if "GROQ_API_KEY" in st.secrets["api_key"] and st.secrets["api_key"]["GROQ_API_KEY"]:
return True
elif "GROQ_API_KEY" in os.environ and os.environ["GROQ_API_KEY"]:
return True
return False

# Streamlit app
def text_summarization():
st.title("Text Summarization using Groq")
st.markdown("Enter text to generate a concise summary using the Groq LLM.")

# Check if API key exists
if not API_Exists():
st.error("Groq API key is missing. Please add it to Streamlit secrets or environment variables.")
return

# Retrieve the API key
GROQ_API_KEY = st.secrets["api_key"].get("GROQ_API_KEY", os.environ.get("GROQ_API_KEY"))

# Initialize the Groq LLM
llm = ChatGroq(
model_name="llama-3.3-70b-versatile",
api_key=GROQ_API_KEY,
temperature=0.7
)

# Define the summarization prompt
summarization_prompt = ChatPromptTemplate.from_messages([
("system", """Summarize the given text into a concise and coherent paragraph.
Return the summary in the following JSON format:
{{
"summary": "your summary here"
}}
"""),
("user", "{input}")
])

# Define a JSON output parser
parser = JsonOutputParser(pydantic_object={
"type": "object",
"properties": {
"summary": {"type": "string"}
}
})

# Create the summarization chain
chain = summarization_prompt | llm | parser

# Input text for summarization
input_text = st.text_area("Enter Text to Summarize", placeholder="Paste your text here...")

if st.button("Generate Summary"):
if not input_text.strip():
st.warning("Please enter some text to summarize.")
else:
try:
# Summarize the text
result = chain.invoke({"input": input_text})
st.success("Summary Generated!")
st.json(result)
except Exception as e:
st.error(f"Error during summarization: {e}")