File size: 1,914 Bytes
baa8f63
84cb511
 
1fb01d9
84cb511
1fb01d9
bcf654a
 
6722d35
 
84cb511
1fb01d9
84cb511
 
 
 
 
 
 
1fb01d9
84cb511
 
 
 
 
 
 
 
 
1fb01d9
 
 
84cb511
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bdc5d4
84cb511
485cde9
6bdc5d4
84cb511
 
 
 
6bdc5d4
 
 
84cb511
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
import os
from dotenv import load_dotenv
import gradio as gr
from langchain_core.prompts import PromptTemplate
from langchain_huggingface import HuggingFaceEndpoint
from langchain_core.chains import LLMChain

# Load environment variables
load_dotenv()

HF_TOKEN = os.getenv("HF_TOKEN")

# Initialize the HuggingFace model
llm = HuggingFaceEndpoint(
    repo_id="mistralai/Mistral-7B-Instruct-v0.3",
    huggingfacehub_api_token=HF_TOKEN,
    temperature=0.7,
    max_new_tokens=200
)

# Define a prompt template for generating a blog
TEMPLATE = """
Write a detailed blog post on the following topic:
Topic: {topic}
Make sure the blog post is informative, engaging, and well-structured.
"""

# Create a prompt template instance
blog_prompt_template = PromptTemplate(input_variables=["topic"], template=TEMPLATE)

# Initialize the LLMChain with the HuggingFace model and prompt template
blog_chain = LLMChain(llm=llm, prompt=blog_prompt_template)

def generate_blog_post(topic: str, author_name: str) -> str:
    if topic:
        # Generate the blog post
        blog_post = blog_chain.run({"topic": topic})

        # Add author name if provided
        if author_name:
            blog_post = f"**By {author_name}**\n\n" + blog_post

        return blog_post
    else:
        return "Please enter a topic for the blog post."

# Define the Gradio interface
interface = gr.Interface(
    fn=generate_blog_post,
    inputs=[
        gr.Textbox(label="Blog Topic", placeholder="Enter the topic here"),
        gr.Textbox(label="Author Name", placeholder="Optional")
    ],
    outputs="text",
    title="AI Blog Generator",
    description="Welcome to the AI Blog Generator. This tool allows you to generate high-quality, engaging blog posts in just a few clicks. Simply provide a topic, and the AI will create a detailed blog post for you.",
    theme="default"
)

if __name__ == "__main__":
    interface.launch()