Spaces:
Sleeping
Sleeping
import streamlit as st | |
from langchain.prompts import PromptTemplate | |
from langchain.chains import LLMChain | |
from langchain_community.llms import HuggingFaceHub | |
from huggingface_hub import login | |
from dotenv import load_dotenv | |
import os | |
load_dotenv() | |
# Authenticate with Hugging Face Hub | |
hf_token = os.getenv("HUGGINGFACEHUB_API_TOKEN") | |
login(hf_token) | |
# Initialize LLM using HuggingFaceHub | |
llm = HuggingFaceHub( | |
repo_id="caffsean/t5-base-finetuned-keyword-to-text-generation", | |
model_kwargs={ | |
"temperature": 0.7, # Adjust for creativity | |
"max_length": 512, # Adjust for desired output length | |
} | |
) | |
# Define a function to generate responses | |
def get_blog_response(keywords: str, no_words: str, blog_style: str) -> str: | |
# Define a prompt template | |
template = "Generate a blog for {blog_style} job profile based on the keywords '{keywords}' within {no_words} words." | |
prompt = PromptTemplate( | |
input_variables=["keywords", "no_words", "blog_style"], | |
template=template | |
) | |
# Use LangChain LLMChain for structured interaction | |
chain = LLMChain(llm=llm, prompt=prompt) | |
response = chain.run(keywords=keywords, no_words=no_words, blog_style=blog_style) | |
return response | |
# Streamlit UI | |
st.set_page_config( | |
page_title="Blog Generation Using LangChain and Hugging Face", | |
layout="centered", | |
initial_sidebar_state="collapsed" | |
) | |
st.header("Blog Generation App :earth_americas:") | |
st.write("This app uses LangChain and a fine-tuned T5 model for generating blogs. Please provide your inputs below:") | |
# Input fields | |
keywords = st.text_input("Enter the Blog Keywords") | |
col1, col2 = st.columns([5, 5]) | |
with col1: | |
no_words = st.text_input("Number of Words") | |
with col2: | |
blog_style = st.selectbox( | |
"Writing the blog for", | |
["Researchers", "Engineers", "Doctors", "Content Creators", "Sportsman", "Businessman", "Common People"], | |
index=0 | |
) | |
submit = st.button("Generate Blog") | |
# Generate response | |
if submit: | |
if keywords and no_words.isdigit() and int(no_words) > 0: | |
try: | |
st.write("Generating blog...") | |
response = get_blog_response(keywords, no_words, blog_style) | |
st.markdown(f"### Generated Blog:\n\n{response}") | |
except Exception as e: | |
st.error(f"An error occurred: {e}") | |
else: | |
st.warning("Please provide valid inputs for all fields.") | |