File size: 2,491 Bytes
c701e36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
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.")