File size: 1,934 Bytes
7bdcb82
 
 
 
 
 
 
 
 
 
 
 
241acec
7bdcb82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from langchain.prompts import ChatPromptTemplate
from langchain.schema import SystemMessage, HumanMessage
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
from dotenv import load_dotenv
import os

load_dotenv()
HF_TOKEN = os.environ.get('HF_TOKEN')
os.environ['HF_TOKEN'] = HF_TOKEN

llm = HuggingFaceEndpoint(
    repo_id="mistralai/Mistral-7B-Instruct-v0.2",
    task="text-generation",
    huggingfacehub_api_token=HF_TOKEN
)
llm = ChatHuggingFace(llm=llm)

def getLLamaresponse(input_text, no_words, blog_style):
    if not input_text or not no_words:
        return "⚠️ Please enter a valid topic and word count."

    try:
        template = """
        Write a blog for {blog_style} job profile on the topic "{input_text}" 
        within {no_words} words.
        """

        prompt = ChatPromptTemplate.from_template(template)

        messages = [
            SystemMessage(content="You are a helpful blog writer."),
            HumanMessage(content=prompt.format(blog_style=blog_style, input_text=input_text, no_words=no_words))
        ]

        response = llm.invoke(messages)  
        return response.content if response else "⚠️ No response from the model."

    except Exception as e:
        return f"❌ Error: {str(e)}"


st.set_page_config(
    page_title="Generate Blogs",
    page_icon='🤖',
    layout='centered',
    initial_sidebar_state='collapsed'
)

st.header("Generate Blogs 🤖")


input_text = st.text_input("Enter the Blog Topic")

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', 'Data Scientist', 'Common People'), 
                              index=0)

submit = st.button("Generate")
if submit:
    response = getLLamaresponse(input_text, no_words, blog_style)
    st.write(response)